8000 Fix issue with slice negative of filtered sequence by jdeniau · Pull Request #2006 · immutable-js/immutable-js · GitHub
[go: up one dir, main page]

Skip to content

Fix issue with slice negative of filtered sequence #2006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions __tests__/Range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,15 @@ describe('Range', () => {

expect(r).toEqual(200);
});

it('sliced sequence works even on filtered sequence', () => {
expect(Range(0, 3).slice(-2).toArray()).toEqual([1, 2]);

expect(
Range(0, 3)
.filter($ => true)
.slice(-2)
.toArray()
).toEqual([1, 2]);
});
});
10 changes: 5 additions & 5 deletions src/Operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,16 +408,16 @@ export function sliceFactory(collection, begin, end, useKeys) {
return collection;
}

const resolvedBegin = resolveBegin(begin, originalSize);
const resolvedEnd = resolveEnd(end, originalSize);

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

const resolvedBegin = resolveBegin(begin, originalSize);
const resolvedEnd = resolveEnd(end, originalSize);

// Note: resolvedEnd is undefined when the original sequence's length is
// unknown and this slice did not supply an end and should contain all
// elements after resolvedBegin.
Expand Down
Loading
0