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');
7 /** Used for native method references. */
8 var arrayProto = Array.prototype;
10 /** Native method references. */
11 var push = arrayProto.push;
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.
18 * **Note:** Use `_.runInContext` to create a pristine `lodash` function to
19 * avoid conflicts caused by modifying the original.
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
29 * @returns {Function|Object} Returns `object`.
32 * function vowels(string) {
33 * return _.filter(string, function(v) {
34 * return /[aeiou]/i.test(v);
38 * // use `_.runInContext` to avoid conflicts (esp. in Node.js)
39 * var _ = require('lodash').runInContext();
41 * _.mixin({ 'vowels': vowels });
45 * _('fred').vowels().value();
48 * _.mixin({ 'vowels': vowels }, { 'chain': false });
52 function mixin(object, source, options) {
53 var methodNames = baseFunctions(source, keys(source));
57 isFunc = isFunction(object),
58 length = methodNames.length;
60 if (options === false) {
62 } else if (isObject(options) && 'chain' in options) {
63 chain = options.chain;
65 while (++index < length) {
66 var methodName = methodNames[index],
67 func = source[methodName];
69 object[methodName] = func;
71 object.prototype[methodName] = (function(func) {
73 var chainAll = this.__chain__;
74 if (chain || chainAll) {
75 var result = object(this.__wrapped__),
76 actions = result.__actions__ = arrayCopy(this.__actions__);
78 actions.push({ 'func': func, 'args': arguments, 'thisArg': object });
79 result.__chain__ = chainAll;
82 var args = [this.value()];
83 push.apply(args, arguments);
84 return func.apply(object, args);
92 module.exports = mixin;