Last active
June 9, 2020 14:53
-
-
Save jozefcipa/061fe21ed334853cf510bb1dd5dc10db to your computer and use it in GitHub Desktop.
Replace sensitive data in JSON (logs)
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
import cloneDeepWith from 'lodash.clonedeepwith' // or import { cloneDeepWith } from 'lodash' | |
function redactSensitiveData(data, sensitiveKeys) { | |
return cloneDeepWith(data, value => { | |
if (value && typeof value === 'object') { | |
sensitiveKeys.forEach(key => { | |
if (value[key]) { | |
value[key] = '[redacted]' | |
} | |
}) | |
} | |
}) | |
} | |
// Example | |
// >> redactSensitiveData({ name: 'john', password: '12345' }, ['password']) | |
// >> { name: 'john', password: '[redacted]' } | |
// Inspired by https://github.com/lodash/lodash/issues/723#issuecomment-194385740 🙏 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment