Skip to content

Commit 7bc3b73

Browse files
committed
Clean up existing equality comparisons
1 parent dee77ce commit 7bc3b73

1 file changed

Lines changed: 27 additions & 17 deletions

File tree

src/compiler/core.ts

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -650,13 +650,13 @@ namespace ts {
650650
}
651651

652652
// TODO: fixme (N^2) - add optional comparer so collection can be sorted before deduplication.
653-
export function deduplicate<T>(array: ReadonlyArray<T>, areEqual?: (a: T, b: T) => boolean): T[] {
653+
export function deduplicate<T>(array: ReadonlyArray<T>, equalityComparer: (a: T, b: T) => boolean = equateValues): T[] {
654654
let result: T[];
655655
if (array) {
656656
result = [];
657657
loop: for (const item of array) {
658658
for (const res of result) {
659-
if (areEqual ? areEqual(res, item) : res === item) {
659+
if (equalityComparer(res, item)) {
660660
continue loop;
661661
}
662662
}
@@ -666,7 +666,7 @@ namespace ts {
666666
return result;
667667
}
668668

669-
export function arrayIsEqualTo<T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>, equaler?: (a: T, b: T) => boolean): boolean {
669+
export function arrayIsEqualTo<T>(array1: ReadonlyArray<T>, array2: ReadonlyArray<T>, equalityComparer: (a: T, b: T) => boolean = equateValues): boolean {
670670
if (!array1 || !array2) {
671671
return array1 === array2;
672672
}
@@ -676,8 +676,7 @@ namespace ts {
676676
}
677677

678678
for (let i = 0; i < array1.length; i++) {
679-
const equals = equaler ? equaler(array1[i], array2[i]) : array1[i] === array2[i];
680-
if (!equals) {
679+
if (!equalityComparer(array1[i], array2[i])) {
681680
return false;
682681
}
683682
}
@@ -916,6 +915,7 @@ namespace ts {
916915
}
917916

918917
export type Comparer<T> = (a: T, b: T) => Comparison;
918+
export type EqualityComparer<T> = (a: T, b: T) => boolean;
919919

920920
/**
921921
* Performs a binary search, finding the index at which 'value' occurs in 'array'.
@@ -1124,13 +1124,13 @@ namespace ts {
11241124
* @param left A map-like whose properties should be compared.
11251125
* @param right A map-like whose properties should be compared.
11261126
*/
1127-
export function equalOwnProperties<T>(left: MapLike<T>, right: MapLike<T>, equalityComparer?: (left: T, right: T) => boolean) {
1127+
export function equalOwnProperties<T>(left: MapLike<T>, right: MapLike<T>, equalityComparer: EqualityComparer<T> = equateValues) {
11281128
if (left === right) return true;
11291129
if (!left || !right) return false;
11301130
for (const key in left) {
11311131
if (hasOwnProperty.call(left, key)) {
11321132
if (!hasOwnProperty.call(right, key) === undefined) return false;
1133-
if (equalityComparer ? !equalityComparer(left[key], right[key]) : left[key] !== right[key]) return false;
1133+
if (!equalityComparer(left[key], right[key])) return false;
11341134
}
11351135
}
11361136

@@ -1483,16 +1483,26 @@ namespace ts {
14831483
return headChain;
14841484
}
14851485

1486+
/**
1487+
* Compare two values for their order relative to each other.
1488+
*/
14861489
export function compareValues<T>(a: T | undefined, b: T | undefined) {
14871490
if (a === b) return Comparison.EqualTo;
14881491
if (a === undefined) return Comparison.LessThan;
14891492
if (b === undefined) return Comparison.GreaterThan;
14901493
return a < b ? Comparison.LessThan : Comparison.GreaterThan;
14911494
}
14921495

1496+
/**
1497+
* Compare two values for their equality.
1498+
*/
1499+
export function equateValues<T>(a: T | undefined, b: T | undefined) {
1500+
return a === b;
1501+
}
1502+
14931503
interface StringCollator {
14941504
compare(a: string, b: string): number;
1495-
equals(a: string, b: string): boolean;
1505+
equate(a: string, b: string): boolean;
14961506
}
14971507

14981508
// Gets string comparers compatible with the current host
@@ -1506,7 +1516,7 @@ namespace ts {
15061516
// Intl.Collator.prototype.compare is bound to the collator. See NOTE in
15071517
// http://www.ecma-international.org/ecma-402/2.0/#sec-Intl.Collator.prototype.compare
15081518
compare: sortCollator.compare,
1509-
equals: (a, b) => searchCollator.compare(a, b) === 0
1519+
equate: (a, b) => searchCollator.compare(a, b) === 0
15101520
};
15111521
}
15121522

@@ -1516,7 +1526,7 @@ namespace ts {
15161526
// lowercase (such as ẞ).
15171527
return {
15181528
compare: (a, b) => a.toLocaleUpperCase().localeCompare(b.toLocaleUpperCase()),
1519-
equals: (a, b) => a.toLocaleUpperCase() === b.toLocaleUpperCase()
1529+
equate: (a, b) => a.toLocaleUpperCase() === b.toLocaleUpperCase()
15201530
};
15211531
}
15221532

@@ -1536,7 +1546,7 @@ namespace ts {
15361546
upperA > upperB ? Comparison.GreaterThan :
15371547
Comparison.EqualTo;
15381548
},
1539-
equals: (a, b) => a.toUpperCase() === b.toUpperCase()
1549+
equate: (a, b) => a.toUpperCase() === b.toUpperCase()
15401550
};
15411551
}
15421552

@@ -1597,14 +1607,14 @@ namespace ts {
15971607
return a === b
15981608
|| a !== undefined
15991609
&& b !== undefined
1600-
&& caseInsensitiveCollator.equals(a, b);
1610+
&& caseInsensitiveCollator.equate(a, b);
16011611
}
16021612

16031613
/**
16041614
* Performs a case-sensitive equality comparison between two strings.
16051615
*/
16061616
export function equateStringsCaseSensitive(a: string | undefined, b: string | undefined) {
1607-
return a === b;
1617+
return equateValues(a, b);
16081618
}
16091619

16101620
export function equateStrings(a: string | undefined, b: string | undefined, ignoreCase?: boolean) {
@@ -1993,9 +2003,9 @@ namespace ts {
19932003
const aComponents = getNormalizedPathComponents(a, currentDirectory);
19942004
const bComponents = getNormalizedPathComponents(b, currentDirectory);
19952005
const sharedLength = Math.min(aComponents.length, bComponents.length);
1996-
const stringComparer = ignoreCase ? compareStringsCaseInsensitive : compareStringsCaseSensitive;
2006+
const comparer = ignoreCase ? compareStringsCaseInsensitive : compareStringsCaseSensitive;
19972007
for (let i = 0; i < sharedLength; i++) {
1998-
const result = stringComparer(aComponents[i], bComponents[i]);
2008+
const result = comparer(aComponents[i], bComponents[i]);
19992009
if (result !== Comparison.EqualTo) {
20002010
return result;
20012011
}
@@ -2016,9 +2026,9 @@ namespace ts {
20162026
return false;
20172027
}
20182028

2019-
const stringEqualityComparer = ignoreCase ? equateStringsCaseInsensitive : equateStringsCaseSensitive;
2029+
const equalityComparer = ignoreCase ? equateStringsCaseInsensitive : equateStringsCaseSensitive;
20202030
for (let i = 0; i < parentComponents.length; i++) {
2021-
if (!stringEqualityComparer(parentComponents[i], childComponents[i])) {
2031+
if (!equalityComparer(parentComponents[i], childComponents[i])) {
20222032
return false;
20232033
}
20242034
}

0 commit comments

Comments
 (0)