Created
October 13, 2016 01:09
-
-
Save bradparker/ed93c58ed79c85a32dc8cc50fae3509f to your computer and use it in GitHub Desktop.
Mutator
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
const { keys, assign } = Object | |
const exampleSource = { | |
first_name: 'Bob', | |
last_name: 'Boberson', | |
charity_1: '1', | |
charity_2: '2', | |
charity_3: '3' | |
} | |
const exampleMutation = { | |
name ({ | |
first_name, | |
last_name | |
}) { | |
return `${first_name} ${last_name}` | |
}, | |
charity_id ({ | |
charity_1, | |
charity_2, | |
charity_3 | |
}) { | |
return [ | |
charity_1, | |
charity_2, | |
charity_3 | |
] | |
} | |
} | |
const mutator = (mutation) => (source) => ( | |
keys(mutation).reduce((acc, key) => (assign(acc, { | |
[key]: mutation[key](source) | |
})), {}) | |
) | |
const exampleMutator = mutator(exampleMutation) | |
console.log(exampleMutator(exampleSource)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment