Created
August 4, 2018 12:53
-
-
Save mvaldesdeleon/fe344b187767f0ec50d5605cf586378e to your computer and use it in GitHub Desktop.
General object transformation pattern.
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
const xxx = | |
prK => | |
prV => | |
mapK => | |
mapV => | |
obj => Object.entries(obj).reduce( | |
(obj, [key, value]) => | |
({ | |
...obj, | |
...(prK(key) && prV(value) ? {[mapK(key)]: mapV(value)} : {}) | |
}) | |
, {}); | |
const data = {a: 1, b: 2, c: 'jon', d: 'jane'}; | |
const isNum = x => Number.isInteger(x); | |
const alwaysTrue = x => true; | |
const identity = x => x; | |
const toUpper = x => x.toUpperCase(); | |
const timesTwo = x => x * 2; | |
const includes = xs => x => xs.includes(x); | |
xxx(alwaysTrue)(alwaysTrue)(identity)(identity)(data); | |
xxx(includes(['a', 'b']))(alwaysTrue)(toUpper)(timesTwo)(data); | |
xxx(alwaysTrue)(isNum)(identity)(identity)(data); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment