fast-redact
Advanced tools
Comparing version 3.0.2 to 3.1.0
@@ -29,2 +29,4 @@ 'use strict' | ||
const redactIntermediateWildCensorFunctionWithPath = fastRedact({ paths: ['av.*.c'], censorFnWithPath, serialize: false }) | ||
const redactMultiWild = fastRedact({ paths: ['aw.*.*'] }) | ||
const redactMultiWildCensorFunction = fastRedact({ paths: ['ax.*.*'], censor: censorFn, serialize: false }) | ||
@@ -205,2 +207,16 @@ const getObj = (outerKey) => ({ | ||
setImmediate(cb) | ||
}, | ||
function benchFastRedactMultiWild (cb) { | ||
const obj = getObj('aw') | ||
for (var i = 0; i < max; i++) { | ||
redactMultiWild(obj) | ||
} | ||
setImmediate(cb) | ||
}, | ||
function benchFastRedactMultiWildCensorFunction (cb) { | ||
const obj = getObj('ax') | ||
for (var i = 0; i < max; i++) { | ||
redactMultiWildCensorFunction(obj) | ||
} | ||
setImmediate(cb) | ||
} | ||
@@ -207,0 +223,0 @@ ], 500) |
@@ -48,3 +48,16 @@ 'use strict' | ||
const { key, target, value } = arr[i] | ||
target[key] = value | ||
if (has(target, key)) { | ||
target[key] = value | ||
} | ||
/* istanbul ignore else */ | ||
if (typeof target === 'object') { | ||
const targetKeys = Object.keys(target) | ||
for (var j = 0; j < targetKeys.length; j++) { | ||
const tKey = targetKeys[j] | ||
const subTarget = target[tKey] | ||
if (has(subTarget, key)) { | ||
subTarget[key] = value | ||
} | ||
} | ||
} | ||
} | ||
@@ -71,3 +84,5 @@ } | ||
function has (obj, prop) { | ||
return Object.prototype.hasOwnProperty.call(obj, prop) | ||
return obj !== undefined && obj !== null | ||
? ('hasOwn' in Object ? Object.hasOwn(obj, prop) : Object.prototype.hasOwnProperty.call(obj, prop)) | ||
: false | ||
} | ||
@@ -85,2 +100,3 @@ | ||
var exists = true | ||
var wc = null | ||
ov = n = o[k] | ||
@@ -91,14 +107,47 @@ if (typeof n !== 'object') return { value: null, parent: null, exists } | ||
oov = ov | ||
if (!(k in n)) { | ||
if (k !== '*' && !wc && !(typeof n === 'object' && k in n)) { | ||
exists = false | ||
break | ||
} | ||
ov = n[k] | ||
nv = (i !== lastPathIndex) | ||
? ov | ||
: (isCensorFct | ||
? (censorFctTakesPath ? censor(ov, [...path, originalKey, ...afterPath]) : censor(ov)) | ||
: censor) | ||
n[k] = (has(n, k) && nv === ov) || (nv === undefined && censor !== undefined) ? n[k] : nv | ||
n = n[k] | ||
if (k === '*') { | ||
wc = k | ||
if (i !== lastPathIndex) { | ||
continue | ||
} | ||
} | ||
if (wc) { | ||
const wcKeys = Object.keys(n) | ||
for (var j = 0; j < wcKeys.length; j++) { | ||
const wck = wcKeys[j] | ||
const wcov = n[wck] | ||
const kIsWc = k === '*' | ||
if (kIsWc || (typeof wcov === 'object' && k in wcov)) { | ||
if (kIsWc) { | ||
ov = wcov | ||
} else { | ||
ov = wcov[k] | ||
} | ||
nv = (i !== lastPathIndex) | ||
? ov | ||
: (isCensorFct | ||
? (censorFctTakesPath ? censor(ov, [...path, originalKey, ...afterPath]) : censor(ov)) | ||
: censor) | ||
if (kIsWc) { | ||
n[wck] = nv | ||
} else { | ||
wcov[k] = (nv === undefined && censor !== undefined) || (has(wcov, k) && nv === ov) ? wcov[k] : nv | ||
} | ||
} | ||
} | ||
wc = null | ||
} else { | ||
ov = n[k] | ||
nv = (i !== lastPathIndex) | ||
? ov | ||
: (isCensorFct | ||
? (censorFctTakesPath ? censor(ov, [...path, originalKey, ...afterPath]) : censor(ov)) | ||
: censor) | ||
n[k] = (has(n, k) && nv === ov) || (nv === undefined && censor !== undefined) ? n[k] : nv | ||
n = n[k] | ||
} | ||
if (typeof n !== 'object') break | ||
@@ -105,0 +154,0 @@ } |
@@ -22,3 +22,2 @@ 'use strict' | ||
const after = path.slice(star + 1, path.length) | ||
if (after.indexOf('*') > -1) throw Error('fast-redact – Only one wildcard per path is supported') | ||
const nested = after.length > 0 | ||
@@ -25,0 +24,0 @@ wcLen++ |
{ | ||
"name": "fast-redact", | ||
"version": "3.0.2", | ||
"version": "3.1.0", | ||
"description": "very fast object redaction", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -210,9 +210,2 @@ 'use strict' | ||
test('throws if more than one wildcard in a path', ({ end, throws }) => { | ||
throws(() => { | ||
fastRedact({ paths: ['a.*.x.*'], serialize: false }) | ||
}, Error('fast-redact – Only one wildcard per path is supported')) | ||
end() | ||
}) | ||
test('throws if a custom serializer is used and remove is true', ({ end, throws }) => { | ||
@@ -640,3 +633,3 @@ throws(() => { | ||
test('ultimate wildcards – handles circulars – restore', ({ end, is, same }) => { | ||
test('ultimate wildcards – handles circulars – restore', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['bar.baz.*'], serialize: false }) | ||
@@ -655,3 +648,17 @@ const bar = { b: 2 } | ||
test('ultimate wildcards – handles circulars and cross references – restore', ({ end, is, same }) => { | ||
test('ultimate multi wildcards – handles circulars – restore', ({ end, is, same }) => { | ||
const redact = fastRedact({ paths: ['bar.*.baz.*.b'], serialize: false }) | ||
const bar = { b: 2 } | ||
const o = { a: 1, bar } | ||
bar.baz = bar | ||
o.bar.baz = o.bar | ||
is(o.bar.baz, bar) | ||
redact(o) | ||
is(o.bar.baz.b, censor) | ||
redact.restore(o) | ||
same(o.bar.baz, bar) | ||
end() | ||
}) | ||
test('ultimate wildcards – handles circulars and cross references – restore', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['bar.baz.*', 'cf.*'], serialize: false }) | ||
@@ -764,2 +771,17 @@ const bar = { b: 2 } | ||
test('multi object wildcard', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['a.*.x.*.c'], serialize: false }) | ||
const result = redact({ a: { b: { x: { z: { c: 's' } } }, d: { x: { u: { a: 's', b: 's', c: 's' } } } } }) | ||
is(result.a.b.x.z.c, censor) | ||
is(result.a.d.x.u.a, 's') | ||
is(result.a.d.x.u.b, 's') | ||
is(result.a.d.x.u.c, censor) | ||
redact.restore(result) | ||
is(result.a.b.x.z.c, 's') | ||
is(result.a.d.x.u.a, 's') | ||
is(result.a.d.x.u.b, 's') | ||
is(result.a.d.x.u.c, 's') | ||
end() | ||
}) | ||
test('parent wildcard – two following keys – reuse', ({ end, is }) => { | ||
@@ -802,2 +824,102 @@ const redact = fastRedact({ paths: ['a.*.x.c'], serialize: false }) | ||
test('multiple wildcards', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['a[*].c[*].d'], serialize: false }) | ||
const obj = { | ||
a: [ | ||
{ c: [{ d: '1', e: '2' }, { d: '1', e: '3' }, { d: '1', e: '4' }] }, | ||
{ c: [{ d: '1', f: '5' }] }, | ||
{ c: [{ d: '2', g: '6' }] } | ||
] | ||
} | ||
const result = redact(obj) | ||
is(result.a[0].c[0].d, censor) | ||
is(result.a[0].c[0].e, '2') | ||
is(result.a[0].c[1].d, censor) | ||
is(result.a[0].c[1].e, '3') | ||
is(result.a[0].c[2].d, censor) | ||
is(result.a[0].c[2].e, '4') | ||
is(result.a[1].c[0].d, censor) | ||
is(result.a[1].c[0].f, '5') | ||
is(result.a[2].c[0].d, censor) | ||
is(result.a[2].c[0].g, '6') | ||
redact.restore(result) | ||
is(result.a[0].c[0].d, '1') | ||
is(result.a[0].c[0].e, '2') | ||
is(result.a[0].c[1].d, '1') | ||
is(result.a[0].c[1].e, '3') | ||
is(result.a[0].c[2].d, '1') | ||
is(result.a[0].c[2].e, '4') | ||
is(result.a[1].c[0].d, '1') | ||
is(result.a[1].c[0].f, '5') | ||
is(result.a[2].c[0].d, '2') | ||
is(result.a[2].c[0].g, '6') | ||
end() | ||
}) | ||
test('multiple wildcards - censor function', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['a[*].c[*].d'], censor: censorFct, serialize: false }) | ||
const obj = { | ||
a: [ | ||
{ c: [{ d: '1', e: '2' }, { d: '1', e: '3' }, { d: '1', e: '4' }] }, | ||
{ c: [{ d: '1', f: '5' }] }, | ||
{ c: [{ d: '2', g: '6' }] } | ||
] | ||
} | ||
const result = redact(obj) | ||
is(result.a[0].c[0].d, 'xxx1') | ||
is(result.a[0].c[0].e, '2') | ||
is(result.a[0].c[1].d, 'xxx1') | ||
is(result.a[0].c[1].e, '3') | ||
is(result.a[0].c[2].d, 'xxx1') | ||
is(result.a[0].c[2].e, '4') | ||
is(result.a[1].c[0].d, 'xxx1') | ||
is(result.a[1].c[0].f, '5') | ||
is(result.a[2].c[0].d, 'xxx2') | ||
is(result.a[2].c[0].g, '6') | ||
end() | ||
}) | ||
test('multiple wildcards end', ({ end, is, same }) => { | ||
const redact = fastRedact({ paths: ['a[*].c.d[*]'], serialize: false }) | ||
const obj = { | ||
a: [ | ||
{ c: { d: [ '1', '2' ], e: '3' } }, | ||
{ c: { d: [ '1' ], f: '4' } }, | ||
{ c: { d: [ '1' ], g: '5' } } | ||
] | ||
} | ||
const result = redact(obj) | ||
same(result.a[0].c.d, [censor, censor]) | ||
is(result.a[0].c.e, '3') | ||
same(result.a[1].c.d, [censor]) | ||
is(result.a[1].c.f, '4') | ||
same(result.a[2].c.d, [censor]) | ||
is(result.a[2].c.g, '5') | ||
end() | ||
}) | ||
test('multiple wildcards depth after n wildcard', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['a[*].c.d[*].i'], serialize: false }) | ||
const obj = { | ||
a: [ | ||
{ c: { d: [ { i: '1', j: '2' } ], e: '3' } }, | ||
{ c: { d: [ { i: '1', j: '2' }, { i: '1', j: '3' } ], f: '4' } }, | ||
{ c: { d: [ { i: '1', j: '2' } ], g: '5' } } | ||
] | ||
} | ||
const result = redact(obj) | ||
is(result.a[0].c.d[0].i, censor) | ||
is(result.a[0].c.d[0].j, '2') | ||
is(result.a[0].c.e, '3') | ||
is(result.a[1].c.d[0].i, censor) | ||
is(result.a[1].c.d[0].j, '2') | ||
is(result.a[1].c.d[1].i, censor) | ||
is(result.a[1].c.d[1].j, '3') | ||
is(result.a[1].c.f, '4') | ||
is(result.a[2].c.d[0].i, censor) | ||
is(result.a[2].c.d[0].j, '2') | ||
is(result.a[2].c.g, '5') | ||
end() | ||
}) | ||
test('parent wildcards – array – single index', ({ end, same }) => { | ||
@@ -834,3 +956,3 @@ const redact = fastRedact({ paths: ['insideArray.like[3].*.foo'], serialize: false }) | ||
test('parent wildcards – handles circulars', ({ end, is, same }) => { | ||
test('parent wildcards – handles circulars', ({ end, same }) => { | ||
const redact = fastRedact({ paths: ['x.*.baz'], serialize: false }) | ||
@@ -845,3 +967,3 @@ const bar = { b: 2 } | ||
test('parent wildcards – handles circulars – restore', ({ end, is, same }) => { | ||
test('parent wildcards – handles circulars – restore', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['x.*.baz'], serialize: false }) | ||
@@ -861,3 +983,3 @@ const bar = { b: 2 } | ||
test('parent wildcards – handles circulars and cross references – restore', ({ end, is, same }) => { | ||
test('parent wildcards – handles circulars and cross references – restore', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['x.*.baz', 'x.*.cf.bar'], serialize: false }) | ||
@@ -879,3 +1001,3 @@ const bar = { b: 2 } | ||
test('parent wildcards – handles missing paths', ({ end, is, same }) => { | ||
test('parent wildcards – handles missing paths', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['z.*.baz'] }) | ||
@@ -887,3 +1009,3 @@ const o = { a: { b: { c: 's' }, d: { a: 's', b: 's', c: 's' } } } | ||
test('ultimate wildcards – handles missing paths', ({ end, is, same }) => { | ||
test('ultimate wildcards – handles missing paths', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['z.*'] }) | ||
@@ -937,3 +1059,3 @@ const o = { a: { b: { c: 's' }, d: { a: 's', b: 's', c: 's' } } } | ||
test('(leading brackets) ultimate wildcards – handles circulars and cross references – restore', ({ end, is, same }) => { | ||
test('(leading brackets) ultimate wildcards – handles circulars and cross references – restore', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['bar.baz.*', 'cf.*'], serialize: false }) | ||
@@ -955,3 +1077,3 @@ const bar = { b: 2 } | ||
test('(leading brackets) parent wildcards – handles circulars and cross references – restore', ({ end, is, same }) => { | ||
test('(leading brackets) parent wildcards – handles circulars and cross references – restore', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['["x"].*.baz', '["x"].*.cf.bar'], serialize: false }) | ||
@@ -973,3 +1095,3 @@ const bar = { b: 2 } | ||
test('(leading brackets) ultimate wildcards – handles missing paths', ({ end, is, same }) => { | ||
test('(leading brackets) ultimate wildcards – handles missing paths', ({ end, is }) => { | ||
const redact = fastRedact({ paths: ['["z"].*'] }) | ||
@@ -1093,1 +1215,39 @@ const o = { a: { b: { c: 's' }, d: { a: 's', b: 's', c: 's' } } } | ||
}) | ||
test('handles multi wildcards within arrays', ({ end, is }) => { | ||
const redact = fastRedact({ | ||
paths: ['a[*].x.d[*].i.*'] | ||
}) | ||
const o = { | ||
a: [ { x: { d: [ { j: { i: 'R' } }, { i: 'R', j: 'NR' } ] } } ] | ||
} | ||
is(redact(o), '{"a":[{"x":{"d":["[REDACTED]","[REDACTED]"]}}]}') | ||
end() | ||
}) | ||
test('handles multi wildcards within arrays with a censorFct', ({ end, is }) => { | ||
const redact = fastRedact({ | ||
paths: ['a[*].x.d[*].i.*.i'], | ||
censor: censorWithPath | ||
}) | ||
const o = { | ||
a: [ | ||
{ x: { d: [ { i: 'R', j: 'NR' } ] } } | ||
] | ||
} | ||
is(redact(o), '{"a":[{"x":{"d":[{"i":"a.0.x.d.*.i.*.i xxxR","j":"NR"}]}}]}') | ||
end() | ||
}) | ||
test('handles multi wildcards within arrays with undefined values', ({ end, is }) => { | ||
const redact = fastRedact({ | ||
paths: ['a[*].x.d[*].i.*.i'] | ||
}) | ||
const o = { | ||
a: [ | ||
{ x: { d: [ { i: undefined, j: 'NR' } ] } } | ||
] | ||
} | ||
is(redact(o), '{"a":[{"x":{"d":[{"i":"[REDACTED]","j":"NR"}]}}]}') | ||
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
80046
22
1875