Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make decirc function public #34

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,33 @@ suite.add('fast-safe-stringify: deep circular', function () {
fastSafeStringify(deepCirc)
})

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
suite.on('cycle', function (event) {
console.log(String(event.target))
Expand Down
3 changes: 2 additions & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ declare function stringify(data: any): string;
declare namespace stringify {
export function stable(data: any): string;
export function stableStringify(data: any): string;
export function decycle(val: any, replacer?: (val: any, k: string, stack: [any, any][], parent?: any) => any | void): any;
}

export default stringify;
export default stringify;
27 changes: 20 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module.exports = stringify
stringify.default = stringify
stringify.stable = deterministicStringify
stringify.stableStringify = deterministicStringify
stringify.decycle = decycle

const arr = []

Expand All @@ -15,27 +16,39 @@ function stringify (obj, replacer, spacer) {
}
return res
}
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) {
replacer = 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)
}
} else {
const keys = Object.keys(val)
for (i = 0; i < keys.length; i++) {
var key = keys[i]
decirc(val[key], key, stack, val)
decirc(val[key], key, stack, val, replacer)
}
}
stack.pop()
Expand Down
75 changes: 75 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,78 @@ test('nested child circular reference in toJSON', function (assert) {
assert.is(actual, expected)
assert.end()
})

// this custom replacer will add a json pointer $ref pointing to the referenced js object
test('decycle supports custom replacer', function (assert) {
const fixture = {
definitions: {
Customer: {
properties: {
partners: {
items: {}
}
}
},
Partner: {
properties: {
customers: {
items: {}
}
}
}
}
}

fixture.definitions.Customer.properties.partners.items = fixture.definitions.Partner
fixture.definitions.Partner.properties.customers.items = fixture.definitions.Customer

const expected = {
definitions: {
Customer: {
properties: {
partners: {
items: {
properties: {
customers: {
items: {
$ref: '#/definitions/Customer'
}
}
}
}
}
}
},
Partner: {
properties: {
customers: {
items: {
$ref: '#/definitions/Customer'
}
}
}
}
}
}

const actual = fss.decycle(fixture, (val, k, stack, parent) => {
let $ref = '#'

for (let i = 0; i < stack.length; i++) {
if (stack[i][1]) {
$ref += `/${stack[i][1]}`
}

if (stack[i][0] === val) {
break
}
}

return {
$ref
}
})

assert.deepEqual(actual, expected)
assert.end()
})