Skip to content

Instantly share code, notes, and snippets.

@tuxsudo
Created August 20, 2016 17:57
Show Gist options
  • Save tuxsudo/2c75b8cbce600b6b8145f35a9326344c to your computer and use it in GitHub Desktop.
Save tuxsudo/2c75b8cbce600b6b8145f35a9326344c to your computer and use it in GitHub Desktop.
// transform object's values
// given a object of functions and an object of value
// run object of values through object of functions
// matched by same prop name
export const map = (fieldFns = {} ) => obj => (
Object.keys(obj)
.reduce( (newObj, key) => {
const fn = fieldFns[ key ];
const originalValue = obj[ key ];
return ({
...newObj,
[ key ]: fn ? fn(originalValue) : originalValue
});
}, {})
);
// filter out some object props
// given a object of functions and an object of value
// run object of values through object of functions
// matched by same prop name
export const filter = (fieldFns = {}) => obj => (
Object.keys(obj)
.filter( key => fieldFns[key] ? fieldFns[key]( obj[key] ) : true )
.reduce( (newObj, key) => ({...newObj, [key]: obj[key] }), {} )
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment