1 var mapping = require('./_mapping'),
2 fallbackHolder = require('./placeholder');
5 * Creates a function, with an arity of `n`, that invokes `func` with the
6 * arguments it receives.
9 * @param {Function} func The function to wrap.
10 * @param {number} n The arity of the new function.
11 * @returns {Function} Returns the new function.
13 function baseArity(func, n) {
15 ? function(a, b) { return func.apply(undefined, arguments); }
16 : function(a) { return func.apply(undefined, arguments); };
20 * Creates a function that invokes `func`, with up to `n` arguments, ignoring
21 * any additional arguments.
24 * @param {Function} func The function to cap arguments for.
25 * @param {number} n The arity cap.
26 * @returns {Function} Returns the new function.
28 function baseAry(func, n) {
30 ? function(a, b) { return func(a, b); }
31 : function(a) { return func(a); };
35 * Creates a clone of `array`.
38 * @param {Array} array The array to clone.
39 * @returns {Array} Returns the cloned array.
41 function cloneArray(array) {
42 var length = array ? array.length : 0,
43 result = Array(length);
46 result[length] = array[length];
52 * Creates a function that clones a given object using the assignment `func`.
55 * @param {Function} func The assignment function.
56 * @returns {Function} Returns the new cloner function.
58 function createCloner(func) {
59 return function(object) {
60 return func({}, object);
65 * Creates a function that wraps `func` and uses `cloner` to clone the first
66 * argument it receives.
69 * @param {Function} func The function to wrap.
70 * @param {Function} cloner The function to clone arguments.
71 * @returns {Function} Returns the new immutable function.
73 function wrapImmutable(func, cloner) {
75 var length = arguments.length;
79 var args = Array(length);
81 args[length] = arguments[length];
83 var result = args[0] = cloner.apply(undefined, args);
84 func.apply(undefined, args);
90 * The base implementation of `convert` which accepts a `util` object of methods
91 * required to perform conversions.
93 * @param {Object} util The util object.
94 * @param {string} name The name of the function to convert.
95 * @param {Function} func The function to convert.
96 * @param {Object} [options] The options object.
97 * @param {boolean} [options.cap=true] Specify capping iteratee arguments.
98 * @param {boolean} [options.curry=true] Specify currying.
99 * @param {boolean} [options.fixed=true] Specify fixed arity.
100 * @param {boolean} [options.immutable=true] Specify immutable operations.
101 * @param {boolean} [options.rearg=true] Specify rearranging arguments.
102 * @returns {Function|Object} Returns the converted function or object.
104 function baseConvert(util, name, func, options) {
106 isLib = typeof name == 'function',
107 isObj = name === Object(name);
117 options || (options = {});
120 'cap': 'cap' in options ? options.cap : true,
121 'curry': 'curry' in options ? options.curry : true,
122 'fixed': 'fixed' in options ? options.fixed : true,
123 'immutable': 'immutable' in options ? options.immutable : true,
124 'rearg': 'rearg' in options ? options.rearg : true
127 var forceCurry = ('curry' in options) && options.curry,
128 forceFixed = ('fixed' in options) && options.fixed,
129 forceRearg = ('rearg' in options) && options.rearg,
130 placeholder = isLib ? func : fallbackHolder,
131 pristine = isLib ? func.runInContext() : undefined;
133 var helpers = isLib ? func : {
135 'assign': util.assign,
138 'forEach': util.forEach,
139 'isArray': util.isArray,
140 'isFunction': util.isFunction,
141 'iteratee': util.iteratee,
144 'spread': util.spread,
145 'toInteger': util.toInteger,
146 'toPath': util.toPath
149 var ary = helpers.ary,
150 assign = helpers.assign,
151 clone = helpers.clone,
152 curry = helpers.curry,
153 each = helpers.forEach,
154 isArray = helpers.isArray,
155 isFunction = helpers.isFunction,
157 rearg = helpers.rearg,
158 spread = helpers.spread,
159 toInteger = helpers.toInteger,
160 toPath = helpers.toPath;
162 var aryMethodKeys = keys(mapping.aryMethod);
165 'castArray': function(castArray) {
167 var value = arguments[0];
168 return isArray(value)
169 ? castArray(cloneArray(value))
170 : castArray.apply(undefined, arguments);
173 'iteratee': function(iteratee) {
175 var func = arguments[0],
176 arity = arguments[1],
177 result = iteratee(func, arity),
178 length = result.length;
180 if (config.cap && typeof arity == 'number') {
181 arity = arity > 2 ? (arity - 2) : 1;
182 return (length && length <= arity) ? result : baseAry(result, arity);
187 'mixin': function(mixin) {
188 return function(source) {
190 if (!isFunction(func)) {
191 return mixin(func, Object(source));
194 each(keys(source), function(key) {
195 if (isFunction(source[key])) {
196 pairs.push([key, func.prototype[key]]);
200 mixin(func, Object(source));
202 each(pairs, function(pair) {
204 if (isFunction(value)) {
205 func.prototype[pair[0]] = value;
207 delete func.prototype[pair[0]];
213 'nthArg': function(nthArg) {
215 var arity = n < 0 ? 1 : (toInteger(n) + 1);
216 return curry(nthArg(n), arity);
219 'rearg': function(rearg) {
220 return function(func, indexes) {
221 var arity = indexes ? indexes.length : 0;
222 return curry(rearg(func, indexes), arity);
225 'runInContext': function(runInContext) {
226 return function(context) {
227 return baseConvert(util, runInContext(context), options);
232 /*--------------------------------------------------------------------------*/
235 * Casts `func` to a function with an arity capped iteratee if needed.
238 * @param {string} name The name of the function to inspect.
239 * @param {Function} func The function to inspect.
240 * @returns {Function} Returns the cast function.
242 function castCap(name, func) {
244 var indexes = mapping.iterateeRearg[name];
246 return iterateeRearg(func, indexes);
248 var n = !isLib && mapping.iterateeAry[name];
250 return iterateeAry(func, n);
257 * Casts `func` to a curried function if needed.
260 * @param {string} name The name of the function to inspect.
261 * @param {Function} func The function to inspect.
262 * @param {number} n The arity of `func`.
263 * @returns {Function} Returns the cast function.
265 function castCurry(name, func, n) {
266 return (forceCurry || (config.curry && n > 1))
272 * Casts `func` to a fixed arity function if needed.
275 * @param {string} name The name of the function to inspect.
276 * @param {Function} func The function to inspect.
277 * @param {number} n The arity cap.
278 * @returns {Function} Returns the cast function.
280 function castFixed(name, func, n) {
281 if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
282 var data = mapping.methodSpread[name],
283 start = data && data.start;
285 return start === undefined ? ary(func, n) : spread(func, start);
291 * Casts `func` to an rearged function if needed.
294 * @param {string} name The name of the function to inspect.
295 * @param {Function} func The function to inspect.
296 * @param {number} n The arity of `func`.
297 * @returns {Function} Returns the cast function.
299 function castRearg(name, func, n) {
300 return (config.rearg && n > 1 && (forceRearg || !mapping.skipRearg[name]))
301 ? rearg(func, mapping.methodRearg[name] || mapping.aryRearg[n])
306 * Creates a clone of `object` by `path`.
309 * @param {Object} object The object to clone.
310 * @param {Array|string} path The path to clone by.
311 * @returns {Object} Returns the cloned object.
313 function cloneByPath(object, path) {
317 length = path.length,
318 lastIndex = length - 1,
319 result = clone(Object(object)),
322 while (nested != null && ++index < length) {
323 var key = path[index],
327 nested[path[index]] = clone(index == lastIndex ? value : Object(value));
329 nested = nested[key];
335 * Converts `lodash` to an immutable auto-curried iteratee-first data-last
336 * version with conversion `options` applied.
338 * @param {Object} [options] The options object. See `baseConvert` for more details.
339 * @returns {Function} Returns the converted `lodash`.
341 function convertLib(options) {
342 return _.runInContext.convert(options)(undefined);
346 * Create a converter function for `func` of `name`.
348 * @param {string} name The name of the function to convert.
349 * @param {Function} func The function to convert.
350 * @returns {Function} Returns the new converter function.
352 function createConverter(name, func) {
353 var realName = mapping.aliasToReal[name] || name,
354 methodName = mapping.remap[realName] || realName,
355 oldOptions = options;
357 return function(options) {
358 var newUtil = isLib ? pristine : helpers,
359 newFunc = isLib ? pristine[methodName] : func,
360 newOptions = assign(assign({}, oldOptions), options);
362 return baseConvert(newUtil, realName, newFunc, newOptions);
367 * Creates a function that wraps `func` to invoke its iteratee, with up to `n`
368 * arguments, ignoring any additional arguments.
371 * @param {Function} func The function to cap iteratee arguments for.
372 * @param {number} n The arity cap.
373 * @returns {Function} Returns the new function.
375 function iterateeAry(func, n) {
376 return overArg(func, function(func) {
377 return typeof func == 'function' ? baseAry(func, n) : func;
382 * Creates a function that wraps `func` to invoke its iteratee with arguments
383 * arranged according to the specified `indexes` where the argument value at
384 * the first index is provided as the first argument, the argument value at
385 * the second index is provided as the second argument, and so on.
388 * @param {Function} func The function to rearrange iteratee arguments for.
389 * @param {number[]} indexes The arranged argument indexes.
390 * @returns {Function} Returns the new function.
392 function iterateeRearg(func, indexes) {
393 return overArg(func, function(func) {
394 var n = indexes.length;
395 return baseArity(rearg(baseAry(func, n), indexes), n);
400 * Creates a function that invokes `func` with its first argument transformed.
403 * @param {Function} func The function to wrap.
404 * @param {Function} transform The argument transform.
405 * @returns {Function} Returns the new function.
407 function overArg(func, transform) {
409 var length = arguments.length;
413 var args = Array(length);
415 args[length] = arguments[length];
417 var index = config.rearg ? 0 : (length - 1);
418 args[index] = transform(args[index]);
419 return func.apply(undefined, args);
424 * Creates a function that wraps `func` and applys the conversions
428 * @param {string} name The name of the function to wrap.
429 * @param {Function} func The function to wrap.
430 * @returns {Function} Returns the converted function.
432 function wrap(name, func) {
434 realName = mapping.aliasToReal[name] || name,
436 wrapper = wrappers[realName];
439 wrapped = wrapper(func);
441 else if (config.immutable) {
442 if (mapping.mutate.array[realName]) {
443 wrapped = wrapImmutable(func, cloneArray);
445 else if (mapping.mutate.object[realName]) {
446 wrapped = wrapImmutable(func, createCloner(func));
448 else if (mapping.mutate.set[realName]) {
449 wrapped = wrapImmutable(func, cloneByPath);
452 each(aryMethodKeys, function(aryKey) {
453 each(mapping.aryMethod[aryKey], function(otherName) {
454 if (realName == otherName) {
455 var spreadData = mapping.methodSpread[realName],
456 afterRearg = spreadData && spreadData.afterRearg;
459 ? castFixed(realName, castRearg(realName, wrapped, aryKey), aryKey)
460 : castRearg(realName, castFixed(realName, wrapped, aryKey), aryKey);
462 result = castCap(realName, result);
463 result = castCurry(realName, result, aryKey);
470 result || (result = wrapped);
471 if (result == func) {
472 result = forceCurry ? curry(result, 1) : function() {
473 return func.apply(this, arguments);
476 result.convert = createConverter(realName, func);
477 if (mapping.placeholder[realName]) {
478 setPlaceholder = true;
479 result.placeholder = func.placeholder = placeholder;
484 /*--------------------------------------------------------------------------*/
487 return wrap(name, func);
491 // Convert methods by ary cap.
493 each(aryMethodKeys, function(aryKey) {
494 each(mapping.aryMethod[aryKey], function(key) {
495 var func = _[mapping.remap[key] || key];
497 pairs.push([key, wrap(key, func)]);
502 // Convert remaining methods.
503 each(keys(_), function(key) {
505 if (typeof func == 'function') {
506 var length = pairs.length;
508 if (pairs[length][0] == key) {
512 func.convert = createConverter(key, func);
513 pairs.push([key, func]);
517 // Assign to `_` leaving `_.prototype` unchanged to allow chaining.
518 each(pairs, function(pair) {
519 _[pair[0]] = pair[1];
522 _.convert = convertLib;
523 if (setPlaceholder) {
524 _.placeholder = placeholder;
527 each(keys(_), function(key) {
528 each(mapping.realToAlias[key] || [], function(alias) {
536 module.exports = baseConvert;