-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.ts
233 lines (212 loc) · 4.69 KB
/
utils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* Types
-------------------------*/
export type None = undefined | null;
// deno-lint-ignore no-explicit-any
export type Forced = any;
/* Enums
-------------------------*/
export enum Resource {
Money,
Oil,
Coal,
Iron,
Bauxite,
Lead,
Uranium,
Food,
Gasoline,
Steel,
Aluminum,
Munitions,
Credits,
}
export enum Market {
Oil,
Coal,
Iron,
Bauxite,
Lead,
Uranium,
Food,
Gasoline,
Steel,
Aluminum,
Munitions,
Credits,
}
/* Functions
-------------------------*/
export function sleep(ms: number): Promise<true> {
return new Promise<true>((a) => setTimeout(() => a(true), ms));
}
export async function waitTilFalse(
func: () => boolean,
delay = 0,
): Promise<void> {
while (func()) {
await sleep(delay);
}
}
export function filterMap<T, U>(
array: T[],
func: (element: T, i: number) => U | None,
): U[] {
const input = [...array];
const output: U[] = [];
let i = 0;
for (const element of input) {
const result = func(element, i++);
if (result != undefined) {
output.push(result);
}
}
return output;
}
export function capitalise(text: string): string {
return filterMap(
text.split(" "),
(word) =>
word
? word[0].toLocaleUpperCase() +
word.slice(1).toLocaleLowerCase()
: null,
).join(" ");
}
export function abs(integer: bigint): bigint {
return integer < 0 ? integer * -1n : integer;
}
export function max(...integers: bigint[]): bigint {
let max = integers.shift() as bigint;
for (let i = 0; i < integers.length; ++i) {
if (max < integers[i]) {
max = integers[i];
}
}
return max;
}
export function min(...integers: bigint[]): bigint {
let min = integers.shift() as bigint;
for (let i = 0; i < integers.length; ++i) {
if (integers[i] < min) {
min = integers[i];
}
}
return min;
}
export function cusMax<T, U>(func: (value: T) => U, ...values: T[]): T {
let maxValue = values.shift() as T;
let maxResult = func(maxValue);
for (let i = 0; i < values.length; ++i) {
const result = func(values[i]);
if (maxResult < result) {
maxValue = values[i];
maxResult = result;
}
}
return maxValue;
}
export function cusMin<T, U>(func: (value: T) => U, ...values: T[]): T {
let minValue = values.shift() as T;
let minResult = func(minValue);
for (let i = 0; i < values.length; ++i) {
const result = func(values[i]);
if (result < minResult) {
minValue = values[i];
minResult = result;
}
}
return minValue;
}
export function uniqueRandomID(): string {
const char = "abcdefghijklmnopqrstuvwxyz";
let id: string;
do {
id = "";
for (let i = 0; i < 50; ++i) {
id += char[Math.floor(Math.random() * 26)];
}
} while (document.querySelector(`#${id}`));
return id;
}
export function endTime(startTime: number): string {
const endTime = performance.now();
return (endTime - startTime).toLocaleString("en-US", {
maximumFractionDigits: 2,
}) + "ms";
}
export function passIfTruthy<T>(x: T | None, func: (x: T) => void): None | T {
if (x) {
func(x);
}
return x;
}
// deno-lint-ignore no-explicit-any
export function pass<T>(x: T, func: (x: T) => any): T {
func(x);
return x;
}
export function wrap<T, U>(x: T, func: (x: T) => U): U {
return func(x);
}
export async function attemptPromise<T, U>(
func: () => Promise<T>,
// deno-lint-ignore no-explicit-any
error: ((e: any) => Promise<U>) | None = undefined,
): Promise<T | U | undefined> {
try {
return await func();
} catch (e) {
if (error != undefined) {
return await error(e);
}
console.error(e);
}
}
export function attempt<T, U>(
func: () => T,
// deno-lint-ignore no-explicit-any
error: ((e: any) => U) | None = undefined,
): T | U | undefined {
try {
return func();
} catch (e) {
if (error != undefined) {
return error(e);
}
console.error(e);
}
}
export function formatDate(date = new Date()): string {
let text = "";
text += date.getHours().toString().padStart(2, "0");
text += ":";
text += date.getMinutes().toString().padStart(2, "0");
text += " ";
text += date.getDate().toString().padStart(2, "0");
text += "/";
text += [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
][date.getMonth()];
text += "/";
text += date.getFullYear();
return text;
}
export function formatNumber(number: number, digits = 2): string {
return number.toLocaleString("en-US", { maximumFractionDigits: digits });
}
export function formatBigInt(x: bigint): string {
return `${(x / 100n).toLocaleString("en-US", { maximumFractionDigits: 0 })}.${
(x % 100n).toString().padStart(2, "0")
}`;
}