Ensure _#plant resets iterator data of the cloned sequence.

This commit is contained in:
John-David Dalton
2015-08-31 18:47:37 -07:00
parent 3b131f0231
commit 2d8cc57642
2 changed files with 28 additions and 6 deletions

View File

@@ -1486,11 +1486,10 @@
* @private * @private
* @param {*} value The value to wrap. * @param {*} value The value to wrap.
* @param {boolean} [chainAll] Enable chaining for all wrapper methods. * @param {boolean} [chainAll] Enable chaining for all wrapper methods.
* @param {Array} [actions=[]] Actions to peform to resolve the unwrapped value.
*/ */
function LodashWrapper(value, chainAll, actions) { function LodashWrapper(value, chainAll) {
this.__wrapped__ = value; this.__wrapped__ = value;
this.__actions__ = actions || []; this.__actions__ = [];
this.__chain__ = !!chainAll; this.__chain__ = !!chainAll;
this.__index__ = 0; this.__index__ = 0;
this.__values__ = undefined; this.__values__ = undefined;
@@ -4347,9 +4346,14 @@
* @returns {Object} Returns the cloned wrapper. * @returns {Object} Returns the cloned wrapper.
*/ */
function wrapperClone(wrapper) { function wrapperClone(wrapper) {
return wrapper instanceof LazyWrapper if (wrapper instanceof LazyWrapper) {
? wrapper.clone() return wrapper.clone();
: new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__, copyArray(wrapper.__actions__)); }
var result = new LodashWrapper(wrapper.__wrapped__, wrapper.__chain__);
result.__actions__ = wrapper.__actions__;
result.__index__ = wrapper.__index__;
result.__values__ = wrapper.__values__;
return result;
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
@@ -5950,6 +5954,8 @@
while (parent instanceof baseLodash) { while (parent instanceof baseLodash) {
var clone = wrapperClone(parent); var clone = wrapperClone(parent);
clone.__index__ = 0;
clone.__values__ = undefined;
if (result) { if (result) {
previous.__wrapped__ = clone; previous.__wrapped__ = clone;
} else { } else {

View File

@@ -16637,6 +16637,22 @@
skipTest(2); skipTest(2);
} }
}); });
test('should reset iterator data on cloned sequences', 2, function() {
if (!isNpm) {
var array1 = [2, 4],
array2 = [6, 8],
wrapped1 = _(array1).map(square);
deepEqual(_.toArray(wrapped1), [4, 16]);
var wrapped2 = wrapped1.plant(array2);
deepEqual(_.toArray(wrapped2), [36, 64]);
}
else {
skipTest(2);
}
});
}()); }());
/*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/