From d0ce3cca67be41a5532054004ef39945d92bf5de Mon Sep 17 00:00:00 2001 From: novarx Date: Tue, 6 Feb 2024 07:32:49 +0100 Subject: [PATCH] support array of strings --- src/index.js | 6 +++--- test/redactyl.spec.js | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index ac4555c..5c01188 100644 --- a/src/index.js +++ b/src/index.js @@ -51,13 +51,13 @@ class Redactyl { } redact(json) { - let isObject = this.isObject(json); + const isObject = this.isObject(json); if (!isObject && !Array.isArray(json)) { - throw new TypeError('A valid JSON object must be specified'); + return json; } - let redacted = JSON.parse(JSON.stringify(json, this.replacer)); + const redacted = JSON.parse(JSON.stringify(json, this.replacer)); for (let prop in redacted) { if (this.properties.includes(prop)) { diff --git a/test/redactyl.spec.js b/test/redactyl.spec.js index 97eb67e..a2d8bd6 100644 --- a/test/redactyl.spec.js +++ b/test/redactyl.spec.js @@ -85,15 +85,9 @@ describe('Redactyl test suite', function () { let properties = [ 'apiKey', 'password', 'phone' ]; let redactyl = new Redactyl({ 'properties': properties }); - let error = null; - try { - redactyl.redact('invalid'); - } catch (err) { - error = err; - } + const redacted = redactyl.redact('any string wLYm0Vthq'); - expect(error).to.not.equal(null); - expect(error.constructor.name).to.equal('TypeError'); + expect(redacted).to.equal('any string wLYm0Vthq'); }); it('Should redact shallow JSON', async function () { @@ -246,6 +240,32 @@ describe('Redactyl test suite', function () { }); }); + it('Should redact array of strings', async function () { + const properties = [ 'sensitiveData' ]; + const redactyl = new Redactyl({ 'properties': properties }); + + const json = { + sensitiveData: [ + 'RxcKQr3', + 'jvicULO' + ], + nonSensitive: [ + 'J2AZps8w', + 'YVTr9O1' + ] + }; + + const redacted = redactyl.redact(json); + + expect(redacted).to.deep.equal({ + sensitiveData: DEFAULT_TEXT, + nonSensitive: [ + 'J2AZps8w', + 'YVTr9O1' + ] + }); + }); + it('Should fail to set a custom replacer function, function not passed as options', async function () { const options = { 'replacer': ['not', 'a', 'function'] };