mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Add options object to _.mixin. [closes #404]
This commit is contained in:
84
dist/lodash.compat.js
vendored
84
dist/lodash.compat.js
vendored
@@ -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') {
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user