Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

createChainedFunction.js 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. /**
  5. * Safe chained function
  6. *
  7. * Will only create a new function if needed,
  8. * otherwise will pass back existing functions or null.
  9. *
  10. * @param {function} functions to chain
  11. * @returns {function|null}
  12. */
  13. function createChainedFunction() {
  14. for (var _len = arguments.length, funcs = new Array(_len), _key = 0; _key < _len; _key++) {
  15. funcs[_key] = arguments[_key];
  16. }
  17. return funcs.filter(function (f) {
  18. return f != null;
  19. }).reduce(function (acc, f) {
  20. if (typeof f !== 'function') {
  21. throw new Error('Invalid Argument Type, must only provide functions, undefined, or null.');
  22. }
  23. if (acc === null) return f;
  24. return function chainedFunction() {
  25. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  26. args[_key2] = arguments[_key2];
  27. }
  28. acc.apply(this, args);
  29. f.apply(this, args);
  30. };
  31. }, null);
  32. }
  33. var _default = createChainedFunction;
  34. exports.default = _default;
  35. module.exports = exports["default"];