define-function-length.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. "use strict";
  2. var test = function (arg1, arg2) { return arg2; };
  3. try {
  4. Object.defineProperty(test, "length", {
  5. configurable: true,
  6. writable: false,
  7. enumerable: false,
  8. value: 1
  9. });
  10. }
  11. catch (ignore) {}
  12. if (test.length === 1) {
  13. // ES2015+
  14. var desc = { configurable: true, writable: false, enumerable: false };
  15. module.exports = function (length, fn) {
  16. if (fn.length === length) return fn;
  17. desc.value = length;
  18. return Object.defineProperty(fn, "length", desc);
  19. };
  20. return;
  21. }
  22. module.exports = function (length, fn) {
  23. if (fn.length === length) return fn;
  24. switch (length) {
  25. case 0:
  26. return function () { return fn.apply(this, arguments); };
  27. case 1:
  28. return function (ignored1) { return fn.apply(this, arguments); };
  29. case 2:
  30. return function (ignored1, ignored2) { return fn.apply(this, arguments); };
  31. case 3:
  32. return function (ignored1, ignored2, ignored3) { return fn.apply(this, arguments); };
  33. case 4:
  34. return function (ignored1, ignored2, ignored3, ignored4) {
  35. return fn.apply(this, arguments);
  36. };
  37. case 5:
  38. return function (ignored1, ignored2, ignored3, ignored4, ignored5) {
  39. return fn.apply(this, arguments);
  40. };
  41. case 6:
  42. return function (ignored1, ignored2, ignored3, ignored4, ignored5, ignored6) {
  43. return fn.apply(this, arguments);
  44. };
  45. case 7:
  46. return function (ignored1, ignored2, ignored3, ignored4, ignored5, ignored6, ignored7) {
  47. return fn.apply(this, arguments);
  48. };
  49. default:
  50. throw new Error("Usupported function length");
  51. }
  52. };