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

Allow replacement to be a function #20

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ npm install mask-json
Option | Default value | Description
------------- | -------------- | -----------------------------------------------------
_ignoreCase_ | false | Whether to ignore case sensitivity when matching keys
_replacement_ | _--REDACTED--_ | The default value to replace
_replacement_ | _--REDACTED--_ | The default value to replace, this can be either an explicit value or a function using current value as only parameter

### Returns

Expand All @@ -41,15 +41,31 @@ maskJson({ foo: 'bar', biz: { username: 'myusername', password: 'mypassword' } }
// => { foo: 'bar', biz: { username: '--REDACTED--', password: '--REDACTED--' } }
```

## Tests
Works the same when "replacement" is not a static string, but a function to convert
the original value somehow, e.g. mask some parts of a credit card number or phone number
like the original "1234-5678-9012" shall become "1234-****-9012".

The replacement function is called with one parameter - the original value to replace.

```javascript
// with replacement function
var replaceFunc = function(value) { return value.toUpperCase() };
var maskJson2 = require('mask-json')(blacklist, {replacement: replaceFunc});

maskJson2({ foo: 'bar', biz: { username: 'myusername', password: 'mypassword' } });

// => { foo: 'bar', biz: { username: 'MYUSERNAME', password: 'MYPASSWORD' } }
```

## Tests

```shell script
$ npm test
```

## Release

```sh
```shell script
npm version [<new version> | major | minor | patch] -m "Release %s"
```

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"version": "npm run changelog && git add -A CHANGELOG.md"
},
"dependencies": {
"lodash": "^4.17.4"
"lodash": "^4.17.21"
},
"devDependencies": {
"@uphold/github-changelog-generator": "^0.8.1",
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module.exports = function maskJson(collection, {
return cloneDeepWith(values, (value, key) => {
// Strip matching keys.
if (some(collection, item => ignoreCase ? toLower(key) === toLower(item) : key === item)) {
return replacement;
return (typeof replacement === 'function') ? replacement(value) : replacement;
}

// Allow cloneDeep to recurse into nested objects.
Expand Down
22 changes: 21 additions & 1 deletion test/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('maskJson()', () => {
});
});

it('should accept a custom `replacement`', () => {
it('should accept a custom `replacement` value', () => {
const object = {
foo: {
password: 'foobar',
Expand All @@ -46,6 +46,26 @@ describe('maskJson()', () => {
});
});

it('should accept a custom `replacement` function', () => {
const object = {
foo: {
password: 'foobar',
secret: 'bizbaz'
}
};

function replacementFunc(value) {
return value.toUpperCase();
}

expect(maskJson(['password', 'secret'], { replacement: replacementFunc })(object)).toEqual({
foo: {
password: 'FOOBAR',
secret: 'BIZBAZ'
}
});
});

it('should mask non-plain objects', () => {
const object = {
bar: {
Expand Down