1 var mapping = require(
'./_mapping'),
2 fallbackHolder = require(
'./placeholder');
5 var push = Array.prototype.push;
16 function baseArity(func, n) {
18 ?
function(a, b) {
return func.apply(undefined, arguments); }
19 :
function(a) {
return func.apply(undefined, arguments); };
31 function baseAry(func, n) {
33 ?
function(a, b) {
return func(a, b); }
34 :
function(a) {
return func(a); };
44 function cloneArray(array) {
45 var length = array ? array.length : 0,
46 result = Array(length);
49 result[length] = array[length];
61 function createCloner(func) {
62 return function(object) {
63 return func({}, object);
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);
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);
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);
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);
297 function castCurry(name, func, n) {
298 return (forceCurry || (config.curry && n > 1))
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);
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])
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];
373 function convertLib(options) {
374 return _.runInContext.convert(options)(undefined);
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);
407 function iterateeAry(func, n) {
408 return overArg(func,
function(func) {
409 return typeof func ==
'function' ? baseAry(func, n) : func;
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);
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);
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;
519 return wrap(name, func);
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)]);
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]);
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;