-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
double.ts
59 lines (46 loc) · 1.7 KB
/
double.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
import type { Option } from "./option.js";
import { None, Some } from "./option.js";
import type { TotalOrder } from "./order.js";
import type { Ordering } from "./ordering.js";
export class Double implements TotalOrder<Double> {
public constructor(public readonly value: number) {}
public static of(value: number): Double {
return new Double(value);
}
public toString(this: Double): string {
return `Double(${this.value})`;
}
public isSame(this: Double, that: Double): boolean {
return Object.is(this.value, that.value);
}
public isNotSame(this: Double, that: Double): boolean {
return !Object.is(this.value, that.value);
}
public isLess(this: Double, that: Double): boolean {
return this.value < that.value;
}
public isNotLess(this: Double, that: Double): boolean {
return this.value > that.value || Object.is(this.value, that.value);
}
public isMore(this: Double, that: Double): boolean {
return this.value > that.value;
}
public isNotMore(this: Double, that: Double): boolean {
return this.value < that.value || Object.is(this.value, that.value);
}
public compare(this: Double, that: Double): Option<Ordering> {
if (Object.is(this.value, that.value)) return new Some("=");
if (this.value < that.value) return new Some("<");
if (this.value > that.value) return new Some(">");
return None.instance;
}
public max(this: Double, that: Double): Double {
return new Double(Math.max(this.value, that.value));
}
public min(this: Double, that: Double): Double {
return new Double(Math.min(this.value, that.value));
}
public clamp(this: Double, lower: Double, upper: Double): Double {
return this.max(lower).min(upper);
}
}