1
>
/*---------------------------------------------------------------------------------------------
equals.ts
2
>
* Copyright (c) Microsoft Corporation. All rights reserved.
3
>
* Licensed under the MIT License. See License.txt in the project root for license information.
4
>
*--------------------------------------------------------------------------------------------*/
5
>
6
>
import * as arrays from './arrays.js';
7
>
8
>
/*
9
>
* Each function in this file which offers an equality comparison, has an accompanying
10
>
* `*C` variant which returns an EqualityComparer function.
11
>
*
12
>
* The `*C` variant allows for easier composition of equality comparers and improved type-inference.
13
>
*/
14
>
15
>
16
>
/** Represents a function that decides if two values are equal. */
17
>
export type EqualityComparer<T> = (a: T, b: T) => boolean;
18
>
19
>
export interface IEquatable<T> {
20
>
equals(other: T): boolean;
21
>
}
22
>
23
>
/**
24
>
* Compares two items for equality using strict equality.
25
>
*/
26
>
export function strictEquals<T>(a: T, b: T): boolean {
27
return a === b;
28
}
30
>
export function strictEqualsC<T>(): EqualityComparer<T> {
31
return (a, b) => a === b;
32
}
34
>
/**
35
>
* Checks if the items of two arrays are equal.
36
>
* By default, strict equality is used to compare elements, but a custom equality comparer can be provided.
37
>
*/
38
>
export function arrayEquals<T>(a: readonly T[], b: readonly T[], itemEquals?: EqualityComparer<T>): boolean {
39
return arrays.equals(a, b, itemEquals ?? strictEquals);
40
}
42
>
/**
43
>
* Checks if the items of two arrays are equal.
44
>
* By default, strict equality is used to compare elements, but a custom equality comparer can be provided.
45
>
*/
46
>
export function arrayEqualsC<T>(itemEquals?: EqualityComparer<T>): EqualityComparer<readonly T[]> {
47
return (a, b) => arrays.equals(a, b, itemEquals ?? strictEquals);
48
}
50
>
/**
51
>
* Drills into arrays (items ordered) and objects (keys unordered) and uses strict equality on everything else.
52
>
*/
53
>
export function structuralEquals<T>(a: T, b: T): boolean {
54
if (a === b) {
55
return true;