Moving key match above recursion for removing branches

This commit is contained in:
Chris Cowan 2015-09-25 11:44:48 -07:00 committed by Court Ewing
parent 21a6660103
commit 71d65e4adb
2 changed files with 11 additions and 3 deletions

View file

@ -21,6 +21,14 @@ describe('applyFilterToKey(obj, key, action)', function () {
});
});
it('should remove an entire branch', function () {
var data = fixture();
applyFilterToKey(data, 'headers', 'remove');
expect(data).to.eql({
req: { }
});
});
it('should censor a key in an object recursivly', function () {
var data = fixture();
applyFilterToKey(data, 'authorization', 'censor');

View file

@ -6,9 +6,7 @@ module.exports = function applyFilterToKey(obj, key, action) {
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
let val = obj[k];
if (typeof val === 'object') {
applyFilterToKey(val, key, action);
} else if (k === key) {
if (k === key) {
val = '' + val;
if (action === 'remove') delete obj[k];
if (action === 'censor') {
@ -17,6 +15,8 @@ module.exports = function applyFilterToKey(obj, key, action) {
if (action instanceof RegExp) {
obj[k] = val.replace(action, replacer);
}
} else if (typeof val === 'object') {
applyFilterToKey(val, key, action);
}
}
}