-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text.ts
63 lines (49 loc) · 1.65 KB
/
text.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
import type { Option } from "./option.js";
import { Some } from "./option.js";
import type { TotalOrder } from "./order.js";
import type { Ordering } from "./ordering.js";
import type { Semigroup } from "./semigroup.js";
export class Text implements Semigroup<Text>, TotalOrder<Text> {
public constructor(public readonly value: string) {}
public static of(value: string): Text {
return new Text(value);
}
public toString(this: Text): string {
return `Text(${JSON.stringify(this.value)})`;
}
public append(this: Text, that: Text): Text {
return new Text(`${this.value}${that.value}`);
}
public isSame(this: Text, that: Text): boolean {
return this.value === that.value;
}
public isNotSame(this: Text, that: Text): boolean {
return this.value !== that.value;
}
public isLess(this: Text, that: Text): boolean {
return this.value < that.value;
}
public isNotLess(this: Text, that: Text): boolean {
return this.value >= that.value;
}
public isMore(this: Text, that: Text): boolean {
return this.value > that.value;
}
public isNotMore(this: Text, that: Text): boolean {
return this.value <= that.value;
}
public compare(this: Text, that: Text): Option<Ordering> {
if (this.value < that.value) return new Some("<");
if (this.value > that.value) return new Some(">");
return new Some("=");
}
public max(this: Text, that: Text): Text {
return this.value >= that.value ? this : that;
}
public min(this: Text, that: Text): Text {
return this.value <= that.value ? this : that;
}
public clamp(this: Text, lower: Text, upper: Text): Text {
return this.max(lower).min(upper);
}
}