00001 var arrayEach = require('./_arrayEach'), 00002 arrayPush = require('./_arrayPush'), 00003 baseFunctions = require('./_baseFunctions'), 00004 copyArray = require('./_copyArray'), 00005 isFunction = require('./isFunction'), 00006 isObject = require('./isObject'), 00007 keys = require('./keys'); 00008 00045 function mixin(object, source, options) { 00046 var props = keys(source), 00047 methodNames = baseFunctions(source, props); 00048 00049 var chain = !(isObject(options) && 'chain' in options) || !!options.chain, 00050 isFunc = isFunction(object); 00051 00052 arrayEach(methodNames, function(methodName) { 00053 var func = source[methodName]; 00054 object[methodName] = func; 00055 if (isFunc) { 00056 object.prototype[methodName] = function() { 00057 var chainAll = this.__chain__; 00058 if (chain || chainAll) { 00059 var result = object(this.__wrapped__), 00060 actions = result.__actions__ = copyArray(this.__actions__); 00061 00062 actions.push({ 'func': func, 'args': arguments, 'thisArg': object }); 00063 result.__chain__ = chainAll; 00064 return result; 00065 } 00066 return func.apply(object, arrayPush([this.value()], arguments)); 00067 }; 00068 } 00069 }); 00070 00071 return object; 00072 } 00073 00074 module.exports = mixin;