Created
August 20, 2016 17:57
-
-
Save tuxsudo/2c75b8cbce600b6b8145f35a9326344c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 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