Last active
August 31, 2022 03:47
-
-
Save Ivannnnn/f2a7c84dda5341323ca828969f8c3ead to your computer and use it in GitHub Desktop.
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
const functionalize = (arr) => { | |
const result = {}; | |
for (let funcOrArr of arr) { | |
const [func, indexOfData = 0] = | |
funcOrArr.constructor.name === "Array" ? funcOrArr : [funcOrArr]; | |
const name = func.name || func; | |
result[name] = | |
(...args) => | |
(data) => { | |
return typeof func === "function" | |
? args.splice(indexOfData, 0, data) && func(...args) | |
: data[func](...args); | |
}; | |
} | |
return result; | |
}; | |
/* USAGE | |
// | |
const { | |
splice, | |
slice, | |
encodeURIComponent: encode, | |
split: sp, | |
forEach, | |
} = functionalize([ | |
"splice", | |
"slice", | |
[split, 1], // split(delimiter, data) | |
"forEach", | |
window.encodeURIComponent, | |
]) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment