redact-object
Advanced tools
Comparing version 1.0.1 to 2.0.0
62
index.js
'use strict'; | ||
const extend = require('util')._extend; // yoink | ||
const some = require('lodash/some'); | ||
const isArray = require('lodash/isArray'); | ||
const isPlainObject = require('lodash/isPlainObject'); | ||
const isObject = require('lodash/isObject'); | ||
/** | ||
* Checks for match | ||
* @param {string[]} keywords A list of keywords to look for | ||
* @param {string} key The string to check | ||
* @param {Boolean} strict Use strict case if true | ||
* @param {Boolean} partial Use partial matching if true | ||
* @return {Boolean} True for match or false | ||
*/ | ||
function isKeywordMatch (keywords, key, strict, partial) { | ||
return some(keywords, (keyword) => { | ||
const keyMatch = strict ? key : key.toLowerCase(); | ||
const keywordMatch = strict ? keyword : keyword.toLowerCase(); | ||
return partial ? (keyMatch.indexOf(keywordMatch) !== -1) : (keyMatch === keywordMatch); | ||
}); | ||
} | ||
/** | ||
* Parses an object and redacts any keys listed in keywords | ||
@@ -11,18 +31,36 @@ * | ||
* @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 | ||
*/ | ||
function redact(target, keywords, replaceVal) { | ||
const replace = replaceVal || '[ REDACTED ]'; | ||
const targetCopy = extend({}, target); | ||
for (const x in targetCopy) { | ||
if (keywords.indexOf(x) > -1) { | ||
targetCopy[x] = replace; | ||
} else if (targetCopy[x] === Object(targetCopy[x])) { | ||
targetCopy[x] = redact(targetCopy[x], keywords); | ||
} | ||
function redact (target, keywords, replaceVal, config) { | ||
config = config || {}; | ||
const partial = config.hasOwnProperty('partial') ? config.partial : true; | ||
const strict = config.hasOwnProperty('strict') ? config.strict : true; | ||
if (!isObject(target)) { | ||
// If it's not an object then it's a primitive. Nothing to redact. | ||
return target; | ||
} else if (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)); | ||
} else if (isPlainObject(target)) { | ||
return Object.keys(target).reduce((newObj, key) => { | ||
const isMatch = isKeywordMatch(keywords, key, strict, partial); | ||
if (isMatch) { | ||
newObj[key] = replaceVal || '[ REDACTED ]'; | ||
} else { | ||
newObj[key] = redact(target[key], keywords, replaceVal, config); | ||
} | ||
return newObj; | ||
}, {}); | ||
} | ||
return targetCopy; | ||
// Redaction only works on arrays, plain objects, and primitives. | ||
throw new Error('Unsupported value for redaction'); | ||
} | ||
module.exports = redact; |
{ | ||
"name": "redact-object", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "Object redactor", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "npm run unit", | ||
"unit": "jasmine-node test/unit --verbose", | ||
"coverage": "istanbul cover --include-all-sources jasmine-node test/unit", | ||
"start": "node app.js" | ||
"test": "npm run lint && npm run unit", | ||
"lint": "semistandard --verbose | snazzy", | ||
"unit": "nyc jasmine-node test/unit --verbose", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls" | ||
}, | ||
@@ -23,11 +23,27 @@ "repository": { | ||
"author": "Shaun Buridck <github@shaunburdick.com>", | ||
"contributors": [ | ||
"Parsha Pourkhomami <parshap@gmail.com>" | ||
], | ||
"license": "ISC", | ||
"devDependencies": { | ||
"babel-eslint": "^5.0.0-beta2", | ||
"eslint": "^1.10.2", | ||
"eslint-config-airbnb": "^1.0.2", | ||
"istanbul": "^0.4.1", | ||
"jasmine-node": "^1.14.5" | ||
"coveralls": "^2.11.12", | ||
"jasmine-node": "^1.14.5", | ||
"nyc": "^8.1.0", | ||
"semistandard": "^8.0.0", | ||
"snazzy": "^4.0.1" | ||
}, | ||
"homepage": "https://github.com/shaunburdick/redact-object" | ||
"homepage": "https://github.com/shaunburdick/redact-object", | ||
"dependencies": { | ||
"lodash": "^4.15.0" | ||
}, | ||
"semistandard": { | ||
"ignore": [ | ||
"coverage" | ||
] | ||
}, | ||
"nyc": { | ||
"include": [ | ||
"index.js" | ||
] | ||
} | ||
} |
# Redact Object | ||
[![Build Status](https://travis-ci.org/shaunburdick/redact-object.svg?branch=master)](https://travis-ci.org/shaunburdick/redact-object) [![Coverage Status](https://coveralls.io/repos/github/shaunburdick/redact-object/badge.svg?branch=master)](https://coveralls.io/github/shaunburdick/redact-object?branch=master) [![npm version](https://badge.fury.io/js/redact-object.svg)](https://badge.fury.io/js/redact-object) [![js-semistandard-style](https://img.shields.io/badge/code%20style-semistandard-brightgreen.svg?style=flat-square)](https://github.com/Flet/semistandard) | ||
A javascript object redactor. So I like to output any configurations when my app starts up. Problem is if you have any passwords or other info you don't want in the log you have to clear it out beforehand. | ||
@@ -15,3 +18,8 @@ | ||
key: 'value' | ||
} | ||
}, | ||
buzz: [ | ||
// it can do arrays too | ||
{ foo: 'bar' }, | ||
22 | ||
] | ||
}; | ||
@@ -26,3 +34,8 @@ | ||
* key: 'value' | ||
* } | ||
* }, | ||
* buzz: [ | ||
* // it can do arrays too | ||
* { foo: 'bar' }, | ||
* 22 | ||
* ] | ||
* } | ||
@@ -37,2 +50,5 @@ */ | ||
- {string} replaceVal Optional custom replace value | ||
- {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_ | ||
@@ -44,4 +60,3 @@ ## Contributing | ||
4. Run `npm test` to see if the tests pass. Repeat steps 2-4 until done. | ||
5. Check code coverage `npm run coverage` and add test paths as needed. | ||
6. Update the documentation to reflect any changes. | ||
7. Push to your fork and submit a pull request. | ||
5. Update the documentation to reflect any changes. | ||
6. Push to your fork and submit a pull request. |
@@ -0,1 +1,2 @@ | ||
/* eslint-env node, jasmine */ | ||
'use strict'; | ||
@@ -8,7 +9,12 @@ | ||
fizz: { | ||
foo: 'bar', | ||
foo: 'bar' | ||
}, | ||
derp: 'poo', | ||
'auth-token': 'foo', | ||
'array': [{ foo: 'bar' }, 5] | ||
}; | ||
function NonPlainObject () { | ||
} | ||
const redactVal = '[ REDACTED ]'; | ||
@@ -21,2 +27,4 @@ | ||
expect(redacted.fizz.foo).toEqual(redactVal); | ||
expect(Array.isArray(redacted.array)).toBe(true); | ||
expect(redacted.array).toEqual([{ foo: redactVal }, 5]); | ||
}); | ||
@@ -40,2 +48,32 @@ | ||
}); | ||
it('should match partial strings', () => { | ||
const redacted = redact(testConfig, ['token']); | ||
expect(redacted['auth-token']).toEqual(redactVal); | ||
}); | ||
it('should not match partial strings if configured', () => { | ||
const origValue = testConfig['auth-token']; | ||
const redacted = redact(testConfig, ['token'], redactVal, { partial: false }); | ||
expect(redacted['auth-token']).toEqual(origValue); | ||
}); | ||
it('should be case-sensitive', () => { | ||
const origValue = testConfig.foo; | ||
const redacted = redact(testConfig, ['FOO']); | ||
expect(redacted.foo).toEqual(origValue); | ||
}); | ||
it('should be case-insensitive if configured', () => { | ||
const redacted = redact(testConfig, ['FOO'], redactVal, { strict: false }); | ||
expect(redacted.foo).toEqual(redactVal); | ||
expect(redacted.fizz.foo).toEqual(redactVal); | ||
}); | ||
it('should throw with non-plain object', () => { | ||
expect(() => { | ||
const nonPlainObject = new NonPlainObject(); | ||
redact(nonPlainObject, ['FOO']); | ||
}).toThrow(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
8105
122
59
1
6
+ Addedlodash@^4.15.0
+ Addedlodash@4.17.21(transitive)