-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5340 from epixa/backport-pr-5038
- Loading branch information
Showing
6 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
var applyFiltersToKeys = require('../applyFiltersToKeys'); | ||
var expect = require('expect.js'); | ||
|
||
describe('applyFiltersToKeys(obj, actionsByKey)', function () { | ||
it('applies for each key+prop in actionsByKey', function () { | ||
var data = applyFiltersToKeys({ | ||
a: { | ||
b: { | ||
c: 1 | ||
}, | ||
d: { | ||
e: 'foobar' | ||
} | ||
}, | ||
req: { | ||
headers: { | ||
authorization: 'Basic dskd939k2i' | ||
} | ||
} | ||
}, { | ||
b: 'remove', | ||
e: 'censor', | ||
authorization: '/([^\\s]+)$/' | ||
}); | ||
|
||
expect(data).to.eql({ | ||
a: { | ||
d: { | ||
e: 'XXXXXX', | ||
}, | ||
}, | ||
req: { | ||
headers: { | ||
authorization: 'Basic XXXXXXXXXX' | ||
} | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
function toPojo(obj) { | ||
return JSON.parse(JSON.stringify(obj)); | ||
} | ||
|
||
function replacer(match, group) { | ||
return (new Array(group.length + 1).join('X')); | ||
} | ||
|
||
function apply(obj, key, action) { | ||
for (let k in obj) { | ||
if (obj.hasOwnProperty(k)) { | ||
let val = obj[k]; | ||
if (k === key) { | ||
if (action === 'remove') { | ||
delete obj[k]; | ||
} | ||
else if (action === 'censor' && typeof val === 'object') { | ||
delete obj[key]; | ||
} | ||
else if (action === 'censor') { | ||
obj[k] = ('' + val).replace(/./g, 'X'); | ||
} | ||
else if (/\/.+\//.test(action)) { | ||
var matches = action.match(/\/(.+)\//); | ||
if (matches) { | ||
let regex = new RegExp(matches[1]); | ||
obj[k] = ('' + val).replace(regex, replacer); | ||
} | ||
} | ||
} else if (typeof val === 'object') { | ||
val = apply(val, key, action); | ||
} | ||
} | ||
} | ||
return obj; | ||
} | ||
|
||
module.exports = function (obj, actionsByKey) { | ||
return Object.keys(actionsByKey).reduce((output, key) => { | ||
return apply(output, key, actionsByKey[key]); | ||
}, toPojo(obj)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters