mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Allow _.mixin to accept a destination object as well as a source object.
Former-commit-id: 11ccb77653f017270c07579f59d75b847d4e6c65
This commit is contained in:
5
build.js
5
build.js
@@ -153,7 +153,7 @@
|
|||||||
'memoize': [],
|
'memoize': [],
|
||||||
'merge': ['createCallback', 'forEach', 'forOwn', 'getArray', 'isArray', 'isObject', 'isPlainObject', 'releaseArray'],
|
'merge': ['createCallback', 'forEach', 'forOwn', 'getArray', 'isArray', 'isObject', 'isPlainObject', 'releaseArray'],
|
||||||
'min': ['basicEach', 'charAtCallback', 'createCallback', 'isArray', 'isString'],
|
'min': ['basicEach', 'charAtCallback', 'createCallback', 'isArray', 'isString'],
|
||||||
'mixin': ['forEach', 'functions'],
|
'mixin': ['forEach', 'functions', 'isFunction'],
|
||||||
'noConflict': [],
|
'noConflict': [],
|
||||||
'omit': ['basicFlatten', 'createCallback', 'forIn', 'getIndexOf'],
|
'omit': ['basicFlatten', 'createCallback', 'forIn', 'getIndexOf'],
|
||||||
'once': [],
|
'once': [],
|
||||||
@@ -2716,6 +2716,9 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (_.contains(plusFuncs, 'chain') == !isUnderscore) {
|
||||||
|
funcDependencyMap.mixin = _.without(funcDependencyMap.mixin, 'isFunction');
|
||||||
|
}
|
||||||
if (isUnderscore) {
|
if (isUnderscore) {
|
||||||
if (!isLodash('clone') && !isLodash('cloneDeep')) {
|
if (!isLodash('clone') && !isLodash('cloneDeep')) {
|
||||||
funcDependencyMap.clone = _.without(funcDependencyMap.clone, 'forEach', 'forOwn');
|
funcDependencyMap.clone = _.without(funcDependencyMap.clone, 'forEach', 'forOwn');
|
||||||
|
|||||||
33
lodash.js
33
lodash.js
@@ -485,6 +485,7 @@
|
|||||||
/** Native method shortcuts */
|
/** Native method shortcuts */
|
||||||
var ceil = Math.ceil,
|
var ceil = Math.ceil,
|
||||||
clearTimeout = context.clearTimeout,
|
clearTimeout = context.clearTimeout,
|
||||||
|
defineProperty = reNative.test(defineProperty = Object.defineProperty) && defineProperty,
|
||||||
floor = Math.floor,
|
floor = Math.floor,
|
||||||
fnToString = Function.prototype.toString,
|
fnToString = Function.prototype.toString,
|
||||||
getPrototypeOf = reNative.test(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
|
getPrototypeOf = reNative.test(getPrototypeOf = Object.getPrototypeOf) && getPrototypeOf,
|
||||||
@@ -5223,20 +5224,26 @@
|
|||||||
* _('moe').capitalize();
|
* _('moe').capitalize();
|
||||||
* // => 'Moe'
|
* // => 'Moe'
|
||||||
*/
|
*/
|
||||||
function mixin(object) {
|
function mixin(object, source) {
|
||||||
forEach(functions(object), function(methodName) {
|
if (!source) {
|
||||||
var func = lodash[methodName] = object[methodName];
|
source = object;
|
||||||
|
object = lodash;
|
||||||
|
}
|
||||||
|
var isFunc = isFunction(object);
|
||||||
|
forEach(functions(source), function(methodName) {
|
||||||
|
var func = object[methodName] = source[methodName];
|
||||||
|
if (isFunc) {
|
||||||
|
object.prototype[methodName] = function() {
|
||||||
|
var value = this.__wrapped__,
|
||||||
|
args = [value];
|
||||||
|
|
||||||
lodash.prototype[methodName] = function() {
|
push.apply(args, arguments);
|
||||||
var value = this.__wrapped__,
|
var result = func.apply(object, args);
|
||||||
args = [value];
|
return (value && typeof value == 'object' && value === result)
|
||||||
|
? this
|
||||||
push.apply(args, arguments);
|
: new lodashWrapper(result);
|
||||||
var result = func.apply(lodash, args);
|
};
|
||||||
return (value && typeof value == 'object' && value === result)
|
}
|
||||||
? this
|
|
||||||
: new lodashWrapper(result);
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
18
test/test.js
18
test/test.js
@@ -2234,6 +2234,24 @@
|
|||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
|
QUnit.module('lodash.mixin');
|
||||||
|
|
||||||
|
(function() {
|
||||||
|
test('should accept an `object` argument', function() {
|
||||||
|
var lodash = {};
|
||||||
|
_.mixin(lodash, { 'a': function(a) { return a[0]; } });
|
||||||
|
strictEqual(lodash.a(['a']), 'a');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should accept a function `object` argument', function() {
|
||||||
|
var lodash = _.runInContext();
|
||||||
|
_.mixin(lodash, { 'a': function(a) { return a[0]; } });
|
||||||
|
strictEqual(lodash(['a']).a().value(), 'a');
|
||||||
|
});
|
||||||
|
}());
|
||||||
|
|
||||||
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|
||||||
QUnit.module('lodash.omit');
|
QUnit.module('lodash.omit');
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|||||||
Reference in New Issue
Block a user