Skip to content

Instantly share code, notes, and snippets.

@novascreen
Created September 11, 2019 02:22
Show Gist options
  • Save novascreen/58f847eefa06153f5096a9e34720169e to your computer and use it in GitHub Desktop.
Save novascreen/58f847eefa06153f5096a9e34720169e to your computer and use it in GitHub Desktop.
Ramda Redact
import { redact } from './redact'
test('replaces given paths with [Redacted]', () => {
const data = { secret: '1234', nested: { secret: '5678' } }
const redactedData = redact([['secret'], ['nested', 'secret']], data)
const curriedRedactedData = redact([['secret'], ['nested', 'secret']])(data)
expect(redactedData).toMatchInlineSnapshot(`
Object {
"nested": Object {
"secret": "[Redacted]",
},
"secret": "[Redacted]",
}
`)
// ensure changes didn't mutate data
expect(redactedData).not.toEqual(data)
// ensure curried version of the function works
expect(curriedRedactedData).toEqual(redactedData)
})
import R from 'ramda'
/**
* Replaces values of provided paths in object with `[Redacted]`
*
* @param paths An array of ramda compatible path arrays
* @param obj The object you want to redact
*/
export const redact = R.curry(function redact(paths: string[][], obj: object): object {
let result = obj
R.forEach<string[]>(path => {
if (R.path(path, result)) {
result = R.assocPath(path, '[Redacted]', result)
}
}, paths)
return result
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment