Skip to content

Instantly share code, notes, and snippets.

@mrkev
Created May 30, 2016 17:56
Show Gist options
  • Save mrkev/19f6df021140309d08ebdc16c2020493 to your computer and use it in GitHub Desktop.
Save mrkev/19f6df021140309d08ebdc16c2020493 to your computer and use it in GitHub Desktop.
Objectifies functions lol
'use strict';
var args = function (fun) {
var str = fun.toString().split("\n")[0];
return str.substring(str.indexOf("(")+1, str.indexOf(")")).split(",").map(function (x) { return x.trim(); });
};
var objectify = function (fun) {
if (typeof fun !== 'function') throw new Error(fun + " is not a function.");
// could extend so it also accepts an array or arguments
return function (obj) {
var call_args = args(fun).map(function (arg) {
return obj[arg];
});
return fun.apply(this, call_args);
};
};
// let add = (a = 3, b, c) => a + b + c;
// var oadd = objectify(add);
// console.log(oadd({a: 1, b: 2, c: 3}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment