Created
May 22, 2017 20:10
-
-
Save thisgeek/437d91d1a95c6829ee0d30dea6bd63cc to your computer and use it in GitHub Desktop.
A composition of lodash utilities that produces a function which changes the way any function passed to it receives parameters.
This file contains 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
import _ from 'lodash'; | |
// return a function where the keys in the passed object will be | |
// applied as arguments. | |
/* | |
var foo = function (a, b, c, d) { | |
// does stuff | |
}; | |
foo(1, 0, 'fi', 'fo'); | |
var bar = inject(foo, ['a', 'b', 'goo', 'boo']); | |
bar({ | |
a: 1, | |
b: 0, | |
goo: 'fi', | |
boo: 'fo' | |
}); | |
*/ | |
var inject = _.rest(function (fn, keys) { | |
return function (obj) { | |
var params = _.at(obj, keys); | |
var spreaded = _.spread(fn); | |
return spreaded(params); | |
}; | |
}); | |
module.exports = inject; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment