Skip to content

Instantly share code, notes, and snippets.

@mvaldesdeleon
Created August 4, 2018 12:53
Show Gist options
  • Save mvaldesdeleon/fe344b187767f0ec50d5605cf586378e to your computer and use it in GitHub Desktop.
Save mvaldesdeleon/fe344b187767f0ec50d5605cf586378e to your computer and use it in GitHub Desktop.
General object transformation pattern.
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