Skip to content

Commit 7fb6c78

Browse files
OverlappingFieldsCanBeMergedRule: refactor 'PairSet' (#3099)
1 parent b029ef8 commit 7fb6c78

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

src/validation/rules/OverlappingFieldsCanBeMergedRule.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -785,15 +785,16 @@ function subfieldConflicts(
785785
* not matter. We do this by maintaining a sort of double adjacency sets.
786786
*/
787787
class PairSet {
788-
_data: ObjMap<ObjMap<boolean>>;
788+
_data: Map<string, Map<string, boolean>>;
789789

790790
constructor() {
791-
this._data = Object.create(null);
791+
this._data = new Map();
792792
}
793793

794794
has(a: string, b: string, areMutuallyExclusive: boolean): boolean {
795-
const first = this._data[a];
796-
const result = first && first[b];
795+
const [key1, key2] = a < b ? [a, b] : [b, a];
796+
797+
const result = this._data.get(key1)?.get(key2);
797798
if (result === undefined) {
798799
return false;
799800
}
@@ -807,16 +808,13 @@ class PairSet {
807808
}
808809

809810
add(a: string, b: string, areMutuallyExclusive: boolean): void {
810-
this._pairSetAdd(a, b, areMutuallyExclusive);
811-
this._pairSetAdd(b, a, areMutuallyExclusive);
812-
}
811+
const [key1, key2] = a < b ? [a, b] : [b, a];
813812

814-
_pairSetAdd(a: string, b: string, areMutuallyExclusive: boolean): void {
815-
let map = this._data[a];
816-
if (!map) {
817-
map = Object.create(null);
818-
this._data[a] = map;
813+
const map = this._data.get(key1);
814+
if (map === undefined) {
815+
this._data.set(key1, new Map([[key2, areMutuallyExclusive]]));
816+
} else {
817+
map.set(key2, areMutuallyExclusive);
819818
}
820-
map[b] = areMutuallyExclusive;
821819
}
822820
}

0 commit comments

Comments
 (0)