Fix lazy reverse and rest. [closes #890]

This commit is contained in:
jdalton
2015-01-28 02:45:58 -08:00
parent 13ee78a9c9
commit c888319f22
2 changed files with 82 additions and 12 deletions

View File

@@ -1216,11 +1216,14 @@
* @returns {Object} Returns the new reversed `LazyWrapper` object.
*/
function lazyReverse() {
var filtered = this.filtered,
result = filtered ? new LazyWrapper(this) : this.clone();
result.dir = this.dir * -1;
result.filtered = filtered;
if (this.filtered) {
var result = new LazyWrapper(this);
result.dir = -1;
result.filtered = true;
} else {
result = this.clone();
result.dir *= -1;
}
return result;
}
@@ -1239,12 +1242,12 @@
}
var dir = this.dir,
isRight = dir < 0,
length = array.length,
view = getView(0, length, this.views),
view = getView(0, array.length, this.views),
start = view.start,
end = view.end,
length = end - start,
dropCount = this.dropCount,
takeCount = nativeMin(end - start, this.takeCount - dropCount),
takeCount = nativeMin(length, this.takeCount - dropCount),
index = isRight ? end : start - 1,
iteratees = this.iteratees,
iterLength = iteratees ? iteratees.length : 0,
@@ -1280,7 +1283,7 @@
result[resIndex++] = value;
}
}
return isRight ? result.reverse() : result;
return result;
}
/*------------------------------------------------------------------------*/
@@ -5499,6 +5502,9 @@
function wrapperReverse() {
var value = this.__wrapped__;
if (value instanceof LazyWrapper) {
if (this.__actions__.length) {
value = new LazyWrapper(this);
}
return new LodashWrapper(value.reverse());
}
return this.thru(function(value) {
@@ -10902,7 +10908,8 @@
// Add `LazyWrapper` methods to `lodash.prototype`.
baseForOwn(LazyWrapper.prototype, function(func, methodName) {
var retUnwrapped = /^(?:first|last)$/.test(methodName);
var lodashFunc = lodash[methodName],
retUnwrapped = /^(?:first|last)$/.test(methodName);
lodash.prototype[methodName] = function() {
var value = this.__wrapped__,
@@ -10915,12 +10922,12 @@
if (retUnwrapped && !chainAll) {
return onlyLazy
? func.call(value)
: lodash[methodName](this.value());
: lodashFunc.call(lodash, this.value());
}
var interceptor = function(value) {
var otherArgs = [value];
push.apply(otherArgs, args);
return lodash[methodName].apply(lodash, otherArgs);
return lodashFunc.apply(lodash, otherArgs);
};
if (isLazy || isArray(value)) {
var wrapper = onlyLazy ? value : new LazyWrapper(this),

View File

@@ -5768,6 +5768,25 @@
skipTest(2);
}
});
test('should work in a lazy chain sequence', 2, function() {
if (!isNpm) {
var array = [1, 2, 3],
values = [];
var actual = _(array).initial().filter(function(value) {
values.push(value);
return false;
})
.value();
deepEqual(actual, []);
deepEqual(values, [1, 2]);
}
else {
skipTest(2);
}
});
}());
/*--------------------------------------------------------------------------*/
@@ -11271,6 +11290,25 @@
}
});
test('should work in a lazy chain sequence', 2, function() {
if (!isNpm) {
var array = [1, 2, 3],
values = [];
var actual = _(array).rest().filter(function(value) {
values.push(value);
return false;
})
.value();
deepEqual(actual, []);
deepEqual(values, [2, 3]);
}
else {
skipTest(2);
}
});
test('should be aliased', 1, function() {
strictEqual(_.tail, _.rest);
});
@@ -14200,6 +14238,16 @@
}
});
test('should work in a lazy chain sequence', 1, function() {
if (!isNpm) {
var actual = _([1, 2, 3, null]).map(_.identity).reverse().value();
deepEqual(actual, [null, 3, 2, 1]);
}
else {
skipTest();
}
});
test('should be lazy when in a lazy chain sequence', 2, function() {
if (!isNpm) {
var spy = {
@@ -14220,6 +14268,21 @@
skipTest(2);
}
});
test('should would in a hybrid chain sequence', 2, function() {
if (!isNpm) {
var array = [1, 2, 3, null],
actual = _(array).map(_.identity).compact().reverse().value();
deepEqual(actual, [3, 2, 1]);
actual = _([1, 2, 3, null]).map(_.identity).compact().pull(1).push(4).reverse().value()
deepEqual(actual, [4, 3, 2]);
}
else {
skipTest(2);
}
});
}());
/*--------------------------------------------------------------------------*/