Skip to content

Commit

Permalink
Merge pull request #40 from lrecknagel/multi-wildcard-support-finaliz…
Browse files Browse the repository at this point in the history
…ation

Multi wildcard support finalization
  • Loading branch information
davidmarkclements authored Jan 20, 2022
2 parents d162e4a + c906c98 commit e231e2a
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 30 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.nyc_output
package-lock.json
coverage
coverage
.idea
16 changes: 16 additions & 0 deletions benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const redactIntermediateWildCensorFunction = fastRedact({ paths: ['as.*.c'], cen
const redactCensorFunctionWithPath = fastRedact({ paths: ['at.d.b'], censor: censorFn, serialize: false })
const redactWildCensorFunctionWithPath = fastRedact({ paths: ['au.d.*'], censor: censorFnWithPath, serialize: false })
const redactIntermediateWildCensorFunctionWithPath = fastRedact({ paths: ['av.*.c'], censorFnWithPath, serialize: false })
const redactMultiWild = fastRedact({ paths: ['aw.*.*'] })
const redactMultiWildCensorFunction = fastRedact({ paths: ['ax.*.*'], censor: censorFn, serialize: false })

const getObj = (outerKey) => ({
[outerKey]: {
Expand Down Expand Up @@ -201,6 +203,20 @@ var run = bench([
redactIntermediateWildCensorFunctionWithPath(obj)
}
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)
}
], 500)

Expand Down
11 changes: 11 additions & 0 deletions example/multi-wildcard-array-depth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'
const fastRedact = require('..')
const redact = fastRedact({ paths: ['a[*].c.d[*].i'] })
const obj = {
a: [
{ c: { d: [ { i: 'redact me', j: 'not me' } ], e: 'leave me be' } },
{ c: { d: [ { i: 'redact me too', j: 'not me' }, { i: 'redact me too', j: 'not me' } ], f: 'I want to live' } },
{ c: { d: [ { i: 'redact me 3', j: 'not me' } ], g: 'I want to run in a stream' } }
]
}
console.log(redact(obj))
11 changes: 11 additions & 0 deletions example/multi-wildcard-array-end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'
const fastRedact = require('..')
const redact = fastRedact({ paths: ['a[*].c.d[*]'] })
const obj = {
a: [
{ c: { d: ['hide me', '2'], e: 'leave me be' } },
{ c: { d: ['and me'], f: 'I want to live' } },
{ c: { d: ['and also I'], g: 'I want to run in a stream' } }
]
}
console.log(redact(obj))
11 changes: 11 additions & 0 deletions example/multi-wildcard-array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict'
const fastRedact = require('..')
const redact = fastRedact({ paths: ['a[*].c[*].d'] })
const obj = {
a: [
{ c: [{ d: 'hide me', e: 'leave me be' }, { d: 'hide me too', e: 'leave me be' }, { d: 'hide me 3', e: 'leave me be' }] },
{ c: [{ d: 'and me', f: 'I want to live' }] },
{ c: [{ d: 'and also I', g: 'I want to run in a stream' }] }
]
}
console.log(redact(obj))
71 changes: 60 additions & 11 deletions lib/modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,20 @@ function nestedRestore (arr) {
const length = arr.length
for (var i = 0; i < length; i++) {
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
}
}
}
}
}

Expand All @@ -67,7 +80,9 @@ function nestedRedact (store, o, path, ns, censor, isCensorFct, censorFctTakesPa
}

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
}

function specialSet (o, k, path, afterPath, censor, isCensorFct, censorFctTakesPath) {
Expand All @@ -80,23 +95,57 @@ function specialSet (o, k, path, afterPath, censor, isCensorFct, censorFctTakesP
var ov
var oov = null
var exists = true
var wc = null
ov = n = o[k]
if (typeof n !== 'object') return { value: null, parent: null, exists }
while (n != null && ++i < afterPathLen) {
k = afterPath[i]
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
}
return { value: ov, parent: oov, exists }
Expand Down
1 change: 0 additions & 1 deletion lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ function parse ({ paths }) {
const before = path.slice(0, star)
const beforeStr = before.join('.')
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
wcLen++
wildcards.push({
Expand Down
Loading

0 comments on commit e231e2a

Please sign in to comment.