source: node_modules/ramda/src/internal/_dispatchable.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: 1.6 KB
Line 
1var _isArray =
2/*#__PURE__*/
3require("./_isArray.js");
4
5var _isTransformer =
6/*#__PURE__*/
7require("./_isTransformer.js");
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 created by [transducerCreator] to return a new transformer
14 * (transducer case).
15 * Otherwise, it will default to executing [fn].
16 *
17 * @private
18 * @param {Array} methodNames properties to check for a custom implementation
19 * @param {Function} transducerCreator transducer factory if object is transformer
20 * @param {Function} fn default ramda implementation
21 * @return {Function} A function that dispatches on object in list position
22 */
23
24
25function _dispatchable(methodNames, transducerCreator, fn) {
26 return function () {
27 if (arguments.length === 0) {
28 return fn();
29 }
30
31 var obj = arguments[arguments.length - 1];
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, Array.prototype.slice.call(arguments, 0, -1));
39 }
40
41 idx += 1;
42 }
43
44 if (_isTransformer(obj)) {
45 var transducer = transducerCreator.apply(null, Array.prototype.slice.call(arguments, 0, -1));
46 return transducer(obj);
47 }
48 }
49
50 return fn.apply(this, arguments);
51 };
52}
53
54module.exports = _dispatchable;
Note: See TracBrowser for help on using the repository browser.