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));
195 each(keys(source), function(key) {
196 var value = source[key];
197 if (isFunction(value)) {
198 methodNames.push(key);
199 methods.push(func.prototype[key]);
203 mixin(func, Object(source));
205 each(methodNames, function(methodName, index) {
206 var method = methods[index];
207 if (isFunction(method)) {
208 func.prototype[methodName] = method;
210 delete func.prototype[methodName];
216 'runInContext': function(runInContext) {
217 return function(context) {
218 return baseConvert(util, runInContext(context), options);
223 /*--------------------------------------------------------------------------*/
226 * Creates a clone of `object` by `path`.
229 * @param {Object} object The object to clone.
230 * @param {Array|string} path The path to clone by.
231 * @returns {Object} Returns the cloned object.
233 function cloneByPath(object, path) {
237 length = path.length,
238 result = clone(Object(object)),
241 while (nested != null && ++index < length) {
242 var key = path[index],
246 nested[key] = clone(Object(value));
248 nested = nested[key];
254 * Converts `lodash` to an immutable auto-curried iteratee-first data-last
255 * version with conversion `options` applied.
257 * @param {Object} [options] The options object. See `baseConvert` for more details.
258 * @returns {Function} Returns the converted `lodash`.
260 function convertLib(options) {
261 return _.runInContext.convert(options)(undefined);
265 * Create a converter function for `func` of `name`.
267 * @param {string} name The name of the function to convert.
268 * @param {Function} func The function to convert.
269 * @returns {Function} Returns the new converter function.
271 function createConverter(name, func) {
272 var oldOptions = options;
273 return function(options) {
274 var newUtil = isLib ? pristine : helpers,
275 newFunc = isLib ? pristine[name] : func,
276 newOptions = assign(assign({}, oldOptions), options);
278 return baseConvert(newUtil, name, newFunc, newOptions);
283 * Creates a function that wraps `func` to invoke its iteratee, with up to `n`
284 * arguments, ignoring any additional arguments.
287 * @param {Function} func The function to cap iteratee arguments for.
288 * @param {number} n The arity cap.
289 * @returns {Function} Returns the new function.
291 function iterateeAry(func, n) {
292 return overArg(func, function(func) {
293 return typeof func == 'function' ? baseAry(func, n) : func;
298 * Creates a function that wraps `func` to invoke its iteratee with arguments
299 * arranged according to the specified `indexes` where the argument value at
300 * the first index is provided as the first argument, the argument value at
301 * the second index is provided as the second argument, and so on.
304 * @param {Function} func The function to rearrange iteratee arguments for.
305 * @param {number[]} indexes The arranged argument indexes.
306 * @returns {Function} Returns the new function.
308 function iterateeRearg(func, indexes) {
309 return overArg(func, function(func) {
310 var n = indexes.length;
311 return baseArity(rearg(baseAry(func, n), indexes), n);
316 * Creates a function that invokes `func` with its first argument passed
320 * @param {Function} func The function to wrap.
321 * @param {...Function} transform The functions to transform the first argument.
322 * @returns {Function} Returns the new function.
324 function overArg(func, transform) {
326 var length = arguments.length;
330 var args = Array(length);
332 args[length] = arguments[length];
334 var index = config.rearg ? 0 : (length - 1);
335 args[index] = transform(args[index]);
336 return func.apply(undefined, args);
341 * Creates a function that wraps `func` and applys the conversions
345 * @param {string} name The name of the function to wrap.
346 * @param {Function} func The function to wrap.
347 * @returns {Function} Returns the converted function.
349 function wrap(name, func) {
350 name = mapping.aliasToReal[name] || name;
354 wrapper = wrappers[name];
357 wrapped = wrapper(func);
359 else if (config.immutable) {
360 if (mutateMap.array[name]) {
361 wrapped = immutWrap(func, cloneArray);
363 else if (mutateMap.object[name]) {
364 wrapped = immutWrap(func, createCloner(func));
366 else if (mutateMap.set[name]) {
367 wrapped = immutWrap(func, cloneByPath);
370 each(aryMethodKeys, function(aryKey) {
371 each(mapping.aryMethod[aryKey], function(otherName) {
372 if (name == otherName) {
373 var aryN = !isLib && mapping.iterateeAry[name],
374 reargIndexes = mapping.iterateeRearg[name],
375 spreadStart = mapping.methodSpread[name];
378 if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
379 result = spreadStart === undefined
380 ? ary(result, aryKey)
381 : spread(result, spreadStart);
383 if (config.rearg && aryKey > 1 && (forceRearg || !mapping.skipRearg[name])) {
384 result = rearg(result, mapping.methodRearg[name] || mapping.aryRearg[aryKey]);
388 result = iterateeRearg(result, reargIndexes);
390 result = iterateeAry(result, aryN);
393 if (forceCurry || (config.curry && aryKey > 1)) {
394 forceCurry && console.log(forceCurry, name);
395 result = curry(result, aryKey);
403 result || (result = wrapped);
404 if (result == func) {
405 result = forceCurry ? curry(result, 1) : function() {
406 return func.apply(this, arguments);
409 result.convert = createConverter(name, func);
410 if (mapping.placeholder[name]) {
411 setPlaceholder = true;
412 result.placeholder = func.placeholder = placeholder;
417 /*--------------------------------------------------------------------------*/
420 return wrap(name, func);
424 // Convert methods by ary cap.
426 each(aryMethodKeys, function(aryKey) {
427 each(mapping.aryMethod[aryKey], function(key) {
428 var func = _[mapping.remap[key] || key];
430 pairs.push([key, wrap(key, func)]);
435 // Convert remaining methods.
436 each(keys(_), function(key) {
438 if (typeof func == 'function') {
439 var length = pairs.length;
441 if (pairs[length][0] == key) {
445 func.convert = createConverter(key, func);
446 pairs.push([key, func]);
450 // Assign to `_` leaving `_.prototype` unchanged to allow chaining.
451 each(pairs, function(pair) {
452 _[pair[0]] = pair[1];
455 _.convert = convertLib;
456 if (setPlaceholder) {
457 _.placeholder = placeholder;
460 each(keys(_), function(key) {
461 each(mapping.realToAlias[key] || [], function(alias) {
469 module.exports = baseConvert;