/
comparators.ts
100 lines (91 loc) · 2.53 KB
/
comparators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
//================================================================
/**
* @packageDocumentation
* @module std
*/
//================================================================
import { IComparable } from "./IComparable";
import { get_uid } from "./uid";
/**
* Test whether two arguments are equal.
*
* @param x The first argument to compare.
* @param y The second argument to compare.
* @return Whether two arguments are equal or not.
*/
export function equal_to<T>(x: T, y: T): boolean
{
// CONVERT TO PRIMITIVE TYPE
x = x ? (x as any).valueOf() as T : x;
y = y ? (y as any).valueOf() as T : y;
// DO COMPARE
if (x instanceof Object && (<any>x as IComparable<T>).equals instanceof Function)
return (<any>x as IComparable<T>).equals(y);
else
return x === y;
}
/**
* Test whether two arguments are not equal.
*
* @param x The first argument to compare.
* @param y The second argument to compare.
* @return Returns `true`, if two arguments are not equal, otherwise `false`.
*/
export function not_equal_to<T>(x: T, y: T): boolean
{
return !equal_to(x, y);
}
/**
* Test whether *x* is less than *y*.
*
* @param x The first argument to compare.
* @param y The second argument to compare.
* @return Whether *x* is less than *y*.
*/
export function less<T>(x: T, y: T): boolean
{
// CONVERT TO PRIMITIVE TYPE
x = (x as any).valueOf() as T;
y = (y as any).valueOf() as T;
// DO COMPARE
if (x instanceof Object)
if ((<any>x as IComparable<T>).less instanceof Function) // has less()
return (<any>x as IComparable<T>).less(y);
else
return get_uid(<any>x) < get_uid(<any>y);
else
return x < y;
}
/**
* Test whether *x* is less than or equal to *y*.
*
* @param x The first argument to compare.
* @param y The second argument to compare.
* @return Whether *x* is less than or equal to *y*.
*/
export function less_equal<T>(x: T, y: T): boolean
{
return less(x, y) || equal_to(x, y);
}
/**
* Test whether *x* is greater than *y*.
*
* @param x The first argument to compare.
* @param y The second argument to compare.
* @return Whether *x* is greater than *y*.
*/
export function greater<T>(x: T, y: T): boolean
{
return !less_equal(x, y);
}
/**
* Test whether *x* is greater than or equal to *y*.
*
* @param x The first argument to compare.
* @param y The second argument to compare.
* @return Whether *x* is greater than or equal to *y*.
*/
export function greater_equal<T>(x: T, y: T): boolean
{
return !less(x, y);
}