forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
30 lines (25 loc) · 683 Bytes
/
util.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
export type ICompareFunction<T> = (a: T, b: T) => number;
export type IEqualsFunction<T> = (a: T, b: T) => boolean;
export enum Compare {
LESS_THAN = -1,
BIGGER_THAN = 1
}
export function defaultCompare<T>(a: T, b: T): number {
if (a === b) {
return 0;
}
return a < b ? Compare.LESS_THAN : Compare.BIGGER_THAN;
}
export function defaultEquals<T>(a: T, b: T): boolean {
return a === b;
}
export function defaultToString(item: any): string {
if (item === null) {
return 'NULL';
} else if (item === undefined) {
return 'UNDEFINED';
} else if (typeof item === 'string' || item instanceof String) {
return `${item}`;
}
return item.toString();
}