1 var mapping = require('./_mapping'),
2 mutateMap = mapping.mutate,
3 fallbackHolder = require('./placeholder');
6 * Creates a function, with an arity of `n`, that invokes `func` with the
7 * arguments it receives.
10 * @param {Function} func The function to wrap.
11 * @param {number} n The arity of the new function.
12 * @returns {Function} Returns the new function.
14 function baseArity(func, n) {
16 ? function(a, b) { return func.apply(undefined, arguments); }
17 : function(a) { return func.apply(undefined, arguments); };
21 * Creates a function that invokes `func`, with up to `n` arguments, ignoring
22 * any additional arguments.
25 * @param {Function} func The function to cap arguments for.
26 * @param {number} n The arity cap.
27 * @returns {Function} Returns the new function.
29 function baseAry(func, n) {
31 ? function(a, b) { return func(a, b); }
32 : function(a) { return func(a); };
36 * Creates a clone of `array`.
39 * @param {Array} array The array to clone.
40 * @returns {Array} Returns the cloned array.
42 function cloneArray(array) {
43 var length = array ? array.length : 0,
44 result = Array(length);
47 result[length] = array[length];
53 * Creates a function that clones a given object using the assignment `func`.
56 * @param {Function} func The assignment function.
57 * @returns {Function} Returns the new cloner function.
59 function createCloner(func) {
60 return function(object) {
61 return func({}, object);
66 * Creates a function that wraps `func` and uses `cloner` to clone the first
67 * argument it receives.
70 * @param {Function} func The function to wrap.
71 * @param {Function} cloner The function to clone arguments.
72 * @returns {Function} Returns the new immutable function.
74 function immutWrap(func, cloner) {
76 var length = arguments.length;
80 var args = Array(length);
82 args[length] = arguments[length];
84 var result = args[0] = cloner.apply(undefined, args);
85 func.apply(undefined, args);
91 * The base implementation of `convert` which accepts a `util` object of methods
92 * required to perform conversions.
94 * @param {Object} util The util object.
95 * @param {string} name The name of the function to convert.
96 * @param {Function} func The function to convert.
97 * @param {Object} [options] The options object.
98 * @param {boolean} [options.cap=true] Specify capping iteratee arguments.
99 * @param {boolean} [options.curry=true] Specify currying.
100 * @param {boolean} [options.fixed=true] Specify fixed arity.
101 * @param {boolean} [options.immutable=true] Specify immutable operations.
102 * @param {boolean} [options.rearg=true] Specify rearranging arguments.
103 * @returns {Function|Object} Returns the converted function or object.
105 function baseConvert(util, name, func, options) {
107 isLib = typeof name == 'function',
108 isObj = name === Object(name);
118 options || (options = {});
121 'cap': 'cap' in options ? options.cap : true,
122 'curry': 'curry' in options ? options.curry : true,
123 'fixed': 'fixed' in options ? options.fixed : true,
124 'immutable': 'immutable' in options ? options.immutable : true,
125 'rearg': 'rearg' in options ? options.rearg : true
128 var forceCurry = ('curry' in options) && options.curry,
129 forceFixed = ('fixed' in options) && options.fixed,
130 forceRearg = ('rearg' in options) && options.rearg,
131 placeholder = isLib ? func : fallbackHolder,
132 pristine = isLib ? func.runInContext() : undefined;
134 var helpers = isLib ? func : {
136 'assign': util.assign,
139 'forEach': util.forEach,
140 'isArray': util.isArray,
141 'isFunction': util.isFunction,
142 'iteratee': util.iteratee,
145 'spread': util.spread,
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 toPath = helpers.toPath;
161 var aryMethodKeys = keys(mapping.aryMethod);
164 'castArray': function(castArray) {
166 var value = arguments[0];
167 return isArray(value)
168 ? castArray(cloneArray(value))
169 : castArray.apply(undefined, arguments);
172 'iteratee': function(iteratee) {
174 var func = arguments[0],
175 arity = arguments[1],
176 result = iteratee(func, arity),
177 length = result.length;
179 if (config.cap && typeof arity == 'number') {
180 arity = arity > 2 ? (arity - 2) : 1;
181 return (length && length <= arity) ? result : baseAry(result, arity);
186 'mixin': function(mixin) {
187 return function(source) {
189 if (!isFunction(func)) {
190 return mixin(func, Object(source));
193 each(keys(source), function(key) {
194 if (isFunction(source[key])) {
195 pairs.push([key, func.prototype[key]]);
199 mixin(func, Object(source));
201 each(pairs, function(pair) {
203 if (isFunction(value)) {
204 func.prototype[pair[0]] = value;
206 delete func.prototype[pair[0]];
212 'runInContext': function(runInContext) {
213 return function(context) {
214 return baseConvert(util, runInContext(context), options);
219 /*--------------------------------------------------------------------------*/
222 * Creates a clone of `object` by `path`.
225 * @param {Object} object The object to clone.
226 * @param {Array|string} path The path to clone by.
227 * @returns {Object} Returns the cloned object.
229 function cloneByPath(object, path) {
233 length = path.length,
234 lastIndex = length - 1,
235 result = clone(Object(object)),
238 while (nested != null && ++index < length) {
239 var key = path[index],
243 nested[path[index]] = clone(index == lastIndex ? value : Object(value));
245 nested = nested[key];
251 * Converts `lodash` to an immutable auto-curried iteratee-first data-last
252 * version with conversion `options` applied.
254 * @param {Object} [options] The options object. See `baseConvert` for more details.
255 * @returns {Function} Returns the converted `lodash`.
257 function convertLib(options) {
258 return _.runInContext.convert(options)(undefined);
262 * Create a converter function for `func` of `name`.
264 * @param {string} name The name of the function to convert.
265 * @param {Function} func The function to convert.
266 * @returns {Function} Returns the new converter function.
268 function createConverter(name, func) {
269 var oldOptions = options;
270 return function(options) {
271 var newUtil = isLib ? pristine : helpers,
272 newFunc = isLib ? pristine[name] : func,
273 newOptions = assign(assign({}, oldOptions), options);
275 return baseConvert(newUtil, name, newFunc, newOptions);
280 * Creates a function that wraps `func` to invoke its iteratee, with up to `n`
281 * arguments, ignoring any additional arguments.
284 * @param {Function} func The function to cap iteratee arguments for.
285 * @param {number} n The arity cap.
286 * @returns {Function} Returns the new function.
288 function iterateeAry(func, n) {
289 return overArg(func, function(func) {
290 return typeof func == 'function' ? baseAry(func, n) : func;
295 * Creates a function that wraps `func` to invoke its iteratee with arguments
296 * arranged according to the specified `indexes` where the argument value at
297 * the first index is provided as the first argument, the argument value at
298 * the second index is provided as the second argument, and so on.
301 * @param {Function} func The function to rearrange iteratee arguments for.
302 * @param {number[]} indexes The arranged argument indexes.
303 * @returns {Function} Returns the new function.
305 function iterateeRearg(func, indexes) {
306 return overArg(func, function(func) {
307 var n = indexes.length;
308 return baseArity(rearg(baseAry(func, n), indexes), n);
313 * Creates a function that invokes `func` with its first argument passed
317 * @param {Function} func The function to wrap.
318 * @param {...Function} transform The functions to transform the first argument.
319 * @returns {Function} Returns the new function.
321 function overArg(func, transform) {
323 var length = arguments.length;
327 var args = Array(length);
329 args[length] = arguments[length];
331 var index = config.rearg ? 0 : (length - 1);
332 args[index] = transform(args[index]);
333 return func.apply(undefined, args);
338 * Creates a function that wraps `func` and applys the conversions
342 * @param {string} name The name of the function to wrap.
343 * @param {Function} func The function to wrap.
344 * @returns {Function} Returns the converted function.
346 function wrap(name, func) {
347 name = mapping.aliasToReal[name] || name;
351 wrapper = wrappers[name];
354 wrapped = wrapper(func);
356 else if (config.immutable) {
357 if (mutateMap.array[name]) {
358 wrapped = immutWrap(func, cloneArray);
360 else if (mutateMap.object[name]) {
361 wrapped = immutWrap(func, createCloner(func));
363 else if (mutateMap.set[name]) {
364 wrapped = immutWrap(func, cloneByPath);
367 each(aryMethodKeys, function(aryKey) {
368 each(mapping.aryMethod[aryKey], function(otherName) {
369 if (name == otherName) {
370 var aryN = !isLib && mapping.iterateeAry[name],
371 reargIndexes = mapping.iterateeRearg[name],
372 spreadStart = mapping.methodSpread[name];
375 if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
376 result = spreadStart === undefined
377 ? ary(result, aryKey)
378 : spread(result, spreadStart);
380 if (config.rearg && aryKey > 1 && (forceRearg || !mapping.skipRearg[name])) {
381 result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[aryKey]);
385 result = iterateeRearg(result, reargIndexes);
387 result = iterateeAry(result, aryN);
390 if (forceCurry || (config.curry && aryKey > 1)) {
391 forceCurry && console.log(forceCurry, name);
392 result = curry(result, aryKey);
400 result || (result = wrapped);
401 if (result == func) {
402 result = forceCurry ? curry(result, 1) : function() {
403 return func.apply(this, arguments);
406 result.convert = createConverter(name, func);
407 if (mapping.placeholder[name]) {
408 setPlaceholder = true;
409 result.placeholder = func.placeholder = placeholder;
414 /*--------------------------------------------------------------------------*/
417 return wrap(name, func);
421 // Convert methods by ary cap.
423 each(aryMethodKeys, function(aryKey) {
424 each(mapping.aryMethod[aryKey], function(key) {
425 var func = _[mapping.remap[key] || key];
427 pairs.push([key, wrap(key, func)]);
432 // Convert remaining methods.
433 each(keys(_), function(key) {
435 if (typeof func == 'function') {
436 var length = pairs.length;
438 if (pairs[length][0] == key) {
442 func.convert = createConverter(key, func);
443 pairs.push([key, func]);
447 // Assign to `_` leaving `_.prototype` unchanged to allow chaining.
448 each(pairs, function(pair) {
449 _[pair[0]] = pair[1];
452 _.convert = convertLib;
453 if (setPlaceholder) {
454 _.placeholder = placeholder;
457 each(keys(_), function(key) {
458 each(mapping.realToAlias[key] || [], function(alias) {
466 module.exports = baseConvert;