Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support array of strings #20

Merged
merged 1 commit into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading