mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-14 12:47:49 +00:00
Ensure lazy drop when applied after filter works correctly. [closes #1026]
This commit is contained in:
@@ -1223,7 +1223,6 @@
|
|||||||
|
|
||||||
result.__actions__ = actions ? arrayCopy(actions) : null;
|
result.__actions__ = actions ? arrayCopy(actions) : null;
|
||||||
result.__dir__ = this.__dir__;
|
result.__dir__ = this.__dir__;
|
||||||
result.__dropCount__ = this.__dropCount__;
|
|
||||||
result.__filtered__ = this.__filtered__;
|
result.__filtered__ = this.__filtered__;
|
||||||
result.__iteratees__ = iteratees ? arrayCopy(iteratees) : null;
|
result.__iteratees__ = iteratees ? arrayCopy(iteratees) : null;
|
||||||
result.__takeCount__ = this.__takeCount__;
|
result.__takeCount__ = this.__takeCount__;
|
||||||
@@ -1270,9 +1269,8 @@
|
|||||||
start = view.start,
|
start = view.start,
|
||||||
end = view.end,
|
end = view.end,
|
||||||
length = end - start,
|
length = end - start,
|
||||||
dropCount = this.__dropCount__,
|
|
||||||
takeCount = nativeMin(length, this.__takeCount__),
|
|
||||||
index = isRight ? end : start - 1,
|
index = isRight ? end : start - 1,
|
||||||
|
takeCount = nativeMin(length, this.__takeCount__),
|
||||||
iteratees = this.__iteratees__,
|
iteratees = this.__iteratees__,
|
||||||
iterLength = iteratees ? iteratees.length : 0,
|
iterLength = iteratees ? iteratees.length : 0,
|
||||||
resIndex = 0,
|
resIndex = 0,
|
||||||
@@ -1290,28 +1288,32 @@
|
|||||||
iteratee = data.iteratee,
|
iteratee = data.iteratee,
|
||||||
type = data.type;
|
type = data.type;
|
||||||
|
|
||||||
if (type != LAZY_DROP_WHILE_FLAG) {
|
if (type == LAZY_DROP_WHILE_FLAG) {
|
||||||
var computed = iteratee(value);
|
if (data.done && (isRight ? index > data.index : index < data.index)) {
|
||||||
} else {
|
data.count = 0;
|
||||||
data.done = data.done && (isRight ? index < data.index : index > data.index);
|
data.done = false;
|
||||||
|
}
|
||||||
data.index = index;
|
data.index = index;
|
||||||
computed = data.done || (data.done = !iteratee(value));
|
if (!data.done) {
|
||||||
}
|
var limit = data.limit;
|
||||||
if (type == LAZY_MAP_FLAG) {
|
if (!(data.done = limit > -1 ? data.count++ >= limit : !iteratee(value))) {
|
||||||
value = computed;
|
continue outer;
|
||||||
} else if (!computed) {
|
}
|
||||||
if (type == LAZY_TAKE_WHILE_FLAG) {
|
}
|
||||||
break outer;
|
} else {
|
||||||
} else {
|
var computed = iteratee(value);
|
||||||
continue outer;
|
if (type == LAZY_MAP_FLAG) {
|
||||||
|
value = computed;
|
||||||
|
} else if (!computed) {
|
||||||
|
if (type == LAZY_FILTER_FLAG) {
|
||||||
|
continue outer;
|
||||||
|
} else {
|
||||||
|
break outer;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (dropCount) {
|
result[resIndex++] = value;
|
||||||
dropCount--;
|
|
||||||
} else {
|
|
||||||
result[resIndex++] = value;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -11580,24 +11582,35 @@
|
|||||||
result = (filtered && isDropWhile) ? new LazyWrapper(this) : this.clone(),
|
result = (filtered && isDropWhile) ? new LazyWrapper(this) : this.clone(),
|
||||||
iteratees = result.__iteratees__ || (result.__iteratees__ = []);
|
iteratees = result.__iteratees__ || (result.__iteratees__ = []);
|
||||||
|
|
||||||
|
iteratees.push({
|
||||||
|
'done': false,
|
||||||
|
'count': 0,
|
||||||
|
'index': 0,
|
||||||
|
'iteratee': getCallback(iteratee, thisArg, 1),
|
||||||
|
'limit': -1,
|
||||||
|
'type': type
|
||||||
|
});
|
||||||
|
|
||||||
result.__filtered__ = filtered || isFilter;
|
result.__filtered__ = filtered || isFilter;
|
||||||
iteratees.push({ 'done': false, 'index': 0, 'iteratee': getCallback(iteratee, thisArg, 1), 'type': type });
|
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add `LazyWrapper` methods for `_.drop` and `_.take` variants.
|
// Add `LazyWrapper` methods for `_.drop` and `_.take` variants.
|
||||||
arrayEach(['drop', 'take'], function(methodName, index) {
|
arrayEach(['drop', 'take'], function(methodName, index) {
|
||||||
var countName = '__' + methodName + 'Count__',
|
var whileName = methodName + 'While';
|
||||||
whileName = methodName + 'While';
|
|
||||||
|
|
||||||
LazyWrapper.prototype[methodName] = function(n) {
|
LazyWrapper.prototype[methodName] = function(n) {
|
||||||
n = n == null ? 1 : nativeMax(floor(n) || 0, 0);
|
var filtered = this.__filtered__,
|
||||||
|
result = (filtered && !index) ? this.dropWhile() : this.clone();
|
||||||
|
|
||||||
var result = this.clone();
|
n = n == null ? 1 : nativeMax(floor(n) || 0, 0);
|
||||||
if (result.__filtered__) {
|
if (filtered) {
|
||||||
var value = result[countName];
|
if (index) {
|
||||||
result[countName] = index ? nativeMin(value, n) : (value + n);
|
result.__takeCount__ = nativeMin(result.__takeCount__, n);
|
||||||
|
} else {
|
||||||
|
last(result.__iteratees__).limit = n;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
var views = result.__views__ || (result.__views__ = []);
|
var views = result.__views__ || (result.__views__ = []);
|
||||||
views.push({ 'size': n, 'type': methodName + (result.__dir__ < 0 ? 'Right' : '') });
|
views.push({ 'size': n, 'type': methodName + (result.__dir__ < 0 ? 'Right' : '') });
|
||||||
|
|||||||
21
test/test.js
21
test/test.js
@@ -12509,6 +12509,27 @@
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should not execute subsequent iteratees on an empty array in a lazy chain sequence', 4, function() {
|
||||||
|
if (!isNpm) {
|
||||||
|
var array = [1],
|
||||||
|
iteratee = function() { pass = false },
|
||||||
|
pass = true,
|
||||||
|
actual = _(array).rest().map(iteratee).value();
|
||||||
|
|
||||||
|
ok(pass);
|
||||||
|
deepEqual(actual, []);
|
||||||
|
|
||||||
|
pass = true;
|
||||||
|
actual = _(array).filter(_.identity).rest().map(iteratee).value();
|
||||||
|
|
||||||
|
ok(pass);
|
||||||
|
deepEqual(actual, []);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
skipTest(4);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
test('should be aliased', 1, function() {
|
test('should be aliased', 1, function() {
|
||||||
strictEqual(_.tail, _.rest);
|
strictEqual(_.tail, _.rest);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user