8000 Merge pull request #2030 from alexvictoor/remove-after-bug · immutable-js/immutable-js@9548738 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9548738

Browse files
authored
Merge pull request #2030 from alexvictoor/remove-after-bug
2 parents 879f428 + 60fce4f commit 9548738

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

__tests__/List.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,33 @@ describe('List', () => {
722722
expect(o.get(0)).toBe('f');
723723
});
724724

725+
it('works with push, set and insert without phantom values', () => {
726+
const v = List.of().set(287, 287).push(42).insert(33, 33);
727+
expect(v.toJS().filter(item => item === 287)).toHaveLength(1);
728+
const v2 = List.of().push(0).unshift(-1).unshift(-2).pop().pop().set(2, 2);
729+
expect(v2.toJS()).toEqual([-2, undefined, 2]);
730+
const v3 = List.of().set(447, 447).push(0).insert(65, 65);
731+
expect(v3.toJS().filter(item => item === 447)).toHaveLength(1);
732+
const v4 = List.of().set(-28, -28).push(0).shift().set(-30, -30);
733+
expect(v4.toJS().filter(item => item === -28)).toHaveLength(0);
734+
const v5 = List.of().unshift(0).set(33, 33).shift().set(-35, -35);
735+
expect(v5.toJS().filter(item => item === 0)).toHaveLength(0);
736+
737+
// execute the same test as `v` but for the 2000 first integers
738+
const isOkV1 = v =>
739+
List.of()
740+
.set(v, v)
741+
.push('pushed-value')
742+
.insert(33, 'inserted-value')
743+
.filter(item => item === v).size === 1;
744+
745+
const arr = new Array(2000).fill(null).map((_, v) => v);
746+
747+
const notOkArray = arr.filter(v => !isOkV1(v));
748+
749+
expect(notOkArray).toHaveLength(0);
750+
});
751+
725752
// TODO: assert that findIndex only calls the function as much as it needs to.
726753

727754
it('forEach iterates in the correct order', () => {

src/List.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,10 @@ class VNode {
271271
// TODO: seems like these methods are very similar
272272

273273
removeBefore(ownerID, level, index) {
274-
if (index === level ? 1 << level : 0 || this.array.length === 0) {
274+
if (
275+
(index & ((1 << (level + SHIFT)) - 1)) === 0 ||
276+
this.array.length === 0
277+
) {
275278
return this;
276279
}
277280
const originIndex = (index >>> level) & MASK;
@@ -304,7 +307,10 @@ class VNode {
304307
}
305308

306309
removeAfter(ownerID, level, index) {
307-
if (index === (level ? 1 << level : 0) || this.array.length === 0) {
310+
if (
311+
index === (level ? 1 << (level + SHIFT) : SIZE) ||
312+
this.array.length === 0
313+
) {
308314
return this;
309315
}
310316
const sizeIndex = ((index - 1) >>> level) & MASK;

0 commit comments

Comments
 (0)
0