UNPKG

1.51 kBJavaScriptView Raw
1var _isArray =
2/*#__PURE__*/
3require("./_isArray");
4
5var _isTransformer =
6/*#__PURE__*/
7require("./_isTransformer");
8/**
9 * Returns a function that dispatches with different strategies based on the
10 * object in list position (last argument). If it is an array, executes [fn].
11 * Otherwise, if it has a function with one of the given method names, it will
12 * execute that function (functor case). Otherwise, if it is a transformer,
13 * uses transducer [xf] to return a new transformer (transducer case).
14 * Otherwise, it will default to executing [fn].
15 *
16 * @private
17 * @param {Array} methodNames properties to check for a custom implementation
18 * @param {Function} xf transducer to initialize if object is transformer
19 * @param {Function} fn default ramda implementation
20 * @return {Function} A function that dispatches on object in list position
21 */
22
23
24function _dispatchable(methodNames, xf, fn) {
25 return function () {
26 if (arguments.length === 0) {
27 return fn();
28 }
29
30 var args = Array.prototype.slice.call(arguments, 0);
31 var obj = args.pop();
32
33 if (!_isArray(obj)) {
34 var idx = 0;
35
36 while (idx < methodNames.length) {
37 if (typeof obj[methodNames[idx]] === 'function') {
38 return obj[methodNames[idx]].apply(obj, args);
39 }
40
41 idx += 1;
42 }
43
44 if (_isTransformer(obj)) {
45 var transducer = xf.apply(null, args);
46 return transducer(obj);
47 }
48 }
49
50 return fn.apply(this, arguments);
51 };
52}
53
54module.exports = _dispatchable;
\No newline at end of file