Page MenuHomePhorge

D9677.1768804069.diff
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

D9677.1768804069.diff

diff --git a/lib/utils/objects.js b/lib/utils/objects.js
--- a/lib/utils/objects.js
+++ b/lib/utils/objects.js
@@ -132,6 +132,10 @@
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;
diff --git a/lib/utils/objects.test.js b/lib/utils/objects.test.js
--- a/lib/utils/objects.test.js
+++ b/lib/utils/objects.test.js
@@ -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', () => {
@@ -101,3 +101,55 @@
});
});
});
+
+// 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',
+ );
+ });
+});

File Metadata

Mime Type
text/plain
Expires
Mon, Jan 19, 6:27 AM (20 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5954856
Default Alt Text
D9677.1768804069.diff (2 KB)

Event Timeline