1 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
---|
2 | function _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."); }
|
---|
3 | function _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); }
|
---|
4 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
---|
5 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
---|
6 | function _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; }
|
---|
7 | import { sort, comparator, prop, pipe, head, curryN, reduce, reduced, curry, ifElse } from 'ramda';
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Can be used as a way to compose multiple invokers together to form polymorphic functions,
|
---|
11 | * or functions that exhibit different behaviors based on their argument(s).
|
---|
12 | * Consumes dispatching functions and keep trying to invoke each in turn, until a non-nil value is returned.
|
---|
13 | *
|
---|
14 | * Accepts a list of dispatching functions and returns a new function.
|
---|
15 | * When invoked, this new function is applied to some arguments,
|
---|
16 | * each dispatching function is applied to those same arguments until one of the
|
---|
17 | * dispatching functions returns a non-nil value.
|
---|
18 | *
|
---|
19 | * @func dispatch
|
---|
20 | * @memberOf RA
|
---|
21 | * @since {@link https://char0n.github.io/ramda-adjunct/2.6.0|v2.6.0}
|
---|
22 | * @category Function
|
---|
23 | * @sig [((a, b, ...) -> x1), ((a, b, ...) -> x2), ...] -> x1 | x2 | ...
|
---|
24 | * @param {!Array} functions A list of functions
|
---|
25 | * @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
|
---|
26 | * @see {@link RA.isNotNil}
|
---|
27 | * @example
|
---|
28 | *
|
---|
29 | * // returns first non-nil value
|
---|
30 | * const stubNil = () => null;
|
---|
31 | * const stubUndefined = () => undefined;
|
---|
32 | * const addOne = v => v + 1;
|
---|
33 | * const addTwo = v => v + 2;
|
---|
34 | *
|
---|
35 | * RA.dispatch([stubNil, stubUndefined, addOne, addTwo])(1); //=> 2
|
---|
36 | *
|
---|
37 | * // acts as a switch
|
---|
38 | * const fnSwitch = RA.dispatch([
|
---|
39 | * R.ifElse(RA.isString, s => `${s}-join`, RA.stubUndefined),
|
---|
40 | * R.ifElse(RA.isNumber, n => n + 1, RA.stubUndefined),
|
---|
41 | * R.ifElse(RA.isDate, R.T, RA.stubUndefined),
|
---|
42 | * ]);
|
---|
43 | * fnSwitch(1); //=> 2
|
---|
44 | */
|
---|
45 | import isNotNil from './isNotNil';
|
---|
46 | import isNonEmptyArray from './isNonEmptyArray';
|
---|
47 | import stubUndefined from './stubUndefined';
|
---|
48 | var byArity = comparator(function (a, b) {
|
---|
49 | return a.length > b.length;
|
---|
50 | });
|
---|
51 | var getMaxArity = pipe(sort(byArity), head, prop('length'));
|
---|
52 | var iteratorFn = curry(function (args, accumulator, fn) {
|
---|
53 | var result = fn.apply(void 0, _toConsumableArray(args));
|
---|
54 | return isNotNil(result) ? reduced(result) : accumulator;
|
---|
55 | });
|
---|
56 | var dispatchImpl = function dispatchImpl(functions) {
|
---|
57 | var arity = getMaxArity(functions);
|
---|
58 | return curryN(arity, function () {
|
---|
59 | for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
---|
60 | args[_key] = arguments[_key];
|
---|
61 | }
|
---|
62 | return reduce(iteratorFn(args), undefined, functions);
|
---|
63 | });
|
---|
64 | };
|
---|
65 | var dispatch = ifElse(isNonEmptyArray, dispatchImpl, stubUndefined);
|
---|
66 | export default dispatch; |
---|