1 var mapping = require('./_mapping'),
2 fallbackHolder = require('./placeholder');
4 /** Built-in value reference. */
5 var push = Array.prototype.push;
8 * Creates a function, with an arity of `n`, that invokes `func` with the
9 * arguments it receives.
12 * @param {Function} func The function to wrap.
13 * @param {number} n The arity of the new function.
14 * @returns {Function} Returns the new function.
16 function baseArity(func, n) {
18 ? function(a, b) { return func.apply(undefined, arguments); }
19 : function(a) { return func.apply(undefined, arguments); };
23 * Creates a function that invokes `func`, with up to `n` arguments, ignoring
24 * any additional arguments.
27 * @param {Function} func The function to cap arguments for.
28 * @param {number} n The arity cap.
29 * @returns {Function} Returns the new function.
31 function baseAry(func, n) {
33 ? function(a, b) { return func(a, b); }
34 : function(a) { return func(a); };
38 * Creates a clone of `array`.
41 * @param {Array} array The array to clone.
42 * @returns {Array} Returns the cloned array.
44 function cloneArray(array) {
45 var length = array ? array.length : 0,
46 result = Array(length);
49 result[length] = array[length];
55 * Creates a function that clones a given object using the assignment `func`.
58 * @param {Function} func The assignment function.
59 * @returns {Function} Returns the new cloner function.
61 function createCloner(func) {
62 return function(object) {
63 return func({}, object);
68 * A specialized version of `_.spread` which flattens the spread array into
69 * the arguments of the invoked `func`.
72 * @param {Function} func The function to spread arguments over.
73 * @param {number} start The start position of the spread.
74 * @returns {Function} Returns the new function.
76 function flatSpread(func, start) {
78 var length = arguments.length,
79 lastIndex = length - 1,
83 args[length] = arguments[length];
85 var array = args[start],
86 otherArgs = args.slice(0, start);
89 push.apply(otherArgs, array);
91 if (start != lastIndex) {
92 push.apply(otherArgs, args.slice(start + 1));
94 return func.apply(this, otherArgs);
99 * Creates a function that wraps `func` and uses `cloner` to clone the first
100 * argument it receives.
103 * @param {Function} func The function to wrap.
104 * @param {Function} cloner The function to clone arguments.
105 * @returns {Function} Returns the new immutable function.
107 function wrapImmutable(func, cloner) {
109 var length = arguments.length;
113 var args = Array(length);
115 args[length] = arguments[length];
117 var result = args[0] = cloner.apply(undefined, args);
118 func.apply(undefined, args);
124 * The base implementation of `convert` which accepts a `util` object of methods
125 * required to perform conversions.
127 * @param {Object} util The util object.
128 * @param {string} name The name of the function to convert.
129 * @param {Function} func The function to convert.
130 * @param {Object} [options] The options object.
131 * @param {boolean} [options.cap=true] Specify capping iteratee arguments.
132 * @param {boolean} [options.curry=true] Specify currying.
133 * @param {boolean} [options.fixed=true] Specify fixed arity.
134 * @param {boolean} [options.immutable=true] Specify immutable operations.
135 * @param {boolean} [options.rearg=true] Specify rearranging arguments.
136 * @returns {Function|Object} Returns the converted function or object.
138 function baseConvert(util, name, func, options) {
140 isLib = typeof name == 'function',
141 isObj = name === Object(name);
151 options || (options = {});
154 'cap': 'cap' in options ? options.cap : true,
155 'curry': 'curry' in options ? options.curry : true,
156 'fixed': 'fixed' in options ? options.fixed : true,
157 'immutable': 'immutable' in options ? options.immutable : true,
158 'rearg': 'rearg' in options ? options.rearg : true
161 var forceCurry = ('curry' in options) && options.curry,
162 forceFixed = ('fixed' in options) && options.fixed,
163 forceRearg = ('rearg' in options) && options.rearg,
164 placeholder = isLib ? func : fallbackHolder,
165 pristine = isLib ? func.runInContext() : undefined;
167 var helpers = isLib ? func : {
169 'assign': util.assign,
172 'forEach': util.forEach,
173 'isArray': util.isArray,
174 'isFunction': util.isFunction,
175 'iteratee': util.iteratee,
178 'toInteger': util.toInteger,
179 'toPath': util.toPath
182 var ary = helpers.ary,
183 assign = helpers.assign,
184 clone = helpers.clone,
185 curry = helpers.curry,
186 each = helpers.forEach,
187 isArray = helpers.isArray,
188 isFunction = helpers.isFunction,
190 rearg = helpers.rearg,
191 toInteger = helpers.toInteger,
192 toPath = helpers.toPath;
194 var aryMethodKeys = keys(mapping.aryMethod);
197 'castArray': function(castArray) {
199 var value = arguments[0];
200 return isArray(value)
201 ? castArray(cloneArray(value))
202 : castArray.apply(undefined, arguments);
205 'iteratee': function(iteratee) {
207 var func = arguments[0],
208 arity = arguments[1],
209 result = iteratee(func, arity),
210 length = result.length;
212 if (config.cap && typeof arity == 'number') {
213 arity = arity > 2 ? (arity - 2) : 1;
214 return (length && length <= arity) ? result : baseAry(result, arity);
219 'mixin': function(mixin) {
220 return function(source) {
222 if (!isFunction(func)) {
223 return mixin(func, Object(source));
226 each(keys(source), function(key) {
227 if (isFunction(source[key])) {
228 pairs.push([key, func.prototype[key]]);
232 mixin(func, Object(source));
234 each(pairs, function(pair) {
236 if (isFunction(value)) {
237 func.prototype[pair[0]] = value;
239 delete func.prototype[pair[0]];
245 'nthArg': function(nthArg) {
247 var arity = n < 0 ? 1 : (toInteger(n) + 1);
248 return curry(nthArg(n), arity);
251 'rearg': function(rearg) {
252 return function(func, indexes) {
253 var arity = indexes ? indexes.length : 0;
254 return curry(rearg(func, indexes), arity);
257 'runInContext': function(runInContext) {
258 return function(context) {
259 return baseConvert(util, runInContext(context), options);
264 /*--------------------------------------------------------------------------*/
267 * Casts `func` to a function with an arity capped iteratee if needed.
270 * @param {string} name The name of the function to inspect.
271 * @param {Function} func The function to inspect.
272 * @returns {Function} Returns the cast function.
274 function castCap(name, func) {
276 var indexes = mapping.iterateeRearg[name];
278 return iterateeRearg(func, indexes);
280 var n = !isLib && mapping.iterateeAry[name];
282 return iterateeAry(func, n);
289 * Casts `func` to a curried function if needed.
292 * @param {string} name The name of the function to inspect.
293 * @param {Function} func The function to inspect.
294 * @param {number} n The arity of `func`.
295 * @returns {Function} Returns the cast function.
297 function castCurry(name, func, n) {
298 return (forceCurry || (config.curry && n > 1))
304 * Casts `func` to a fixed arity function if needed.
307 * @param {string} name The name of the function to inspect.
308 * @param {Function} func The function to inspect.
309 * @param {number} n The arity cap.
310 * @returns {Function} Returns the cast function.
312 function castFixed(name, func, n) {
313 if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
314 var data = mapping.methodSpread[name],
315 start = data && data.start;
317 return start === undefined ? ary(func, n) : flatSpread(func, start);
323 * Casts `func` to an rearged function if needed.
326 * @param {string} name The name of the function to inspect.
327 * @param {Function} func The function to inspect.
328 * @param {number} n The arity of `func`.
329 * @returns {Function} Returns the cast function.
331 function castRearg(name, func, n) {
332 return (config.rearg && n > 1 && (forceRearg || !mapping.skipRearg[name]))
333 ? rearg(func, mapping.methodRearg[name] || mapping.aryRearg[n])
338 * Creates a clone of `object` by `path`.
341 * @param {Object} object The object to clone.
342 * @param {Array|string} path The path to clone by.
343 * @returns {Object} Returns the cloned object.
345 function cloneByPath(object, path) {
349 length = path.length,
350 lastIndex = length - 1,
351 result = clone(Object(object)),
354 while (nested != null && ++index < length) {
355 var key = path[index],
359 nested[path[index]] = clone(index == lastIndex ? value : Object(value));
361 nested = nested[key];
367 * Converts `lodash` to an immutable auto-curried iteratee-first data-last
368 * version with conversion `options` applied.
370 * @param {Object} [options] The options object. See `baseConvert` for more details.
371 * @returns {Function} Returns the converted `lodash`.
373 function convertLib(options) {
374 return _.runInContext.convert(options)(undefined);
378 * Create a converter function for `func` of `name`.
380 * @param {string} name The name of the function to convert.
381 * @param {Function} func The function to convert.
382 * @returns {Function} Returns the new converter function.
384 function createConverter(name, func) {
385 var realName = mapping.aliasToReal[name] || name,
386 methodName = mapping.remap[realName] || realName,
387 oldOptions = options;
389 return function(options) {
390 var newUtil = isLib ? pristine : helpers,
391 newFunc = isLib ? pristine[methodName] : func,
392 newOptions = assign(assign({}, oldOptions), options);
394 return baseConvert(newUtil, realName, newFunc, newOptions);
399 * Creates a function that wraps `func` to invoke its iteratee, with up to `n`
400 * arguments, ignoring any additional arguments.
403 * @param {Function} func The function to cap iteratee arguments for.
404 * @param {number} n The arity cap.
405 * @returns {Function} Returns the new function.
407 function iterateeAry(func, n) {
408 return overArg(func, function(func) {
409 return typeof func == 'function' ? baseAry(func, n) : func;
414 * Creates a function that wraps `func` to invoke its iteratee with arguments
415 * arranged according to the specified `indexes` where the argument value at
416 * the first index is provided as the first argument, the argument value at
417 * the second index is provided as the second argument, and so on.
420 * @param {Function} func The function to rearrange iteratee arguments for.
421 * @param {number[]} indexes The arranged argument indexes.
422 * @returns {Function} Returns the new function.
424 function iterateeRearg(func, indexes) {
425 return overArg(func, function(func) {
426 var n = indexes.length;
427 return baseArity(rearg(baseAry(func, n), indexes), n);
432 * Creates a function that invokes `func` with its first argument transformed.
435 * @param {Function} func The function to wrap.
436 * @param {Function} transform The argument transform.
437 * @returns {Function} Returns the new function.
439 function overArg(func, transform) {
441 var length = arguments.length;
445 var args = Array(length);
447 args[length] = arguments[length];
449 var index = config.rearg ? 0 : (length - 1);
450 args[index] = transform(args[index]);
451 return func.apply(undefined, args);
456 * Creates a function that wraps `func` and applys the conversions
460 * @param {string} name The name of the function to wrap.
461 * @param {Function} func The function to wrap.
462 * @returns {Function} Returns the converted function.
464 function wrap(name, func) {
466 realName = mapping.aliasToReal[name] || name,
468 wrapper = wrappers[realName];
471 wrapped = wrapper(func);
473 else if (config.immutable) {
474 if (mapping.mutate.array[realName]) {
475 wrapped = wrapImmutable(func, cloneArray);
477 else if (mapping.mutate.object[realName]) {
478 wrapped = wrapImmutable(func, createCloner(func));
480 else if (mapping.mutate.set[realName]) {
481 wrapped = wrapImmutable(func, cloneByPath);
484 each(aryMethodKeys, function(aryKey) {
485 each(mapping.aryMethod[aryKey], function(otherName) {
486 if (realName == otherName) {
487 var data = mapping.methodSpread[realName],
488 afterRearg = data && data.afterRearg;
491 ? castFixed(realName, castRearg(realName, wrapped, aryKey), aryKey)
492 : castRearg(realName, castFixed(realName, wrapped, aryKey), aryKey);
494 result = castCap(realName, result);
495 result = castCurry(realName, result, aryKey);
502 result || (result = wrapped);
503 if (result == func) {
504 result = forceCurry ? curry(result, 1) : function() {
505 return func.apply(this, arguments);
508 result.convert = createConverter(realName, func);
509 if (mapping.placeholder[realName]) {
510 setPlaceholder = true;
511 result.placeholder = func.placeholder = placeholder;
516 /*--------------------------------------------------------------------------*/
519 return wrap(name, func);
523 // Convert methods by ary cap.
525 each(aryMethodKeys, function(aryKey) {
526 each(mapping.aryMethod[aryKey], function(key) {
527 var func = _[mapping.remap[key] || key];
529 pairs.push([key, wrap(key, func)]);
534 // Convert remaining methods.
535 each(keys(_), function(key) {
537 if (typeof func == 'function') {
538 var length = pairs.length;
540 if (pairs[length][0] == key) {
544 func.convert = createConverter(key, func);
545 pairs.push([key, func]);
549 // Assign to `_` leaving `_.prototype` unchanged to allow chaining.
550 each(pairs, function(pair) {
551 _[pair[0]] = pair[1];
554 _.convert = convertLib;
555 if (setPlaceholder) {
556 _.placeholder = placeholder;
559 each(keys(_), function(key) {
560 each(mapping.realToAlias[key] || [], function(alias) {
568 module.exports = baseConvert;