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