source: node_modules/ramda/es/internal/_dispatchable.js@ d24f17c

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

Initial commit

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import _isArray from "./_isArray.js";
2import _isTransformer from "./_isTransformer.js";
3/**
4 * Returns a function that dispatches with different strategies based on the
5 * object in list position (last argument). If it is an array, executes [fn].
6 * Otherwise, if it has a function with one of the given method names, it will
7 * execute that function (functor case). Otherwise, if it is a transformer,
8 * uses transducer created by [transducerCreator] to return a new transformer
9 * (transducer case).
10 * Otherwise, it will default to executing [fn].
11 *
12 * @private
13 * @param {Array} methodNames properties to check for a custom implementation
14 * @param {Function} transducerCreator transducer factory if object is transformer
15 * @param {Function} fn default ramda implementation
16 * @return {Function} A function that dispatches on object in list position
17 */
18
19export default function _dispatchable(methodNames, transducerCreator, fn) {
20 return function () {
21 if (arguments.length === 0) {
22 return fn();
23 }
24
25 var obj = arguments[arguments.length - 1];
26
27 if (!_isArray(obj)) {
28 var idx = 0;
29
30 while (idx < methodNames.length) {
31 if (typeof obj[methodNames[idx]] === 'function') {
32 return obj[methodNames[idx]].apply(obj, Array.prototype.slice.call(arguments, 0, -1));
33 }
34
35 idx += 1;
36 }
37
38 if (_isTransformer(obj)) {
39 var transducer = transducerCreator.apply(null, Array.prototype.slice.call(arguments, 0, -1));
40 return transducer(obj);
41 }
42 }
43
44 return fn.apply(this, arguments);
45 };
46}
Note: See TracBrowser for help on using the repository browser.