Created
September 11, 2019 02:22
-
-
Save novascreen/58f847eefa06153f5096a9e34720169e to your computer and use it in GitHub Desktop.
Ramda Redact
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 { 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) | |
}) |
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 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