Skip to content

Instantly share code, notes, and snippets.

@jozefcipa
Last active June 9, 2020 14:53
Show Gist options
  • Save jozefcipa/061fe21ed334853cf510bb1dd5dc10db to your computer and use it in GitHub Desktop.
Save jozefcipa/061fe21ed334853cf510bb1dd5dc10db to your computer and use it in GitHub Desktop.
Replace sensitive data in JSON (logs)
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