mirror of
https://github.com/swc-project/swc.git
synced 2024-12-26 23:27:56 +03:00
45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
|
// Loaded from https://deno.land/x/ramda@v0.27.2/source/internal/_dispatchable.js
|
||
|
|
||
|
|
||
|
import _isArray from './_isArray.js';
|
||
|
import _isTransformer from './_isTransformer.js';
|
||
|
|
||
|
|
||
|
/**
|
||
|
* Returns a function that dispatches with different strategies based on the
|
||
|
* object in list position (last argument). If it is an array, executes [fn].
|
||
|
* Otherwise, if it has a function with one of the given method names, it will
|
||
|
* execute that function (functor case). Otherwise, if it is a transformer,
|
||
|
* uses transducer created by [transducerCreator] to return a new transformer
|
||
|
* (transducer case).
|
||
|
* Otherwise, it will default to executing [fn].
|
||
|
*
|
||
|
* @private
|
||
|
* @param {Array} methodNames properties to check for a custom implementation
|
||
|
* @param {Function} transducerCreator transducer factory if object is transformer
|
||
|
* @param {Function} fn default ramda implementation
|
||
|
* @return {Function} A function that dispatches on object in list position
|
||
|
*/
|
||
|
export default function _dispatchable(methodNames, transducerCreator, fn) {
|
||
|
return function() {
|
||
|
if (arguments.length === 0) {
|
||
|
return fn();
|
||
|
}
|
||
|
var obj = arguments[arguments.length - 1];
|
||
|
if (!_isArray(obj)) {
|
||
|
var idx = 0;
|
||
|
while (idx < methodNames.length) {
|
||
|
if (typeof obj[methodNames[idx]] === 'function') {
|
||
|
return obj[methodNames[idx]].apply(obj, Array.prototype.slice.call(arguments, 0, -1));
|
||
|
}
|
||
|
idx += 1;
|
||
|
}
|
||
|
if (_isTransformer(obj)) {
|
||
|
var transducer = transducerCreator.apply(null, Array.prototype.slice.call(arguments, 0, -1));
|
||
|
return transducer(obj);
|
||
|
}
|
||
|
}
|
||
|
return fn.apply(this, arguments);
|
||
|
};
|
||
|
}
|