Skip to content

Commit

Permalink
support array of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
novarx authored and rcjpisani committed Aug 4, 2024
1 parent 486ff9f commit eb6c478
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
36 changes: 28 additions & 8 deletions test/redactyl.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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'] };

Expand Down

0 comments on commit eb6c478

Please sign in to comment.