From fca460bdc3fff648bd0cdd113027e70ba0a0d001 Mon Sep 17 00:00:00 2001 From: roggervalf Date: Thu, 7 Mar 2024 09:12:25 -0500 Subject: [PATCH] refactor: change spread operator to for loop for node v6 and add test case --- lib/redactor.js | 9 ++++++++- test/index.js | 11 +++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/redactor.js b/lib/redactor.js index 1a4201a..06a8887 100644 --- a/lib/redactor.js +++ b/lib/redactor.js @@ -11,7 +11,12 @@ 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)} @@ -19,6 +24,8 @@ function redactor ({ secret, serialize, wcLen, strict, isCensorFct, censorFctTak ${resultTmpl(serialize)} `).bind(state) + redact.secret = secret + if (serialize === false) { redact.restore = (o) => state.restore(o) } diff --git a/test/index.js b/test/index.js index ac2d391..47f4180 100644 --- a/test/index.js +++ b/test/index.js @@ -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')