safe-stable-stringify
Advanced tools
Comparing version 2.4.3 to 2.5.0
@@ -10,3 +10,3 @@ export type Replacer = (number | string)[] | null | undefined | ((key: string, value: unknown) => string | number | boolean | null | object) | ||
circularValue?: string | null | TypeErrorConstructor | ErrorConstructor, | ||
deterministic?: boolean, | ||
deterministic?: boolean | ((a: string, b: string) => number), | ||
maximumBreadth?: number, | ||
@@ -13,0 +13,0 @@ maximumDepth?: number, |
35
index.js
@@ -23,3 +23,3 @@ 'use strict' | ||
// eslint-disable-next-line no-control-regex | ||
const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]|[\ud800-\udbff](?![\udc00-\udfff])|(?:[^\ud800-\udbff]|^)[\udc00-\udfff]/ | ||
const strEscapeSequencesRegExp = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/ | ||
@@ -36,7 +36,7 @@ // Escape C0 control characters, double quotes, the backslash and every code | ||
function insertSort (array) { | ||
// Insertion sort is very efficient for small input sizes but it has a bad | ||
function sort (array, comparator) { | ||
// Insertion sort is very efficient for small input sizes, but it has a bad | ||
// worst case complexity. Thus, use native array sort for bigger values. | ||
if (array.length > 2e2) { | ||
return array.sort() | ||
if (array.length > 2e2 || comparator) { | ||
return array.sort(comparator) | ||
} | ||
@@ -102,2 +102,13 @@ for (let i = 1; i < array.length; i++) { | ||
function getDeterministicOption (options) { | ||
let value | ||
if (hasOwnProperty.call(options, 'deterministic')) { | ||
value = options.deterministic | ||
if (typeof value !== 'boolean' && typeof value !== 'function') { | ||
throw new TypeError('The "deterministic" argument must be of type boolean or comparator function') | ||
} | ||
} | ||
return value === undefined ? true : value | ||
} | ||
function getBooleanOption (options, key) { | ||
@@ -177,3 +188,4 @@ let value | ||
const bigint = getBooleanOption(options, 'bigint') | ||
const deterministic = getBooleanOption(options, 'deterministic') | ||
const deterministic = getDeterministicOption(options) | ||
const comparator = typeof deterministic === 'function' ? deterministic : undefined | ||
const maximumDepth = getPositiveIntegerOption(options, 'maximumDepth') | ||
@@ -255,3 +267,3 @@ const maximumBreadth = getPositiveIntegerOption(options, 'maximumBreadth') | ||
if (deterministic && !isTypedArrayWithEntries(value)) { | ||
keys = insertSort(keys) | ||
keys = sort(keys, comparator) | ||
} | ||
@@ -455,3 +467,3 @@ stack.push(value) | ||
if (deterministic) { | ||
keys = insertSort(keys) | ||
keys = sort(keys, comparator) | ||
} | ||
@@ -518,3 +530,4 @@ stack.push(value) | ||
if (Array.isArray(value)) { | ||
const hasLength = value.length !== undefined | ||
if (hasLength && Array.isArray(value)) { | ||
if (value.length === 0) { | ||
@@ -554,3 +567,3 @@ return '[]' | ||
let maximumPropertiesToStringify = Math.min(keyLength, maximumBreadth) | ||
if (isTypedArrayWithEntries(value)) { | ||
if (hasLength && isTypedArrayWithEntries(value)) { | ||
res += stringifyTypedArray(value, ',', maximumBreadth) | ||
@@ -562,3 +575,3 @@ keys = keys.slice(value.length) | ||
if (deterministic) { | ||
keys = insertSort(keys) | ||
keys = sort(keys, comparator) | ||
} | ||
@@ -565,0 +578,0 @@ stack.push(value) |
{ | ||
"name": "safe-stable-stringify", | ||
"version": "2.4.3", | ||
"version": "2.5.0", | ||
"description": "Deterministic and safely JSON.stringify to quickly serialize JavaScript objects", | ||
@@ -5,0 +5,0 @@ "exports": { |
@@ -53,4 +53,5 @@ # safe-stable-stringify | ||
on circular references. **Default:** `'[Circular]'`. | ||
* `deterministic` {boolean} If `true`, guarantee a deterministic key order | ||
instead of relying on the insertion order. **Default:** `true`. | ||
* `deterministic` {boolean|function} If `true` or a `Array#sort(comparator)` | ||
comparator method, guarantee a deterministic key order instead of relying on | ||
the insertion order. **Default:** `true`. | ||
* `maximumBreadth` {number} Maximum number of entries to serialize per object | ||
@@ -67,3 +68,4 @@ (at least one). The serialized output contains information about how many | ||
Circular values and bigint values throw as well in case either option is not | ||
explicitly defined. Sets and Maps are not detected! **Default:** `false` | ||
explicitly defined. Sets and Maps are not detected as well as Symbol keys! | ||
**Default:** `false` | ||
* Returns: {function} A stringify function with the options applied. | ||
@@ -70,0 +72,0 @@ |
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
30687
610
180