8000 Fix issue with slice negative of filtered sequence (#2006) · immutable-js/immutable-js@23daf26 · GitHub
[go: up one dir, main page]

Skip to content

Commit 23daf26

Browse files
authored
Fix issue with slice negative of filtered sequence (#2006)
1 parent 493afba commit 23daf26

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

__tests__/Range.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,15 @@ describe('Range', () => {
190190

191191
expect(r).toEqual(200);
192192
});
193+
194+
it('sliced sequence works even on filtered sequence', () => {
195+
expect(Range(0, 3).slice(-2).toArray()).toEqual([1, 2]);
196+
197+
expect(
198+
Range(0, 3)
199+
.filter($ => true)
200+
.slice(-2)
201+
.toArray()
202+
).toEqual([1, 2]);
203+
});
193204
});

src/Operations.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -408,16 +408,16 @@ export function sliceFactory(collection, begin, end, useKeys) {
408408
return collection;
409409
}
410410

411-
const resolvedBegin = resolveBegin(begin, originalSize);
412-
const resolvedEnd = resolveEnd(end, originalSize);
413-
414-
// begin or end will be NaN if they were provided as negative numbers and
411+
// begin or end can not be resolved if they were provided as negative numbers and
415412
// this collection's size is unknown. In that case, cache first so there is
416413
// a known size and these do not resolve to NaN.
417-
if (resolvedBegin !== resolvedBegin || resolvedEnd !== resolvedEnd) {
414+
if (typeof originalSize === 'undefined' && (begin < 0 || end < 0)) {
418415
return sliceFactory(collection.toSeq().cacheResult(), begin, end, useKeys);
419416
}
420417

418+
const resolvedBegin = resolveBegin(begin, originalSize);
419+
const resolvedEnd = resolveEnd(end, originalSize);
420+
421421
// Note: resolvedEnd is undefined when the original sequence's length is
422422
// unknown and this slice did not supply an end and should contain all
423423
// elements after resolvedBegin.

0 commit comments

Comments
 (0)
0