Skip to content

Commit ce2b028

Browse files
authored
Fix issue with OrderedMap, OrderedSet and hashCode (#2005)
1 parent 77434b3 commit ce2b028

File tree

4 files changed

+22
-2
lines changed

4 files changed

+22
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Dates are formatted as YYYY-MM-DD.
1919
- `Collection.isAssociative`: use `isAssociative` directly
2020
- `Collection.isOrdered`: use `isOrdered` directly
2121

22+
- [BREAKING] Fix issue implementation of `hashCode` for `OrdererMap` and `OrderedSet` where equal objects might not return the same `hashCode` : [#2005](https://github.com/immutable-js/immutable-js/pull/2005)
23+
2224
- [Internal] Migrating TS type tests from dtslint to [TSTyche](https://tstyche.org/) [#1988](https://github.com/immutable-js/immutable-js/pull/1988) and [#1991](https://github.com/immutable-js/immutable-js/pull/1991) by [@mrazauskas](https://github.com/mrazauskas).
2325
Special thanks to [@arnfaldur](https://github.com/arnfaldur) that migrated every type tests to tsd just before that.
2426

__tests__/OrderedMap.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,13 @@ describe('OrderedMap', () => {
129129

130130
expect(map.size).toBe(SIZE / 2 - 1);
131131
});
132+
133+
it('hashCode should return the same value if the values are the same', () => {
134+
const m1 = OrderedMap({ b: 'b' });
135+
const m2 = OrderedMap({ a: 'a', b: 'b' }).remove('a');
136+
const m3 = OrderedMap({ b: 'b' }).remove('b').set('b', 'b');
137+
138+
expect(m1.hashCode()).toEqual(m2.hashCode());
139+
expect(m1.hashCode()).toEqual(m3.hashCode());
140+
});
132141
});

__tests__/OrderedSet.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,11 @@ describe('OrderedSet', () => {
142142
expect(out.has(second)).toBe(false);
143143
expect(out.has(third)).toBe(true);
144144
});
145+
146+
it('hashCode should return the same value if the values are the same', () => {
147+
const set1 = OrderedSet(['hello']);
148+
const set2 = OrderedSet(['goodbye', 'hello']).remove('goodbye');
149+
150+
expect(set1.hashCode()).toBe(set2.hashCode());
151+
});
145152
});

src/CollectionImpl.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ function hashCollection(collection) {
750750
const ordered = isOrdered(collection);
751751
const keyed = isKeyed(collection);
752752
let h = ordered ? 1 : 0;
753-
const size = collection.__iterate(
753+
754+
collection.__iterate(
754755
keyed
755756
? ordered
756757
? (v, k) => {
@@ -767,7 +768,8 @@ function hashCollection(collection) {
767768
h = (h + hash(v)) | 0;
768769
}
769770
);
770-
return murmurHashOfSize(size, h);
771+
772+
return murmurHashOfSize(collection.size, h);
771773
}
772774

773775
function murmurHashOfSize(size, h) {

0 commit comments

Comments
 (0)