Initial commit
[yaffs-website] / node_modules / grunt-contrib-watch / node_modules / lodash / utility / mixin.js
1 var arrayCopy = require('../internal/arrayCopy'),
2     arrayPush = require('../internal/arrayPush'),
3     baseFunctions = require('../internal/baseFunctions'),
4     isFunction = require('../lang/isFunction'),
5     isObject = require('../lang/isObject'),
6     keys = require('../object/keys');
7
8 /**
9  * Adds all own enumerable function properties of a source object to the
10  * destination object. If `object` is a function then methods are added to
11  * its prototype as well.
12  *
13  * **Note:** Use `_.runInContext` to create a pristine `lodash` function to
14  * avoid conflicts caused by modifying the original.
15  *
16  * @static
17  * @memberOf _
18  * @category Utility
19  * @param {Function|Object} [object=lodash] The destination object.
20  * @param {Object} source The object of functions to add.
21  * @param {Object} [options] The options object.
22  * @param {boolean} [options.chain=true] Specify whether the functions added
23  *  are chainable.
24  * @returns {Function|Object} Returns `object`.
25  * @example
26  *
27  * function vowels(string) {
28  *   return _.filter(string, function(v) {
29  *     return /[aeiou]/i.test(v);
30  *   });
31  * }
32  *
33  * _.mixin({ 'vowels': vowels });
34  * _.vowels('fred');
35  * // => ['e']
36  *
37  * _('fred').vowels().value();
38  * // => ['e']
39  *
40  * _.mixin({ 'vowels': vowels }, { 'chain': false });
41  * _('fred').vowels();
42  * // => ['e']
43  */
44 function mixin(object, source, options) {
45   var methodNames = baseFunctions(source, keys(source));
46
47   var chain = true,
48       index = -1,
49       isFunc = isFunction(object),
50       length = methodNames.length;
51
52   if (options === false) {
53     chain = false;
54   } else if (isObject(options) && 'chain' in options) {
55     chain = options.chain;
56   }
57   while (++index < length) {
58     var methodName = methodNames[index],
59         func = source[methodName];
60
61     object[methodName] = func;
62     if (isFunc) {
63       object.prototype[methodName] = (function(func) {
64         return function() {
65           var chainAll = this.__chain__;
66           if (chain || chainAll) {
67             var result = object(this.__wrapped__),
68                 actions = result.__actions__ = arrayCopy(this.__actions__);
69
70             actions.push({ 'func': func, 'args': arguments, 'thisArg': object });
71             result.__chain__ = chainAll;
72             return result;
73           }
74           return func.apply(object, arrayPush([this.value()], arguments));
75         };
76       }(func));
77     }
78   }
79   return object;
80 }
81
82 module.exports = mixin;