redact-object
Advanced tools
Comparing version 2.0.0 to 2.1.0
44
index.js
'use strict'; | ||
const some = require('lodash/some'); | ||
const isArray = require('lodash/isArray'); | ||
const isPlainObject = require('lodash/isPlainObject'); | ||
const isObject = require('lodash/isObject'); | ||
const isPlainObject = require('lodash.isplainobject'); | ||
// Yoinked from lodash to save dependencies | ||
function isObject (value) { | ||
const type = typeof value; | ||
return value != null && (type === 'object' || type === 'function'); | ||
} | ||
/** | ||
@@ -17,7 +20,9 @@ * Checks for match | ||
function isKeywordMatch (keywords, key, strict, partial) { | ||
return some(keywords, (keyword) => { | ||
return keywords.some(keyword => { | ||
const keyMatch = strict ? key : key.toLowerCase(); | ||
const keywordMatch = strict ? keyword : keyword.toLowerCase(); | ||
return partial ? (keyMatch.indexOf(keywordMatch) !== -1) : (keyMatch === keywordMatch); | ||
return partial | ||
? keyMatch.indexOf(keywordMatch) !== -1 | ||
: keyMatch === keywordMatch; | ||
}); | ||
@@ -29,11 +34,11 @@ } | ||
* | ||
* @param {object} target The target object to scan for redactable items | ||
* @param {string[]} keywords A list of members to redact | ||
* @param {string} replaceVal Optional custom replace value | ||
* @param {object} config Optional config | ||
* { | ||
* partial: boolean, do partial matches, default false | ||
* strict: boolean, do strict key matching, default true | ||
* } | ||
* @return {object} the new redacted object | ||
* @param {object} target The target object to scan for redactable items | ||
* @param {string[]} keywords A list of members to redact | ||
* @param {string|function} replaceVal Optional custom replace value or function replacer | ||
* @param {object} config Optional config | ||
* { | ||
* partial: boolean, do partial matches, default false | ||
* strict: boolean, do strict key matching, default true | ||
* } | ||
* @return {object} the new redacted object | ||
*/ | ||
@@ -48,6 +53,6 @@ function redact (target, keywords, replaceVal, config) { | ||
return target; | ||
} else if (isArray(target)) { | ||
} else if (Array.isArray(target)) { | ||
// Create a new array with each value having been redacted | ||
// Redact each value of the array. | ||
return target.map((val) => redact(val, keywords, replaceVal, config)); | ||
return target.map(val => redact(val, keywords, replaceVal, config)); | ||
} else if (isPlainObject(target)) { | ||
@@ -57,3 +62,6 @@ return Object.keys(target).reduce((newObj, key) => { | ||
if (isMatch) { | ||
newObj[key] = replaceVal || '[ REDACTED ]'; | ||
newObj[key] = | ||
typeof replaceVal === 'function' | ||
? replaceVal(target[key], key) | ||
: replaceVal || '[ REDACTED ]'; | ||
} else { | ||
@@ -60,0 +68,0 @@ newObj[key] = redact(target[key], keywords, replaceVal, config); |
{ | ||
"name": "redact-object", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Object redactor", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
@@ -28,11 +31,11 @@ "test": "npm run lint && npm run unit", | ||
"devDependencies": { | ||
"coveralls": "^2.11.12", | ||
"coveralls": "^3.0.1", | ||
"jasmine-node": "^1.14.5", | ||
"nyc": "^8.1.0", | ||
"semistandard": "^8.0.0", | ||
"snazzy": "^4.0.1" | ||
"nyc": "^12.0.2", | ||
"semistandard": "^12.0.1", | ||
"snazzy": "^7.1.1" | ||
}, | ||
"homepage": "https://github.com/shaunburdick/redact-object", | ||
"dependencies": { | ||
"lodash": "^4.15.0" | ||
"lodash.isplainobject": "^4.0.6" | ||
}, | ||
@@ -39,0 +42,0 @@ "semistandard": { |
@@ -8,5 +8,6 @@ # Redact Object | ||
## Usage | ||
Pass in an object and an array of members you want to redact. It will recursively travel the object redacting any matching members. | ||
``` | ||
```javascript | ||
const redact = require('redact-object'); | ||
@@ -37,3 +38,3 @@ | ||
* // it can do arrays too | ||
* { foo: 'bar' }, | ||
* { foo: '[ REDACTED ]' }, | ||
* 22 | ||
@@ -43,14 +44,45 @@ * ] | ||
*/ | ||
console.dir(redact(obj), ['foo']); | ||
console.dir(redact(obj, ['foo'])); | ||
``` | ||
## Arguments | ||
- {object} target The target object to scan for redactable items | ||
- {string[]} keywords A list of members to redact | ||
- {string} replaceVal Optional custom replace value | ||
- {object} config Option object of config settings: | ||
- {object} `target` The target object to scan for redactable items | ||
- {string[]} `keywords` A list of members to redact | ||
- {string|Function} `replaceVal` Optional custom replace value, or function that returns replace value. Default value is **[ REDACTED ]** | ||
- {object} `config` Option object of config settings: | ||
- partial: boolean, will do partial matching if true, Default _true_ | ||
- strict: boolean, will do strict comparison (case insensitive) if true, Default _true_ | ||
- strict: boolean, will do strict comparison (case sensitive) if true, Default _true_ | ||
### Replace Function | ||
`replaceVal` can be a function. This function will get two arguments `(value, key)`: | ||
- `value`: The value of the object | ||
- `key`: The matched key | ||
Your function's return will replace the value on the new object | ||
#### Example | ||
```javascript | ||
const obj = { | ||
firstname: 'Han', | ||
lastname: 'Solo', | ||
}; | ||
const replacer = val => `[ REDACTED (${val.length}) ]`; | ||
const redacted = redact(obj, ['firstname', 'lastname'], replacer); | ||
/** | ||
* Results in: | ||
* { | ||
* firstname: '[ REDACTED (3) ]', | ||
* lastname: '[ REDACTED (4) ]', | ||
* }; | ||
*/ | ||
``` | ||
## Contributing | ||
1. Create a new branch, please don't work in master directly. | ||
@@ -57,0 +89,0 @@ 2. Add failing tests for the change you want to make. Run `npm test` to see the tests fail. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
91
0
6772
3
66
+ Addedlodash.isplainobject@^4.0.6
+ Addedlodash.isplainobject@4.0.6(transitive)
- Removedlodash@^4.15.0
- Removedlodash@4.17.21(transitive)