source: node_modules/ramda-adjunct/lib/dispatch.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 10.1 KB
RevLine 
[d24f17c]1"use strict";
2
3exports.__esModule = true;
4exports["default"] = void 0;
5var _ramda = require("ramda");
6var _isNotNil = _interopRequireDefault(require("./isNotNil"));
7var _isNonEmptyArray = _interopRequireDefault(require("./isNonEmptyArray"));
8var _stubUndefined = _interopRequireDefault(require("./stubUndefined"));
9function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
10function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
11function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
12function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
13function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
14function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
15function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } /**
16 * Can be used as a way to compose multiple invokers together to form polymorphic functions,
17 * or functions that exhibit different behaviors based on their argument(s).
18 * Consumes dispatching functions and keep trying to invoke each in turn, until a non-nil value is returned.
19 *
20 * Accepts a list of dispatching functions and returns a new function.
21 * When invoked, this new function is applied to some arguments,
22 * each dispatching function is applied to those same arguments until one of the
23 * dispatching functions returns a non-nil value.
24 *
25 * @func dispatch
26 * @memberOf RA
27 * @since {@link https://char0n.github.io/ramda-adjunct/2.6.0|v2.6.0}
28 * @category Function
29 * @sig [((a, b, ...) -> x1), ((a, b, ...) -> x2), ...] -> x1 | x2 | ...
30 * @param {!Array} functions A list of functions
31 * @return {*|undefined} Returns the first not-nil value, or undefined if either an empty list is provided or none of the dispatching functions returns a non-nil value
32 * @see {@link RA.isNotNil}
33 * @example
34 *
35 * // returns first non-nil value
36 * const stubNil = () => null;
37 * const stubUndefined = () => undefined;
38 * const addOne = v => v + 1;
39 * const addTwo = v => v + 2;
40 *
41 * RA.dispatch([stubNil, stubUndefined, addOne, addTwo])(1); //=> 2
42 *
43 * // acts as a switch
44 * const fnSwitch = RA.dispatch([
45 * R.ifElse(RA.isString, s => `${s}-join`, RA.stubUndefined),
46 * R.ifElse(RA.isNumber, n => n + 1, RA.stubUndefined),
47 * R.ifElse(RA.isDate, R.T, RA.stubUndefined),
48 * ]);
49 * fnSwitch(1); //=> 2
50 */
51var byArity = (0, _ramda.comparator)(function (a, b) {
52 return a.length > b.length;
53});
54var getMaxArity = (0, _ramda.pipe)((0, _ramda.sort)(byArity), _ramda.head, (0, _ramda.prop)('length'));
55var iteratorFn = (0, _ramda.curry)(function (args, accumulator, fn) {
56 var result = fn.apply(void 0, _toConsumableArray(args));
57 return (0, _isNotNil["default"])(result) ? (0, _ramda.reduced)(result) : accumulator;
58});
59var dispatchImpl = function dispatchImpl(functions) {
60 var arity = getMaxArity(functions);
61 return (0, _ramda.curryN)(arity, function () {
62 for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
63 args[_key] = arguments[_key];
64 }
65 return (0, _ramda.reduce)(iteratorFn(args), undefined, functions);
66 });
67};
68var dispatch = (0, _ramda.ifElse)(_isNonEmptyArray["default"], dispatchImpl, _stubUndefined["default"]);
69var _default = dispatch;
70exports["default"] = _default;
Note: See TracBrowser for help on using the repository browser.