Make _.mixin chainable and make it assign to this instead of hard coding assignment to lodash.

This commit is contained in:
John-David Dalton
2014-04-30 00:11:45 -07:00
parent 179422a84b
commit 674802abe4
2 changed files with 86 additions and 40 deletions

View File

@@ -668,12 +668,12 @@
* `flatten`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`, * `flatten`, `forEach`, `forEachRight`, `forIn`, `forInRight`, `forOwn`,
* `forOwnRight`, `functions`, `groupBy`, `indexBy`, `initial`, `intersection`, * `forOwnRight`, `functions`, `groupBy`, `indexBy`, `initial`, `intersection`,
* `invert`, `invoke`, `keys`, `map`, `mapValues`, `matches`, `max`, `memoize`, * `invert`, `invoke`, `keys`, `map`, `mapValues`, `matches`, `max`, `memoize`,
* `merge`, `min`, `noop`, `object`, `omit`, `once`, `pairs`, `partial`, * `merge`, `min`, `mixin`, `noop`, `object`, `omit`, `once`, `pairs`,
* `partialRight`, `pick`, `pluck`, `property`, `pull`, `push`, `range`, * `partial`, `partialRight`, `pick`, `pluck`, `property`, `pull`, `push`,
* `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`, `sortBy`, * `range`, `reject`, `remove`, `rest`, `reverse`, `shuffle`, `slice`, `sort`,
* `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`, `union`, * `sortBy`, `splice`, `tap`, `throttle`, `times`, `toArray`, `transform`,
* `uniq`, `unshift`, `unzip`, `values`, `where`, `without`, `wrap`, `xor`, * `union`, `uniq`, `unshift`, `unzip`, `values`, `where`, `without`, `wrap`,
* and `zip` * `xor`, and `zip`
* *
* The non-chainable wrapper functions are: * The non-chainable wrapper functions are:
* `capitalize`, `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`, * `capitalize`, `clone`, `cloneDeep`, `contains`, `escape`, `every`, `find`,
@@ -681,10 +681,10 @@
* `identity`, `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`, * `identity`, `indexOf`, `isArguments`, `isArray`, `isBoolean`, `isDate`,
* `isElement`, `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`, * `isElement`, `isEmpty`, `isEqual`, `isFinite`, `isFunction`, `isNaN`,
* `isNull`, `isNumber`, `isObject`, `isPlainObject`, `isRegExp`, `isString`, * `isNull`, `isNumber`, `isObject`, `isPlainObject`, `isRegExp`, `isString`,
* `isUndefined`, `join`, `lastIndexOf`, `mixin`, `noConflict`, `now`, * `isUndefined`, `join`, `lastIndexOf`, `noConflict`, `now`, `parseInt`,
* `parseInt`, `pop`, `random`, `reduce`, `reduceRight`, `result`, `shift`, * `pop`, `random`, `reduce`, `reduceRight`, `result`, `shift`, `size`, `some`,
* `size`, `some`, `sortedIndex`, `runInContext`, `template`, `trim`, * `sortedIndex`, `runInContext`, `template`, `trim`, `trimLeft`, `trimRight`,
* `trimLeft`, `trimRight`, `unescape`, `uniqueId`, and `value` * `unescape`, `uniqueId`, and `value`
* *
* The wrapper functions `first`, `last`, and `sample` return wrapped values * The wrapper functions `first`, `last`, and `sample` return wrapped values
* when `n` is provided, otherwise they return unwrapped values. * when `n` is provided, otherwise they return unwrapped values.
@@ -7882,11 +7882,12 @@
* @static * @static
* @memberOf _ * @memberOf _
* @category Utilities * @category Utilities
* @param {Function|Object} [object=lodash] object The destination object. * @param {Function|Object} [object=this] object The destination object.
* @param {Object} source The object of functions to add. * @param {Object} source The object of functions to add.
* @param {Object} [options] The options object. * @param {Object} [options] The options object.
* @param {boolean} [options.chain=true] Specify whether the functions added * @param {boolean} [options.chain=true] Specify whether the functions added
* are chainable. * are chainable.
* @returns {Array|Object|string} Returns `object`.
* @example * @example
* *
* function vowels(string) { * function vowels(string) {
@@ -7915,7 +7916,7 @@
options = source; options = source;
} }
source = object; source = object;
object = lodash; object = this;
methodNames = functions(source); methodNames = functions(source);
} }
if (options === false) { if (options === false) {
@@ -7952,6 +7953,7 @@
}(func)); }(func));
} }
} }
return object;
} }
/** /**
@@ -8324,6 +8326,7 @@
lodash.memoize = memoize; lodash.memoize = memoize;
lodash.merge = merge; lodash.merge = merge;
lodash.min = min; lodash.min = min;
lodash.mixin = mixin;
lodash.negate = negate; lodash.negate = negate;
lodash.omit = omit; lodash.omit = omit;
lodash.once = once; lodash.once = once;
@@ -8372,7 +8375,7 @@
lodash.unzip = zip; lodash.unzip = zip;
// add functions to `lodash.prototype` // add functions to `lodash.prototype`
mixin(assign({}, lodash)); mixin(lodash, assign({}, lodash));
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -8416,7 +8419,6 @@
lodash.isUndefined = isUndefined; lodash.isUndefined = isUndefined;
lodash.kebabCase = kebabCase; lodash.kebabCase = kebabCase;
lodash.lastIndexOf = lastIndexOf; lodash.lastIndexOf = lastIndexOf;
lodash.mixin = mixin;
lodash.noConflict = noConflict; lodash.noConflict = noConflict;
lodash.noop = noop; lodash.noop = noop;
lodash.now = now; lodash.now = now;
@@ -8452,7 +8454,7 @@
lodash.include = contains; lodash.include = contains;
lodash.inject = reduce; lodash.inject = reduce;
mixin(function() { mixin(lodash, function() {
var source = {} var source = {}
baseForOwn(lodash, function(func, methodName) { baseForOwn(lodash, function(func, methodName) {
if (!lodash.prototype[methodName]) { if (!lodash.prototype[methodName]) {

View File

@@ -6369,15 +6369,36 @@
} }
var value = ['a'], var value = ['a'],
source = { 'a': function(array) { return array[0]; } }; source = { 'a': function(array) { return array[0]; }, 'b': 'B' };
test('should accept an `object` argument', 1, function() { test('should use `this` as the default `object` value', 3, function() {
var lodash = {}; var object = _.create(_);
_.mixin(lodash, source); object.mixin(source);
strictEqual(lodash.a(value), 'a');
strictEqual(object.a(value), 'a');
ok(!('a' in _));
ok(!('a' in _.prototype));
delete wrapper.a;
delete wrapper.prototype.a;
delete wrapper.b;
delete wrapper.prototype.b;
}); });
test('should accept a function `object` argument', 2, function() { test('should accept an `object` argument', 1, function() {
var object = {};
_.mixin(object, source);
strictEqual(object.a(value), 'a');
});
test('should return `object`', 2, function() {
var object = {};
strictEqual(_.mixin(object, source), object);
strictEqual(_.mixin(), _);
});
test('should work with a function for `object`', 2, function() {
_.mixin(wrapper, source); _.mixin(wrapper, source);
var wrapped = wrapper(value), var wrapped = wrapper(value),
@@ -6388,30 +6409,24 @@
delete wrapper.a; delete wrapper.a;
delete wrapper.prototype.a; delete wrapper.prototype.a;
delete wrapper.b;
delete wrapper.prototype.b;
}); });
test('should mixin `source` methods into lodash', 4, function() { test('should mixin `source` methods into lodash', 4, function() {
if (!isNpm) { _.mixin(source);
_.mixin({
'a': 'a',
'A': function(string) { return string.toUpperCase(); }
});
ok(!('a' in _)); strictEqual(_.a(value), 'a');
ok(!('a' in _.prototype)); strictEqual(_(value).a().__wrapped__, 'a');
delete _.a; delete _.a;
delete _.prototype.a; delete _.prototype.a;
strictEqual(_.A('a'), 'A'); ok(!('b' in _));
strictEqual(_('a').A().value(), 'A'); ok(!('b' in _.prototype));
delete _.A; delete _.b;
delete _.prototype.A; delete _.prototype.b;
}
else {
skipTest(4);
}
}); });
test('should accept an `options` argument', 16, function() { test('should accept an `options` argument', 16, function() {
@@ -6429,7 +6444,7 @@
var wrapped = func(value), var wrapped = func(value),
actual = wrapped.a(); actual = wrapped.a();
if (options && (options === true || options.chain)) { if (options === true || (options && options.chain)) {
strictEqual(actual.__wrapped__, 'a', message(func, true)); strictEqual(actual.__wrapped__, 'a', message(func, true));
ok(actual instanceof func, message(func, true)); ok(actual instanceof func, message(func, true));
} else { } else {
@@ -6438,6 +6453,8 @@
} }
delete func.a; delete func.a;
delete func.prototype.a; delete func.prototype.a;
delete func.b;
delete func.prototype.b;
}); });
}); });
}); });
@@ -6461,9 +6478,36 @@
} }
delete _.a; delete _.a;
delete _.prototype.a; delete _.prototype.a;
delete _.b;
delete _.prototype.b;
ok(pass); ok(pass);
}); });
test('should return the existing wrapper when chaining', 2, function() {
if (!isNpm) {
_.each([_, wrapper], function(func) {
if (func === _) {
var wrapper = _(source),
actual = wrapper.mixin();
strictEqual(actual.value(), _);
}
else {
wrapper = _(func);
actual = wrapper.mixin(source);
strictEqual(actual, wrapper);
}
delete func.a;
delete func.prototype.a;
delete func.b;
delete func.prototype.b;
});
}
else {
skipTest(2);
}
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/
@@ -7804,7 +7848,7 @@
deepEqual(args, [1, 0, array]); deepEqual(args, [1, 0, array]);
}); });
test('supports the `thisArg` argument', 1, function() { test('should support the `thisArg` argument', 1, function() {
var actual = _.rest(array, function(num, index) { var actual = _.rest(array, function(num, index) {
return this[index] < 3; return this[index] < 3;
}, array); }, array);