Skip to content

Commit

Permalink
[lib] Add some unit tests for invertObjectToMap<K, V>(obj: { [K]: V })
Browse files Browse the repository at this point in the history
Summary:
Generated some basic unit tests for `invertObjectToMap` via GitHub Copilot.

One of the generated tests failed and surfaced possiblity that object entries could be "lost" if there were duplicate values... which is very often a possiblity. Added `invariant` to ensure values are unique and no entries are lost. Should be O(1) check so not worried about overhead.

Test Plan: Unit tests pass, made sure we had a test for `BigInt` values.

Reviewers: ashoat, ginsu, tomek, rohan

Reviewed By: ashoat

Subscribers: wyilio

Differential Revision: https://phab.comm.dev/D9677
  • Loading branch information
atulsmadhugiri committed Nov 3, 2023
1 parent 7483cae commit 0eb7a82
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/utils/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ function assertObjectsAreEqual<K, T>(
function invertObjectToMap<K, V>(obj: { +[K]: V }): Map<V, K> {
const invertedMap = new Map<V, K>();
for (const key of Object.keys(obj)) {
invariant(
!invertedMap.has(obj[key]),
`invertObjectToMap: obj[${key}] is already in invertedMap`,
);
invertedMap.set(obj[key], key);
}
return invertedMap;
Expand Down
54 changes: 53 additions & 1 deletion lib/utils/objects.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow

import { deepDiff } from './objects.js';
import { deepDiff, invertObjectToMap } from './objects.js';

describe('deepDiff tests', () => {
it('should return an empty object if the objects are identical', () => {
Expand Down Expand Up @@ -101,3 +101,55 @@ describe('deepDiff tests', () => {
});
});
});

// NOTE: `invertObjectToMap` unit tests were generated by GitHub Copilot.
describe('invertObjectToMap', () => {
it('should invert an object to a map', () => {
const obj = {
key1: 'value1',
key2: 'value2',
};
const map = new Map();
map.set('value1', 'key1');
map.set('value2', 'key2');
expect(invertObjectToMap(obj)).toEqual(map);
});

it('should invert an object with non-string keys to a map', () => {
const obj = {
key1: 1,
key2: 2,
};
const map = new Map();
map.set(1, 'key1');
map.set(2, 'key2');
expect(invertObjectToMap(obj)).toEqual(map);
});

it('should invert an object with BigInt values to map with BigInt keys', () => {
const obj = {
// $FlowIssue bigint-unsupported
key1: 1n,
// $FlowIssue bigint-unsupported
key2: 2n,
};
const map = new Map();
// $FlowIssue bigint-unsupported
map.set(1n, 'key1');
// $FlowIssue bigint-unsupported
map.set(2n, 'key2');
expect(invertObjectToMap(obj)).toEqual(map);
});

it('should invert an object with null values to map with null keys', () => {
const obj = {
key1: null,
key2: null,
};
const map = new Map();
map.set(null, 'key2');
expect(() => invertObjectToMap(obj)).toThrowError(
'invertObjectToMap: obj[key2] is already in invertedMap',
);
});
});

0 comments on commit 0eb7a82

Please sign in to comment.