Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / globule / node_modules / lodash / fp / _baseConvert.js
1 var mapping = require('./_mapping'),
2     fallbackHolder = require('./placeholder');
3
4 /**
5  * Creates a function, with an arity of `n`, that invokes `func` with the
6  * arguments it receives.
7  *
8  * @private
9  * @param {Function} func The function to wrap.
10  * @param {number} n The arity of the new function.
11  * @returns {Function} Returns the new function.
12  */
13 function baseArity(func, n) {
14   return n == 2
15     ? function(a, b) { return func.apply(undefined, arguments); }
16     : function(a) { return func.apply(undefined, arguments); };
17 }
18
19 /**
20  * Creates a function that invokes `func`, with up to `n` arguments, ignoring
21  * any additional arguments.
22  *
23  * @private
24  * @param {Function} func The function to cap arguments for.
25  * @param {number} n The arity cap.
26  * @returns {Function} Returns the new function.
27  */
28 function baseAry(func, n) {
29   return n == 2
30     ? function(a, b) { return func(a, b); }
31     : function(a) { return func(a); };
32 }
33
34 /**
35  * Creates a clone of `array`.
36  *
37  * @private
38  * @param {Array} array The array to clone.
39  * @returns {Array} Returns the cloned array.
40  */
41 function cloneArray(array) {
42   var length = array ? array.length : 0,
43       result = Array(length);
44
45   while (length--) {
46     result[length] = array[length];
47   }
48   return result;
49 }
50
51 /**
52  * Creates a function that clones a given object using the assignment `func`.
53  *
54  * @private
55  * @param {Function} func The assignment function.
56  * @returns {Function} Returns the new cloner function.
57  */
58 function createCloner(func) {
59   return function(object) {
60     return func({}, object);
61   };
62 }
63
64 /**
65  * Creates a function that wraps `func` and uses `cloner` to clone the first
66  * argument it receives.
67  *
68  * @private
69  * @param {Function} func The function to wrap.
70  * @param {Function} cloner The function to clone arguments.
71  * @returns {Function} Returns the new immutable function.
72  */
73 function wrapImmutable(func, cloner) {
74   return function() {
75     var length = arguments.length;
76     if (!length) {
77       return;
78     }
79     var args = Array(length);
80     while (length--) {
81       args[length] = arguments[length];
82     }
83     var result = args[0] = cloner.apply(undefined, args);
84     func.apply(undefined, args);
85     return result;
86   };
87 }
88
89 /**
90  * The base implementation of `convert` which accepts a `util` object of methods
91  * required to perform conversions.
92  *
93  * @param {Object} util The util object.
94  * @param {string} name The name of the function to convert.
95  * @param {Function} func The function to convert.
96  * @param {Object} [options] The options object.
97  * @param {boolean} [options.cap=true] Specify capping iteratee arguments.
98  * @param {boolean} [options.curry=true] Specify currying.
99  * @param {boolean} [options.fixed=true] Specify fixed arity.
100  * @param {boolean} [options.immutable=true] Specify immutable operations.
101  * @param {boolean} [options.rearg=true] Specify rearranging arguments.
102  * @returns {Function|Object} Returns the converted function or object.
103  */
104 function baseConvert(util, name, func, options) {
105   var setPlaceholder,
106       isLib = typeof name == 'function',
107       isObj = name === Object(name);
108
109   if (isObj) {
110     options = func;
111     func = name;
112     name = undefined;
113   }
114   if (func == null) {
115     throw new TypeError;
116   }
117   options || (options = {});
118
119   var config = {
120     'cap': 'cap' in options ? options.cap : true,
121     'curry': 'curry' in options ? options.curry : true,
122     'fixed': 'fixed' in options ? options.fixed : true,
123     'immutable': 'immutable' in options ? options.immutable : true,
124     'rearg': 'rearg' in options ? options.rearg : true
125   };
126
127   var forceCurry = ('curry' in options) && options.curry,
128       forceFixed = ('fixed' in options) && options.fixed,
129       forceRearg = ('rearg' in options) && options.rearg,
130       placeholder = isLib ? func : fallbackHolder,
131       pristine = isLib ? func.runInContext() : undefined;
132
133   var helpers = isLib ? func : {
134     'ary': util.ary,
135     'assign': util.assign,
136     'clone': util.clone,
137     'curry': util.curry,
138     'forEach': util.forEach,
139     'isArray': util.isArray,
140     'isFunction': util.isFunction,
141     'iteratee': util.iteratee,
142     'keys': util.keys,
143     'rearg': util.rearg,
144     'spread': util.spread,
145     'toInteger': util.toInteger,
146     'toPath': util.toPath
147   };
148
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,
156       keys = helpers.keys,
157       rearg = helpers.rearg,
158       spread = helpers.spread,
159       toInteger = helpers.toInteger,
160       toPath = helpers.toPath;
161
162   var aryMethodKeys = keys(mapping.aryMethod);
163
164   var wrappers = {
165     'castArray': function(castArray) {
166       return function() {
167         var value = arguments[0];
168         return isArray(value)
169           ? castArray(cloneArray(value))
170           : castArray.apply(undefined, arguments);
171       };
172     },
173     'iteratee': function(iteratee) {
174       return function() {
175         var func = arguments[0],
176             arity = arguments[1],
177             result = iteratee(func, arity),
178             length = result.length;
179
180         if (config.cap && typeof arity == 'number') {
181           arity = arity > 2 ? (arity - 2) : 1;
182           return (length && length <= arity) ? result : baseAry(result, arity);
183         }
184         return result;
185       };
186     },
187     'mixin': function(mixin) {
188       return function(source) {
189         var func = this;
190         if (!isFunction(func)) {
191           return mixin(func, Object(source));
192         }
193         var pairs = [];
194         each(keys(source), function(key) {
195           if (isFunction(source[key])) {
196             pairs.push([key, func.prototype[key]]);
197           }
198         });
199
200         mixin(func, Object(source));
201
202         each(pairs, function(pair) {
203           var value = pair[1];
204           if (isFunction(value)) {
205             func.prototype[pair[0]] = value;
206           } else {
207             delete func.prototype[pair[0]];
208           }
209         });
210         return func;
211       };
212     },
213     'nthArg': function(nthArg) {
214       return function(n) {
215         var arity = n < 0 ? 1 : (toInteger(n) + 1);
216         return curry(nthArg(n), arity);
217       };
218     },
219     'rearg': function(rearg) {
220       return function(func, indexes) {
221         var arity = indexes ? indexes.length : 0;
222         return curry(rearg(func, indexes), arity);
223       };
224     },
225     'runInContext': function(runInContext) {
226       return function(context) {
227         return baseConvert(util, runInContext(context), options);
228       };
229     }
230   };
231
232   /*--------------------------------------------------------------------------*/
233
234   /**
235    * Casts `func` to a function with an arity capped iteratee if needed.
236    *
237    * @private
238    * @param {string} name The name of the function to inspect.
239    * @param {Function} func The function to inspect.
240    * @returns {Function} Returns the cast function.
241    */
242   function castCap(name, func) {
243     if (config.cap) {
244       var indexes = mapping.iterateeRearg[name];
245       if (indexes) {
246         return iterateeRearg(func, indexes);
247       }
248       var n = !isLib && mapping.iterateeAry[name];
249       if (n) {
250         return iterateeAry(func, n);
251       }
252     }
253     return func;
254   }
255
256   /**
257    * Casts `func` to a curried function if needed.
258    *
259    * @private
260    * @param {string} name The name of the function to inspect.
261    * @param {Function} func The function to inspect.
262    * @param {number} n The arity of `func`.
263    * @returns {Function} Returns the cast function.
264    */
265   function castCurry(name, func, n) {
266     return (forceCurry || (config.curry && n > 1))
267       ? curry(func, n)
268       : func;
269   }
270
271   /**
272    * Casts `func` to a fixed arity function if needed.
273    *
274    * @private
275    * @param {string} name The name of the function to inspect.
276    * @param {Function} func The function to inspect.
277    * @param {number} n The arity cap.
278    * @returns {Function} Returns the cast function.
279    */
280   function castFixed(name, func, n) {
281     if (config.fixed && (forceFixed || !mapping.skipFixed[name])) {
282       var data = mapping.methodSpread[name],
283           start = data && data.start;
284
285       return start  === undefined ? ary(func, n) : spread(func, start);
286     }
287     return func;
288   }
289
290   /**
291    * Casts `func` to an rearged function if needed.
292    *
293    * @private
294    * @param {string} name The name of the function to inspect.
295    * @param {Function} func The function to inspect.
296    * @param {number} n The arity of `func`.
297    * @returns {Function} Returns the cast function.
298    */
299   function castRearg(name, func, n) {
300     return (config.rearg && n > 1 && (forceRearg || !mapping.skipRearg[name]))
301       ? rearg(func, mapping.methodRearg[name] || mapping.aryRearg[n])
302       : func;
303   }
304
305   /**
306    * Creates a clone of `object` by `path`.
307    *
308    * @private
309    * @param {Object} object The object to clone.
310    * @param {Array|string} path The path to clone by.
311    * @returns {Object} Returns the cloned object.
312    */
313   function cloneByPath(object, path) {
314     path = toPath(path);
315
316     var index = -1,
317         length = path.length,
318         lastIndex = length - 1,
319         result = clone(Object(object)),
320         nested = result;
321
322     while (nested != null && ++index < length) {
323       var key = path[index],
324           value = nested[key];
325
326       if (value != null) {
327         nested[path[index]] = clone(index == lastIndex ? value : Object(value));
328       }
329       nested = nested[key];
330     }
331     return result;
332   }
333
334   /**
335    * Converts `lodash` to an immutable auto-curried iteratee-first data-last
336    * version with conversion `options` applied.
337    *
338    * @param {Object} [options] The options object. See `baseConvert` for more details.
339    * @returns {Function} Returns the converted `lodash`.
340    */
341   function convertLib(options) {
342     return _.runInContext.convert(options)(undefined);
343   }
344
345   /**
346    * Create a converter function for `func` of `name`.
347    *
348    * @param {string} name The name of the function to convert.
349    * @param {Function} func The function to convert.
350    * @returns {Function} Returns the new converter function.
351    */
352   function createConverter(name, func) {
353     var realName = mapping.aliasToReal[name] || name,
354         methodName = mapping.remap[realName] || realName,
355         oldOptions = options;
356
357     return function(options) {
358       var newUtil = isLib ? pristine : helpers,
359           newFunc = isLib ? pristine[methodName] : func,
360           newOptions = assign(assign({}, oldOptions), options);
361
362       return baseConvert(newUtil, realName, newFunc, newOptions);
363     };
364   }
365
366   /**
367    * Creates a function that wraps `func` to invoke its iteratee, with up to `n`
368    * arguments, ignoring any additional arguments.
369    *
370    * @private
371    * @param {Function} func The function to cap iteratee arguments for.
372    * @param {number} n The arity cap.
373    * @returns {Function} Returns the new function.
374    */
375   function iterateeAry(func, n) {
376     return overArg(func, function(func) {
377       return typeof func == 'function' ? baseAry(func, n) : func;
378     });
379   }
380
381   /**
382    * Creates a function that wraps `func` to invoke its iteratee with arguments
383    * arranged according to the specified `indexes` where the argument value at
384    * the first index is provided as the first argument, the argument value at
385    * the second index is provided as the second argument, and so on.
386    *
387    * @private
388    * @param {Function} func The function to rearrange iteratee arguments for.
389    * @param {number[]} indexes The arranged argument indexes.
390    * @returns {Function} Returns the new function.
391    */
392   function iterateeRearg(func, indexes) {
393     return overArg(func, function(func) {
394       var n = indexes.length;
395       return baseArity(rearg(baseAry(func, n), indexes), n);
396     });
397   }
398
399   /**
400    * Creates a function that invokes `func` with its first argument transformed.
401    *
402    * @private
403    * @param {Function} func The function to wrap.
404    * @param {Function} transform The argument transform.
405    * @returns {Function} Returns the new function.
406    */
407   function overArg(func, transform) {
408     return function() {
409       var length = arguments.length;
410       if (!length) {
411         return func();
412       }
413       var args = Array(length);
414       while (length--) {
415         args[length] = arguments[length];
416       }
417       var index = config.rearg ? 0 : (length - 1);
418       args[index] = transform(args[index]);
419       return func.apply(undefined, args);
420     };
421   }
422
423   /**
424    * Creates a function that wraps `func` and applys the conversions
425    * rules by `name`.
426    *
427    * @private
428    * @param {string} name The name of the function to wrap.
429    * @param {Function} func The function to wrap.
430    * @returns {Function} Returns the converted function.
431    */
432   function wrap(name, func) {
433     var result,
434         realName = mapping.aliasToReal[name] || name,
435         wrapped = func,
436         wrapper = wrappers[realName];
437
438     if (wrapper) {
439       wrapped = wrapper(func);
440     }
441     else if (config.immutable) {
442       if (mapping.mutate.array[realName]) {
443         wrapped = wrapImmutable(func, cloneArray);
444       }
445       else if (mapping.mutate.object[realName]) {
446         wrapped = wrapImmutable(func, createCloner(func));
447       }
448       else if (mapping.mutate.set[realName]) {
449         wrapped = wrapImmutable(func, cloneByPath);
450       }
451     }
452     each(aryMethodKeys, function(aryKey) {
453       each(mapping.aryMethod[aryKey], function(otherName) {
454         if (realName == otherName) {
455           var spreadData = mapping.methodSpread[realName],
456               afterRearg = spreadData && spreadData.afterRearg;
457
458           result = afterRearg
459             ? castFixed(realName, castRearg(realName, wrapped, aryKey), aryKey)
460             : castRearg(realName, castFixed(realName, wrapped, aryKey), aryKey);
461
462           result = castCap(realName, result);
463           result = castCurry(realName, result, aryKey);
464           return false;
465         }
466       });
467       return !result;
468     });
469
470     result || (result = wrapped);
471     if (result == func) {
472       result = forceCurry ? curry(result, 1) : function() {
473         return func.apply(this, arguments);
474       };
475     }
476     result.convert = createConverter(realName, func);
477     if (mapping.placeholder[realName]) {
478       setPlaceholder = true;
479       result.placeholder = func.placeholder = placeholder;
480     }
481     return result;
482   }
483
484   /*--------------------------------------------------------------------------*/
485
486   if (!isObj) {
487     return wrap(name, func);
488   }
489   var _ = func;
490
491   // Convert methods by ary cap.
492   var pairs = [];
493   each(aryMethodKeys, function(aryKey) {
494     each(mapping.aryMethod[aryKey], function(key) {
495       var func = _[mapping.remap[key] || key];
496       if (func) {
497         pairs.push([key, wrap(key, func)]);
498       }
499     });
500   });
501
502   // Convert remaining methods.
503   each(keys(_), function(key) {
504     var func = _[key];
505     if (typeof func == 'function') {
506       var length = pairs.length;
507       while (length--) {
508         if (pairs[length][0] == key) {
509           return;
510         }
511       }
512       func.convert = createConverter(key, func);
513       pairs.push([key, func]);
514     }
515   });
516
517   // Assign to `_` leaving `_.prototype` unchanged to allow chaining.
518   each(pairs, function(pair) {
519     _[pair[0]] = pair[1];
520   });
521
522   _.convert = convertLib;
523   if (setPlaceholder) {
524     _.placeholder = placeholder;
525   }
526   // Assign aliases.
527   each(keys(_), function(key) {
528     each(mapping.realToAlias[key] || [], function(alias) {
529       _[alias] = _[key];
530     });
531   });
532
533   return _;
534 }
535
536 module.exports = baseConvert;