Guest User

Untitled

a guest
Feb 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. /*
  2. Example:
  3.  
  4. const stringFunctions = makeFunctional(String);
  5.  
  6. stringFunctions.stringSlice('foo,moo,bar')(0)(3); //output 'foo'
  7. */
  8.  
  9. function createNamespace(className, property) {
  10. return className.toLowerCase()
  11. .concat(`${property.charAt(0).toUpperCase()}${property.slice(1)}`);
  12. }
  13.  
  14. function curry(fn, maxArgs) {
  15. const max = maxArgs || fn.length;
  16. const exec = (...args) => {
  17. if (args.length >= max) {
  18. return fn.apply(null, args);
  19. }
  20.  
  21. return exec.bind(null, ...args);
  22. };
  23.  
  24. return exec;
  25. }
  26.  
  27. function makeFunctional(classProvider) {
  28. const proto = classProvider.prototype;
  29. const propertyList = Object.getOwnPropertyNames(proto);
  30. const name = classProvider.name;
  31.  
  32. return propertyList.reduce((fnMap, property) => {
  33. const value = proto[property];
  34. const namespace = createNamespace(name, property);
  35.  
  36. if (!(value instanceof Function)) {
  37. return fnMap;
  38. }
  39.  
  40. return Object.assign({}, fnMap, {
  41. [namespace]: curry(Function.prototype.call.bind(value), value.length + 1)
  42. });
  43. }, {});
  44. }
Add Comment
Please, Sign In to add comment