breadth-filter
Advanced tools
Comparing version 1.1.2 to 1.2.0
@@ -16,2 +16,3 @@ const entries = require('object.entries') | ||
const queue = [[ root, target, [] ]] | ||
const seen = new Set() | ||
let item | ||
@@ -25,2 +26,7 @@ | ||
if (newTarget) { | ||
if (!seen.has(value)) { | ||
seen.add(value) | ||
} else { | ||
continue | ||
} | ||
target[key] = newTarget | ||
@@ -27,0 +33,0 @@ queue.push([ value, target[key], fieldPath ]) |
{ | ||
"name": "breadth-filter", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Breadth-first deep object filter", | ||
@@ -5,0 +5,0 @@ "author": "Stephen Belanger <admin@stephenbelanger.com> (https://github.com/qard)", |
31
test.js
@@ -172,1 +172,32 @@ const tap = require('tap') | ||
}) | ||
tap.test('gracefully handle circular references', (t) => { | ||
const input = { | ||
foo: { | ||
bar: { | ||
baz: 'buz' | ||
}, | ||
bux: 'bax' | ||
} | ||
} | ||
// Form a circular reference | ||
input.foo.input = input | ||
breadthFilter(input, reverse, true) | ||
const expected = { | ||
foo: { | ||
bar: { | ||
baz: 'zub' | ||
}, | ||
bux: 'xab' | ||
} | ||
} | ||
// The expectation also needs a circular reference | ||
expected.foo.input = expected | ||
t.deepEqual(input, expected, 'matches expected output') | ||
t.end() | ||
}) |
6413
202