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) {
139 var isLib = typeof name == 'function',
140 isObj = name === Object(name);
150 options || (options = {});
153 'cap': 'cap' in options ? options.cap : true,
154 'curry': 'curry' in options ? options.curry : true,
155 'fixed': 'fixed' in options ? options.fixed : true,
156 'immutable': 'immutable' in options ? options.immutable : true,
157 'rearg': 'rearg' in options ? options.rearg : true
160 var defaultHolder = isLib ? func : fallbackHolder,
161 forceCurry = ('curry' in options) && options.curry,
162 forceFixed = ('fixed' in options) && options.fixed,
163 forceRearg = ('rearg' in options) && options.rearg,
164 pristine = isLib ? func.runInContext() : undefined;
166 var helpers = isLib ? func : {
168 'assign': util.assign,
171 'forEach': util.forEach,
172 'isArray': util.isArray,
173 'isError': util.isError,
174 'isFunction': util.isFunction,
175 'isWeakMap': util.isWeakMap,
176 'iteratee': util.iteratee,
179 'toInteger': util.toInteger,
180 'toPath': util.toPath
183 var ary = helpers.ary,
184 assign = helpers.assign,
185 clone = helpers.clone,
186 curry = helpers.curry,
187 each = helpers.forEach,
188 isArray = helpers.isArray,
189 isError = helpers.isError,
190 isFunction = helpers.isFunction,
191 isWeakMap = helpers.isWeakMap,
193 rearg = helpers.rearg,
194 toInteger = helpers.toInteger,
195 toPath = helpers.toPath;
197 var aryMethodKeys = keys(mapping.aryMethod);
200 'castArray': function(castArray) {
202 var value = arguments[0];
203 return isArray(value)
204 ? castArray(cloneArray(value))
205 : castArray.apply(undefined, arguments);
208 'iteratee': function(iteratee) {
210 var func = arguments[0],
211 arity = arguments[1],
212 result = iteratee(func, arity),
213 length = result.length;
215 if (config.cap && typeof arity == 'number') {
216 arity = arity > 2 ? (arity - 2) : 1;
217 return (length && length <= arity) ? result : baseAry(result, arity);
222 'mixin': function(mixin) {
223 return function(source) {
225 if (!isFunction(func)) {
226 return mixin(func, Object(source));
229 each(keys(source), function(key) {
230 if (isFunction(source[key])) {
231 pairs.push([key, func.prototype[key]]);
235 mixin(func, Object(source));
237 each(pairs, function(pair) {
239 if (isFunction(value)) {
240 func.prototype[pair[0]] = value;
242 delete func.prototype[pair[0]];
248 'nthArg': function(nthArg) {
250 var arity = n < 0 ? 1 : (toInteger(n) + 1);
251 return curry(nthArg(n), arity);
254 'rearg': function(rearg) {
255 return function(func, indexes) {
256 var arity = indexes ? indexes.length : 0;
257 return curry(rearg(func, indexes), arity);
260 'runInContext': function(runInContext) {
261 return function(context) {
262 return baseConvert(util, runInContext(context), options);
267 /*--------------------------------------------------------------------------*/
270 * Casts `func` to a function with an arity capped iteratee if needed.
273 * @param {string} name The name of the function to inspect.
274 * @param {Function} func The function to inspect.
275 * @returns {Function} Returns the cast function.
277 function castCap(name, func) {
279 var indexes = mapping.iterateeRearg[name];
281 return iterateeRearg(func, indexes);
283 var n = !isLib && mapping.iterateeAry[name];
285 return iterateeAry(func, n);
292 * Casts `func` to a curried function if needed.
295 * @param {string} name The name of the function to inspect.
296 * @param {Function} func The function to inspect.
297 * @param {number} n The arity of `func`.
298 * @returns {Function} Returns the cast function.
300 function castCurry(name, func, n) {
301 return (forceCurry || (config.curry && n > 1))
307 * Casts `func` to a fixed arity function if needed.
310 * @param {string} name The name of the function to inspect.
311 * @param {Function} func The function to inspect.
312 * @param {number} n The arity cap.
313 * @returns {Function} Returns the cast function.
315 function castFixed(name, func, n) {
316 if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
317 var data = mapping.methodSpread[name],
318 start = data && data.start;
320 return start === undefined ? ary(func, n) : flatSpread(func, start);
326 * Casts `func` to an rearged function if needed.
329 * @param {string} name The name of the function to inspect.
330 * @param {Function} func The function to inspect.
331 * @param {number} n The arity of `func`.
332 * @returns {Function} Returns the cast function.
334 function castRearg(name, func, n) {
335 return (config.rearg && n > 1 && (forceRearg || !mapping.skipRearg[name]))
336 ? rearg(func, mapping.methodRearg[name] || mapping.aryRearg[n])
341 * Creates a clone of `object` by `path`.
344 * @param {Object} object The object to clone.
345 * @param {Array|string} path The path to clone by.
346 * @returns {Object} Returns the cloned object.
348 function cloneByPath(object, path) {
352 length = path.length,
353 lastIndex = length - 1,
354 result = clone(Object(object)),
357 while (nested != null && ++index < length) {
358 var key = path[index],
362 !(isFunction(value) || isError(value) || isWeakMap(value))) {
363 nested[key] = clone(index == lastIndex ? value : Object(value));
365 nested = nested[key];
371 * Converts `lodash` to an immutable auto-curried iteratee-first data-last
372 * version with conversion `options` applied.
374 * @param {Object} [options] The options object. See `baseConvert` for more details.
375 * @returns {Function} Returns the converted `lodash`.
377 function convertLib(options) {
378 return _.runInContext.convert(options)(undefined);
382 * Create a converter function for `func` of `name`.
384 * @param {string} name The name of the function to convert.
385 * @param {Function} func The function to convert.
386 * @returns {Function} Returns the new converter function.
388 function createConverter(name, func) {
389 var realName = mapping.aliasToReal[name] || name,
390 methodName = mapping.remap[realName] || realName,
391 oldOptions = options;
393 return function(options) {
394 var newUtil = isLib ? pristine : helpers,
395 newFunc = isLib ? pristine[methodName] : func,
396 newOptions = assign(assign({}, oldOptions), options);
398 return baseConvert(newUtil, realName, newFunc, newOptions);
403 * Creates a function that wraps `func` to invoke its iteratee, with up to `n`
404 * arguments, ignoring any additional arguments.
407 * @param {Function} func The function to cap iteratee arguments for.
408 * @param {number} n The arity cap.
409 * @returns {Function} Returns the new function.
411 function iterateeAry(func, n) {
412 return overArg(func, function(func) {
413 return typeof func == 'function' ? baseAry(func, n) : func;
418 * Creates a function that wraps `func` to invoke its iteratee with arguments
419 * arranged according to the specified `indexes` where the argument value at
420 * the first index is provided as the first argument, the argument value at
421 * the second index is provided as the second argument, and so on.
424 * @param {Function} func The function to rearrange iteratee arguments for.
425 * @param {number[]} indexes The arranged argument indexes.
426 * @returns {Function} Returns the new function.
428 function iterateeRearg(func, indexes) {
429 return overArg(func, function(func) {
430 var n = indexes.length;
431 return baseArity(rearg(baseAry(func, n), indexes), n);
436 * Creates a function that invokes `func` with its first argument transformed.
439 * @param {Function} func The function to wrap.
440 * @param {Function} transform The argument transform.
441 * @returns {Function} Returns the new function.
443 function overArg(func, transform) {
445 var length = arguments.length;
449 var args = Array(length);
451 args[length] = arguments[length];
453 var index = config.rearg ? 0 : (length - 1);
454 args[index] = transform(args[index]);
455 return func.apply(undefined, args);
460 * Creates a function that wraps `func` and applys the conversions
464 * @param {string} name The name of the function to wrap.
465 * @param {Function} func The function to wrap.
466 * @returns {Function} Returns the converted function.
468 function wrap(name, func, placeholder) {
470 realName = mapping.aliasToReal[name] || name,
472 wrapper = wrappers[realName];
475 wrapped = wrapper(func);
477 else if (config.immutable) {
478 if (mapping.mutate.array[realName]) {
479 wrapped = wrapImmutable(func, cloneArray);
481 else if (mapping.mutate.object[realName]) {
482 wrapped = wrapImmutable(func, createCloner(func));
484 else if (mapping.mutate.set[realName]) {
485 wrapped = wrapImmutable(func, cloneByPath);
488 each(aryMethodKeys, function(aryKey) {
489 each(mapping.aryMethod[aryKey], function(otherName) {
490 if (realName == otherName) {
491 var data = mapping.methodSpread[realName],
492 afterRearg = data && data.afterRearg;
495 ? castFixed(realName, castRearg(realName, wrapped, aryKey), aryKey)
496 : castRearg(realName, castFixed(realName, wrapped, aryKey), aryKey);
498 result = castCap(realName, result);
499 result = castCurry(realName, result, aryKey);
506 result || (result = wrapped);
507 if (result == func) {
508 result = forceCurry ? curry(result, 1) : function() {
509 return func.apply(this, arguments);
512 result.convert = createConverter(realName, func);
513 result.placeholder = func.placeholder = placeholder;
518 /*--------------------------------------------------------------------------*/
521 return wrap(name, func, defaultHolder);
525 // Convert methods by ary cap.
527 each(aryMethodKeys, function(aryKey) {
528 each(mapping.aryMethod[aryKey], function(key) {
529 var func = _[mapping.remap[key] || key];
531 pairs.push([key, wrap(key, func, _)]);
536 // Convert remaining methods.
537 each(keys(_), function(key) {
539 if (typeof func == 'function') {
540 var length = pairs.length;
542 if (pairs[length][0] == key) {
546 func.convert = createConverter(key, func);
547 pairs.push([key, func]);
551 // Assign to `_` leaving `_.prototype` unchanged to allow chaining.
552 each(pairs, function(pair) {
553 _[pair[0]] = pair[1];
556 _.convert = convertLib;
560 each(keys(_), function(key) {
561 each(mapping.realToAlias[key] || [], function(alias) {
569 module.exports = baseConvert;