breadth-filter
Advanced tools
Comparing version 1.0.0 to 1.1.0
15
index.js
@@ -1,8 +0,11 @@ | ||
function targetFor (source) { | ||
if (Array.isArray(source)) return [] | ||
if (typeof source === 'object') return {} | ||
function targetFor (source, destructive) { | ||
if (Array.isArray(source)) { | ||
return destructive ? source : [] | ||
} else if (typeof source === 'object') { | ||
return destructive ? source : {} | ||
} | ||
} | ||
module.exports = function breadthFilter (root, fn) { | ||
const target = targetFor(root) | ||
module.exports = function breadthFilter (root, fn, destructive) { | ||
const target = targetFor(root, destructive) | ||
if (!target) return root | ||
@@ -17,3 +20,3 @@ | ||
const fieldPath = path.concat(key) | ||
const newTarget = targetFor(value) | ||
const newTarget = targetFor(value, destructive) | ||
if (newTarget) { | ||
@@ -20,0 +23,0 @@ target[key] = newTarget |
{ | ||
"name": "breadth-filter", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Breadth-first deep object filter", | ||
@@ -5,0 +5,0 @@ "author": "Stephen Belanger <admin@stephenbelanger.com> (https://github.com/qard)", |
27
test.js
@@ -123,3 +123,3 @@ const tap = require('tap') | ||
tap.test('does not mutate', (t) => { | ||
tap.test('does not mutate by default', (t) => { | ||
const input = { | ||
@@ -148,1 +148,26 @@ foo: { | ||
}) | ||
tap.test('mutates in destructive mode', (t) => { | ||
const input = { | ||
foo: { | ||
bar: { | ||
baz: 'buz' | ||
}, | ||
bux: 'bax' | ||
} | ||
} | ||
breadthFilter(input, reverse, true) | ||
const expected = { | ||
foo: { | ||
bar: { | ||
baz: 'zub' | ||
}, | ||
bux: 'xab' | ||
} | ||
} | ||
t.deepEqual(input, expected, 'matches expected output') | ||
t.end() | ||
}) |
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
5438
170