Skip to content

Commit

Permalink
Add options object to _.mixin. [closes #404]
Browse files Browse the repository at this point in the history
  • Loading branch information
jdalton committed Nov 25, 2013
1 parent cbda784 commit c02b8ff
Show file tree
Hide file tree
Showing 8 changed files with 396 additions and 251 deletions.
84 changes: 49 additions & 35 deletions dist/lodash.compat.js
Expand Up @@ -3003,7 +3003,6 @@
if (!isObject(object)) {
return object;
}

// allows working with `_.reduce` and `_.reduceRight` without using
// their `index` and `collection` arguments
if (typeof args[2] != 'number') {
Expand Down Expand Up @@ -6196,51 +6195,71 @@
}

/**
* Adds function properties of a source object to the `lodash` function and
* chainable wrapper.
* Adds function properties of a source object to the destination object.
* If `object` is a function methods will be added to its prototype as well.
*
* @static
* @memberOf _
* @category Utilities
* @param {Object} object The object of function properties to add to `lodash`.
* @param {Object} object The object of function properties to add to `lodash`.
* @param {Function|Object} [object=lodash] object The destination object.
* @param {Object} source The object of functions to add.
* @param {Object} [options] The options object.
* @param {boolean} [options.chain=true] Specify whether the functions added are chainable.
* @example
*
* _.mixin({
* 'capitalize': function(string) {
* return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
* }
* });
* function capitalize(string) {
* return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
* }
*
* _.mixin({ 'capitalize': capitalize });
* _.capitalize('fred');
* // => 'Fred'
*
* _('fred').capitalize().value();
* // => 'Fred'
*
* _.mixin({ 'capitalize': capitalize }, { 'chain': false });
* _('fred').capitalize();
* // => 'Fred'
*/
function mixin(object, source) {
var ctor = object,
isFunc = !source || isFunction(ctor);
function mixin(object, source, options) {
var chain = true,
methodNames = source && functions(source);

if (!source) {
if (!source || (!options && !methodNames.length)) {
if (options == null) {
options = source;
}
ctor = lodashWrapper;
source = object;
object = lodash;
methodNames = functions(source);
}
forEach(functions(source), function(methodName) {
if (options === false) {
chain = false;
} else if (isObject(options) && 'chain' in options) {
chain = options.chain;
}
var ctor = object,
isFunc = isFunction(ctor);

forEach(methodNames, function(methodName) {
var func = object[methodName] = source[methodName];
if (isFunc) {
ctor.prototype[methodName] = function() {
var value = this.__wrapped__,
var chainAll = this.__chain__,
value = this.__wrapped__,
args = [value];

push.apply(args, arguments);
var result = func.apply(object, args);
if (value && typeof value == 'object' && value === result) {
return this;
if (chain || chainAll) {
if (value === result && isObject(result)) {
return this;
}
result = new ctor(result);
result.__chain__ = chainAll;
}
result = new ctor(result);
result.__chain__ = this.__chain__;
return result;
};
}
Expand Down Expand Up @@ -6976,20 +6995,15 @@
lodash.include = contains;
lodash.inject = reduce;

forOwn(lodash, function(func, methodName) {
if (!lodash.prototype[methodName]) {
lodash.prototype[methodName] = function() {
var args = [this.__wrapped__],
chainAll = this.__chain__;

push.apply(args, arguments);
var result = func.apply(lodash, args);
return chainAll
? new lodashWrapper(result, chainAll)
: result;
};
}
});
mixin(function() {
var source = {}
forOwn(lodash, function(func, methodName) {
if (!lodash.prototype[methodName]) {
source[methodName] = func;
}
});
return source;
}(), false);

/*--------------------------------------------------------------------------*/

Expand Down Expand Up @@ -7046,7 +7060,7 @@
};
});

// add `Array` functions that return the wrapped value
// add `Array` functions that return the existing wrapped value
baseEach(['push', 'reverse', 'sort', 'unshift'], function(methodName) {
var func = arrayRef[methodName];
lodash.prototype[methodName] = function() {
Expand Down
6 changes: 3 additions & 3 deletions dist/lodash.compat.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c02b8ff

Please sign in to comment.