@stoplight/fast-safe-stringify
Advanced tools
Comparing version 2.0.3 to 2.1.0
@@ -62,2 +62,29 @@ const Benchmark = require('benchmark') | ||
suite.add('\ndecycle: simple object', function () { | ||
fastSafeStringify.decycle(obj) | ||
}) | ||
suite.add('decycle: circular ', function () { | ||
fastSafeStringify.decycle(circ) | ||
}) | ||
suite.add('decycle: deep ', function () { | ||
fastSafeStringify.decycle(deep) | ||
}) | ||
suite.add('decycle: deep circular', function () { | ||
fastSafeStringify.decycle(deepCirc) | ||
}) | ||
const replacer = (_val, k) => k | ||
suite.add('\ndecycle with custom replacer: simple object', function () { | ||
fastSafeStringify.decycle(obj, replacer) | ||
}) | ||
suite.add('decycle with custom replacer: circular ', function () { | ||
fastSafeStringify.decycle(circ, replacer) | ||
}) | ||
suite.add('decycle with custom replacer: deep ', function () { | ||
fastSafeStringify.decycle(deep, replacer) | ||
}) | ||
suite.add('decycle with custom replacer: deep circular', function () { | ||
fastSafeStringify.decycle(deepCirc, replacer) | ||
}) | ||
// add listeners | ||
@@ -64,0 +91,0 @@ suite.on('cycle', function (event) { |
@@ -6,5 +6,5 @@ declare function stringify(data: any): string; | ||
export function stableStringify(data: any): string; | ||
export function decirc(val: any, k: string, stack: any[], parent?: any): void; | ||
export function decycle(val: any, replacer?: (val: any, k: string, stack: [any, any][], parent?: any) => any | void): any; | ||
} | ||
export default stringify; |
26
index.js
@@ -5,3 +5,3 @@ module.exports = stringify | ||
stringify.stableStringify = deterministicStringify | ||
stringify.decirc = decirc | ||
stringify.decycle = decycle | ||
@@ -20,17 +20,27 @@ const arr = [] | ||
} | ||
function decirc (val, k, stack, parent) { | ||
function decycle (val, replacer) { | ||
decirc(val, '', [], undefined, replacer) | ||
return val | ||
} | ||
function defaultReplacer (val, parentKey, stack, parent) { | ||
arr.push([parent, parentKey, val]) | ||
return '[Circular]' | ||
} | ||
function decirc (val, parentKey, stack, parent, replacer = defaultReplacer) { | ||
var i | ||
if (typeof val === 'object' && val !== null) { | ||
for (i = 0; i < stack.length; i++) { | ||
if (stack[i] === val) { | ||
parent[k] = '[Circular]' | ||
arr.push([parent, k, val]) | ||
if (stack[i][0] === val) { | ||
parent[parentKey] = replacer(val, parentKey, stack, parent) | ||
return | ||
} | ||
} | ||
stack.push(val) | ||
stack.push([val, parentKey]) | ||
// Optimize for Arrays. Big arrays could kill the performance otherwise! | ||
if (Array.isArray(val)) { | ||
for (i = 0; i < val.length; i++) { | ||
decirc(val[i], i, stack, val) | ||
decirc(val[i], i, stack, val, replacer) | ||
} | ||
@@ -41,3 +51,3 @@ } else { | ||
var key = keys[i] | ||
decirc(val[key], key, stack, val) | ||
decirc(val[key], key, stack, val, replacer) | ||
} | ||
@@ -44,0 +54,0 @@ } |
{ | ||
"name": "@stoplight/fast-safe-stringify", | ||
"version": "2.0.3", | ||
"version": "2.1.0", | ||
"description": "Safely and quickly serialize JavaScript objects", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
52
test.js
@@ -6,2 +6,54 @@ const test = require('tap').test | ||
// this custom replacer will add a json pointer $ref pointing to the referenced js object | ||
test('decycle supports custom replacer', function (assert) { | ||
const fixture = { | ||
name: 'Tywin Lannister', | ||
house2: { | ||
inner: {} | ||
}, | ||
house: { | ||
name: 'Lannister' | ||
} | ||
} | ||
fixture.house2.inner = fixture.house | ||
fixture.house.circle = fixture.house2.inner | ||
const expected = { | ||
name: 'Tywin Lannister', | ||
house2: { | ||
inner: { | ||
name: 'Lannister', | ||
circle: { | ||
key: 'circle', | ||
stackSize: 3, | ||
parentName: 'Lannister', | ||
$ref: '#/house2/inner' | ||
} | ||
} | ||
}, | ||
house: { | ||
name: 'Lannister', | ||
circle: { | ||
key: 'circle', | ||
stackSize: 3, | ||
parentName: 'Lannister', | ||
$ref: '#/house2/inner' | ||
} | ||
} | ||
} | ||
const actual = fss.decycle(fixture, (val, k, stack, parent) => { | ||
return { | ||
key: k, | ||
stackSize: stack.length, | ||
parentName: parent.name, | ||
$ref: `#${stack.map((s) => s[1]).join('/')}` | ||
} | ||
}) | ||
assert.deepEqual(actual, expected) | ||
assert.end() | ||
}) | ||
test('circular reference to root', function (assert) { | ||
@@ -8,0 +60,0 @@ const fixture = { name: 'Tywin Lannister' } |
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
28813
683