Created
May 30, 2016 17:56
-
-
Save mrkev/19f6df021140309d08ebdc16c2020493 to your computer and use it in GitHub Desktop.
Objectifies functions lol
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
'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