Skip to content

Fix issue with OrderedMap, OrderedSet and hashCode #2005

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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Dates are formatted as YYYY-MM-DD.
- `Collection.isAssociative`: use `isAssociative` directly
- `Collection.isOrdered`: use `isOrdered` directly

- [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)

- [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).
Special thanks to [@arnfaldur](https://github.com/arnfaldur) that migrated every type tests to tsd just before that.

Expand Down
9 changes: 9 additions & 0 deletions __tests__/OrderedMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,13 @@ describe('OrderedMap', () => {

expect(map.size).toBe(SIZE / 2 - 1);
});

it('hashCode should return the same value if the values are the same', () => {
const m1 = OrderedMap({ b: 'b' });
const m2 = OrderedMap({ a: 'a', b: 'b' }).remove('a');
const m3 = OrderedMap({ b: 'b' }).remove('b').set('b', 'b');

expect(m1.hashCode()).toEqual(m2.hashCode());
expect(m1.hashCode()).toEqual(m3.hashCode());
});
});
7 changes: 7 additions & 0 deletions __tests__/OrderedSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,11 @@ describe('OrderedSet', () => {
expect(out.has(second)).toBe(false);
expect(out.has(third)).toBe(true);
});

it('hashCode should return the same value if the values are the same', () => {
const set1 = OrderedSet(['hello']);
const set2 = OrderedSet(['goodbye', 'hello']).remove('goodbye');

expect(set1.hashCode()).toBe(set2.hashCode());
});
});
6 changes: 4 additions & 2 deletions src/CollectionImpl.js
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ function hashCollection(collection) {
const ordered = isOrdered(collection);
const keyed = isKeyed(collection);
let h = ordered ? 1 : 0;
const size = collection.__iterate(

collection.__iterate(
keyed
? ordered
? (v, k) => {
Expand All @@ -767,7 +768,8 @@ function hashCollection(collection) {
h = (h + hash(v)) | 0;
}
);
return murmurHashOfSize(size, h);

return murmurHashOfSize(collection.size, h);
}

function murmurHashOfSize(size, h) {
Expand Down
Loading