Skip to content

Instantly share code, notes, and snippets.

@bradparker
Created October 13, 2016 01:09
Show Gist options
  • Save bradparker/ed93c58ed79c85a32dc8cc50fae3509f to your computer and use it in GitHub Desktop.
Save bradparker/ed93c58ed79c85a32dc8cc50fae3509f to your computer and use it in GitHub Desktop.
Mutator
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