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

View File

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