Fix _.mixin creates functions that respect __chain__.

This commit is contained in:
John-David Dalton
2013-10-02 19:30:49 -07:00
parent f0f17e0041
commit d3dd97f167
9 changed files with 345 additions and 269 deletions

View File

@@ -206,8 +206,8 @@
/*--------------------------------------------------------------------------*/
/**
* Creates a `lodash` object which wraps the given value to enable method
* chaining.
* Creates a `lodash` object which wraps the given value to enable intuitive
* method chaining.
*
* In addition to Lo-Dash methods, wrappers also have the following `Array` methods:
* `concat`, `join`, `pop`, `push`, `reverse`, `shift`, `slice`, `sort`, `splice`,
@@ -241,6 +241,8 @@
* The wrapper functions `first` and `last` return wrapped values when `n` is
* provided, otherwise they return unwrapped values.
*
* Explicit chaining can be enabled by using the `_.chain` method.
*
* @name _
* @constructor
* @category Chaining
@@ -4146,11 +4148,9 @@
push.apply(args, arguments);
var result = func.apply(lodash, args);
if (this.__chain__) {
result = new lodashWrapper(result);
result.__chain__ = true;
}
return result;
return this.__chain__
? new lodashWrapper(result, true)
: result;
};
});
}
@@ -4467,7 +4467,8 @@
/*--------------------------------------------------------------------------*/
/**
* Creates a `lodash` object that wraps the given value.
* Creates a `lodash` object that wraps the given value with explicit
* method chaining enabled.
*
* @static
* @memberOf _
@@ -4483,9 +4484,10 @@
* ];
*
* var youngest = _.chain(stooges)
* .sortBy(function(stooge) { return stooge.age; })
* .sortBy('age')
* .map(function(stooge) { return stooge.name + ' is ' + stooge.age; })
* .first();
* .first()
* .value();
* // => 'moe is 40'
*/
function chain(value) {
@@ -4522,7 +4524,7 @@
}
/**
* Enables method chaining on the wrapper object.
* Enables explicit method chaining on the wrapper object.
*
* @name chain
* @memberOf _
@@ -4530,11 +4532,21 @@
* @returns {*} Returns the wrapper object.
* @example
*
* var sum = _([1, 2, 3])
* .chain()
* .reduce(function(sum, num) { return sum + num; })
* .value()
* // => 6`
* var stooges = [
* { 'name': 'moe', 'age': 40 },
* { 'name': 'larry', 'age': 50 }
* ];
*
* // without explicit chaining
* _(stooges).first();
* // => { 'name': 'moe', 'age': 40 }
*
* // with explicit chaining
* _(stooges).chain()
* .first()
* .pick('age')
* .value()
* // => { 'age': 40 }
*/
function wrapperChain() {
this.__chain__ = true;