Skip to content

Commit

Permalink
refactor: change spread operator to for loop for node v6 and add test…
Browse files Browse the repository at this point in the history
… case
  • Loading branch information
roggervalf committed Mar 7, 2024
1 parent 7f0d496 commit fca460b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/redactor.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ function redactor ({ secret, serialize, wcLen, strict, isCensorFct, censorFctTak
${strictImpl(strict, serialize)}
}
const { censor, secret } = this
const originalSecret = {...secret}
const originalSecret = {}
const secretKeys = Object.keys(secret)
for (var i = 0; i < secretKeys.length; i++) {
originalSecret[secretKeys[i]] = secret[secretKeys[i]]
}
${redactTmpl(secret, isCensorFct, censorFctTakesPath)}
this.compileRestore()
${dynamicRedactTmpl(wcLen > 0, isCensorFct, censorFctTakesPath)}
this.secret = originalSecret
${resultTmpl(serialize)}
`).bind(state)

redact.secret = secret

if (serialize === false) {
redact.restore = (o) => state.restore(o)
}
Expand Down
11 changes: 11 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ test('masks according to supplied censor function with nested wildcards', ({ end
end()
})

test('does not increment secret size', ({ end, is }) => {
const redact = fastRedact({ paths: ['*.b'], censor: censorFct, serialize: false })
is(redact({ a: { b: '0123456' } }).a.b, 'xxx56')
is(redact.secret[''].length, 1)
is(redact({ c: { b: '0123456', d: 'pristine' } }).c.b, 'xxx56')
is(redact.secret[''].length, 1)
is(redact({ c: { b: '0123456', d: 'pristine' } }).c.d, 'pristine')
is(redact.secret[''].length, 1)
end()
})

test('masks according to supplied censor-with-path function', ({ end, is }) => {
const redact = fastRedact({ paths: ['a'], censor: censorWithPath, serialize: false })
is(redact({ a: '0123456' }).a, 'a xxx56')
Expand Down

0 comments on commit fca460b

Please sign in to comment.