mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 17:47:49 +00:00
Fix _.mixin creates functions that respect __chain__.
This commit is contained in:
45
lodash.js
45
lodash.js
@@ -586,8 +586,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`,
|
||||
@@ -621,6 +621,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
|
||||
@@ -5916,9 +5918,12 @@
|
||||
|
||||
push.apply(args, arguments);
|
||||
var result = func.apply(object, args);
|
||||
return (value && typeof value == 'object' && value === result)
|
||||
? this
|
||||
: new ctor(result);
|
||||
if (value && typeof value == 'object' && value === result) {
|
||||
return this;
|
||||
}
|
||||
result = new ctor(result);
|
||||
result.__chain__ = this.__chain__;
|
||||
return result;
|
||||
};
|
||||
}
|
||||
});
|
||||
@@ -6320,7 +6325,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 _
|
||||
@@ -6336,9 +6342,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) {
|
||||
@@ -6375,7 +6382,7 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables method chaining on the wrapper object.
|
||||
* Enables explicit method chaining on the wrapper object.
|
||||
*
|
||||
* @name chain
|
||||
* @memberOf _
|
||||
@@ -6383,11 +6390,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;
|
||||
|
||||
Reference in New Issue
Block a user