safe-stable-stringify
Advanced tools
Comparing version 2.2.0 to 2.3.0
# Changelog | ||
## v2.3.0 | ||
- Accept the `Error` constructor as `circularValue` option to throw on circular references as the regular JSON.stringify would: | ||
```js | ||
import { configure } from 'safe-stable-stringify' | ||
const object = {} | ||
object.circular = object; | ||
const stringify = configure({ circularValue: TypeError }) | ||
stringify(object) | ||
// TypeError: Converting circular structure to JSON | ||
``` | ||
- Fixed escaping wrong surrogates. Only lone surrogates are now escaped. | ||
## v2.2.0 | ||
@@ -4,0 +22,0 @@ |
@@ -6,3 +6,3 @@ export function stringify(value: any, replacer?: (key: string, value: any) => any, space?: string | number): string; | ||
bigint?: boolean, | ||
circularValue?: string | null, | ||
circularValue?: string | null | TypeErrorConstructor | ErrorConstructor, | ||
deterministic?: boolean, | ||
@@ -9,0 +9,0 @@ maximumBreadth?: number, |
36
index.js
@@ -21,5 +21,5 @@ 'use strict' | ||
// eslint-disable-next-line | ||
const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/ | ||
const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/ | ||
// eslint-disable-next-line | ||
const strEscapeSequencesReplacer = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/g | ||
const strEscapeSequencesReplacer = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?<![\ud800-\udbff])[\udc00-\udfff]/g | ||
@@ -47,3 +47,3 @@ // Escaped special characters. Use empty strings to fill up unused entries. | ||
? meta[charCode] | ||
: `\\u${charCode.toString(16).padStart(4, '0')}` | ||
: `\\u${charCode.toString(16)}` | ||
} | ||
@@ -68,4 +68,11 @@ | ||
last = i + 1 | ||
} else if (point >= 55296 && point <= 57343) { | ||
result += `${str.slice(last, i)}${`\\u${point.toString(16).padStart(4, '0')}`}` | ||
} else if (point >= 0xd800 && point <= 0xdfff) { | ||
if (point <= 0xdbff && i + 1 < str.length) { | ||
const point = str.charCodeAt(i + 1) | ||
if (point >= 0xdc00 && point <= 0xdfff) { | ||
i++ | ||
continue | ||
} | ||
} | ||
result += `${str.slice(last, i)}${`\\u${point.toString(16)}`}` | ||
last = i + 1 | ||
@@ -126,10 +133,17 @@ } | ||
if (typeof circularValue === 'string') { | ||
circularValue = `"${circularValue}"` | ||
} else if (circularValue === undefined) { | ||
return | ||
} else if (circularValue !== null) { | ||
throw new TypeError('The "circularValue" argument must be of type string or the value null or undefined') | ||
return `"${circularValue}"` | ||
} | ||
if (circularValue == null) { | ||
return circularValue | ||
} | ||
if (circularValue === Error || circularValue === TypeError) { | ||
return { | ||
toString () { | ||
throw new TypeError('Converting circular structure to JSON') | ||
} | ||
} | ||
} | ||
throw new TypeError('The "circularValue" argument must be of type string or the value null or undefined') | ||
} | ||
return circularValue === undefined ? '"[Circular]"' : circularValue | ||
return '"[Circular]"' | ||
} | ||
@@ -136,0 +150,0 @@ |
{ | ||
"name": "safe-stable-stringify", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "Deterministic and safely JSON.stringify to quickly serialize JavaScript objects", | ||
@@ -32,3 +32,3 @@ "exports": { | ||
"lint": "standard --fix", | ||
"tsc": "tsc" | ||
"tsc": "tsc --project tsconfig.json" | ||
}, | ||
@@ -50,2 +50,3 @@ "engines": { | ||
"faster-stable-stringify": "^1.0.0", | ||
"fastest-stable-stringify": "^2.0.2", | ||
"json-stable-stringify": "^1.0.1", | ||
@@ -52,0 +53,0 @@ "json-stringify-deterministic": "^1.0.1", |
@@ -47,5 +47,6 @@ # safe-stable-stringify | ||
they are ignored. **Default:** `true`. | ||
* `circularValue` {string|null|undefined} Defines the value for circular | ||
references. Set to `undefined`, circular properties are not serialized (array | ||
entries are replaced with `null`). **Default:** `[Circular]`. | ||
* `circularValue` {string|null|undefined|ErrorConstructor} Defines the value for | ||
circular references. Set to `undefined`, circular properties are not | ||
serialized (array entries are replaced with `null`). Set to `Error`, to throw | ||
on circular references. **Default:** `[Circular]`. | ||
* `deterministic` {boolean} If `true`, guarantee a deterministic key order | ||
@@ -93,2 +94,9 @@ instead of relying on the insertion order. **Default:** `true`. | ||
// } | ||
const throwOnCircular = configure({ | ||
circularValue: Error | ||
}) | ||
throwOnCircular(circular); | ||
// TypeError: Converting circular structure to JSON | ||
``` | ||
@@ -95,0 +103,0 @@ |
@@ -20,3 +20,4 @@ { | ||
}, | ||
"include": ["**/*.js", "**/*.ts"], | ||
"exclude": ["compare.js", "benchmark.js", "./coverage"] | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
33016
616
171
0
15