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');
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.
13 * **Note:** Use `_.runInContext` to create a pristine `lodash` function to
14 * avoid conflicts caused by modifying the original.
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
24 * @returns {Function|Object} Returns `object`.
27 * function vowels(string) {
28 * return _.filter(string, function(v) {
29 * return /[aeiou]/i.test(v);
33 * _.mixin({ 'vowels': vowels });
37 * _('fred').vowels().value();
40 * _.mixin({ 'vowels': vowels }, { 'chain': false });
44 function mixin(object, source, options) {
45 var methodNames = baseFunctions(source, keys(source));
49 isFunc = isFunction(object),
50 length = methodNames.length;
52 if (options === false) {
54 } else if (isObject(options) && 'chain' in options) {
55 chain = options.chain;
57 while (++index < length) {
58 var methodName = methodNames[index],
59 func = source[methodName];
61 object[methodName] = func;
63 object.prototype[methodName] = (function(func) {
65 var chainAll = this.__chain__;
66 if (chain || chainAll) {
67 var result = object(this.__wrapped__),
68 actions = result.__actions__ = arrayCopy(this.__actions__);
70 actions.push({ 'func': func, 'args': arguments, 'thisArg': object });
71 result.__chain__ = chainAll;
74 return func.apply(object, arrayPush([this.value()], arguments));
82 module.exports = mixin;