mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 10:57:49 +00:00
Fix lazy reverse and rest. [closes #890]
This commit is contained in:
@@ -1216,11 +1216,14 @@
|
|||||||
* @returns {Object} Returns the new reversed `LazyWrapper` object.
|
* @returns {Object} Returns the new reversed `LazyWrapper` object.
|
||||||
*/
|
*/
|
||||||
function lazyReverse() {
|
function lazyReverse() {
|
||||||
var filtered = this.filtered,
|
if (this.filtered) {
|
||||||
result = filtered ? new LazyWrapper(this) : this.clone();
|
var result = new LazyWrapper(this);
|
||||||
|
result.dir = -1;
|
||||||
result.dir = this.dir * -1;
|
result.filtered = true;
|
||||||
result.filtered = filtered;
|
} else {
|
||||||
|
result = this.clone();
|
||||||
|
result.dir *= -1;
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1239,12 +1242,12 @@
|
|||||||
}
|
}
|
||||||
var dir = this.dir,
|
var dir = this.dir,
|
||||||
isRight = dir < 0,
|
isRight = dir < 0,
|
||||||
length = array.length,
|
view = getView(0, array.length, this.views),
|
||||||
view = getView(0, length, this.views),
|
|
||||||
start = view.start,
|
start = view.start,
|
||||||
end = view.end,
|
end = view.end,
|
||||||
|
length = end - start,
|
||||||
dropCount = this.dropCount,
|
dropCount = this.dropCount,
|
||||||
takeCount = nativeMin(end - start, this.takeCount - dropCount),
|
takeCount = nativeMin(length, this.takeCount - dropCount),
|
||||||
index = isRight ? end : start - 1,
|
index = isRight ? end : start - 1,
|
||||||
iteratees = this.iteratees,
|
iteratees = this.iteratees,
|
||||||
iterLength = iteratees ? iteratees.length : 0,
|
iterLength = iteratees ? iteratees.length : 0,
|
||||||
@@ -1280,7 +1283,7 @@
|
|||||||
result[resIndex++] = value;
|
result[resIndex++] = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return isRight ? result.reverse() : result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*------------------------------------------------------------------------*/
|
/*------------------------------------------------------------------------*/
|
||||||
@@ -5499,6 +5502,9 @@
|
|||||||
function wrapperReverse() {
|
function wrapperReverse() {
|
||||||
var value = this.__wrapped__;
|
var value = this.__wrapped__;
|
||||||
if (value instanceof LazyWrapper) {
|
if (value instanceof LazyWrapper) {
|
||||||
|
if (this.__actions__.length) {
|
||||||
|
value = new LazyWrapper(this);
|
||||||
|
}
|
||||||
return new LodashWrapper(value.reverse());
|
return new LodashWrapper(value.reverse());
|
||||||
}
|
}
|
||||||
return this.thru(function(value) {
|
return this.thru(function(value) {
|
||||||
@@ -10902,7 +10908,8 @@
|
|||||||
|
|
||||||
// Add `LazyWrapper` methods to `lodash.prototype`.
|
// Add `LazyWrapper` methods to `lodash.prototype`.
|
||||||
baseForOwn(LazyWrapper.prototype, function(func, methodName) {
|
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() {
|
lodash.prototype[methodName] = function() {
|
||||||
var value = this.__wrapped__,
|
var value = this.__wrapped__,
|
||||||
@@ -10915,12 +10922,12 @@
|
|||||||
if (retUnwrapped && !chainAll) {
|
if (retUnwrapped && !chainAll) {
|
||||||
return onlyLazy
|
return onlyLazy
|
||||||
? func.call(value)
|
? func.call(value)
|
||||||
: lodash[methodName](this.value());
|
: lodashFunc.call(lodash, this.value());
|
||||||
}
|
}
|
||||||
var interceptor = function(value) {
|
var interceptor = function(value) {
|
||||||
var otherArgs = [value];
|
var otherArgs = [value];
|
||||||
push.apply(otherArgs, args);
|
push.apply(otherArgs, args);
|
||||||
return lodash[methodName].apply(lodash, otherArgs);
|
return lodashFunc.apply(lodash, otherArgs);
|
||||||
};
|
};
|
||||||
if (isLazy || isArray(value)) {
|
if (isLazy || isArray(value)) {
|
||||||
var wrapper = onlyLazy ? value : new LazyWrapper(this),
|
var wrapper = onlyLazy ? value : new LazyWrapper(this),
|
||||||
|
|||||||
63
test/test.js
63
test/test.js
@@ -5768,6 +5768,25 @@
|
|||||||
skipTest(2);
|
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() {
|
test('should be aliased', 1, function() {
|
||||||
strictEqual(_.tail, _.rest);
|
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() {
|
test('should be lazy when in a lazy chain sequence', 2, function() {
|
||||||
if (!isNpm) {
|
if (!isNpm) {
|
||||||
var spy = {
|
var spy = {
|
||||||
@@ -14220,6 +14268,21 @@
|
|||||||
skipTest(2);
|
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);
|
||||||
|
}
|
||||||
|
});
|
||||||
}());
|
}());
|
||||||
|
|
||||||
/*--------------------------------------------------------------------------*/
|
/*--------------------------------------------------------------------------*/
|
||||||
|
|||||||
Reference in New Issue
Block a user