Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Example:
- const stringFunctions = makeFunctional(String);
- stringFunctions.stringSlice('foo,moo,bar')(0)(3); //output 'foo'
- */
- function createNamespace(className, property) {
- return className.toLowerCase()
- .concat(`${property.charAt(0).toUpperCase()}${property.slice(1)}`);
- }
- function curry(fn, maxArgs) {
- const max = maxArgs || fn.length;
- const exec = (...args) => {
- if (args.length >= max) {
- return fn.apply(null, args);
- }
- return exec.bind(null, ...args);
- };
- return exec;
- }
- function makeFunctional(classProvider) {
- const proto = classProvider.prototype;
- const propertyList = Object.getOwnPropertyNames(proto);
- const name = classProvider.name;
- return propertyList.reduce((fnMap, property) => {
- const value = proto[property];
- const namespace = createNamespace(name, property);
- if (!(value instanceof Function)) {
- return fnMap;
- }
- return Object.assign({}, fnMap, {
- [namespace]: curry(Function.prototype.call.bind(value), value.length + 1)
- });
- }, {});
- }
Add Comment
Please, Sign In to add comment