Skip to content

Instantly share code, notes, and snippets.

@thisgeek
Created May 22, 2017 20:10
Show Gist options
  • Save thisgeek/437d91d1a95c6829ee0d30dea6bd63cc to your computer and use it in GitHub Desktop.
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.
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