-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.ts
33 lines (29 loc) · 1002 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
31
32
33
import { type TimeComponents } from "https://esm.sh/[email protected]";
export const processTime = (str: string) =>
str
.trim()
.replace(/d|days/g, "day")
.replace(/m|min|minutes/g, "minute")
.replace(/s|sec|seconds/g, "second")
.replace(/h|hr|hours/g, "hour");
const plural = (u: number) => (u > 1 ? "s" : "");
export const formatTime = (time: TimeComponents) =>
`${time.days ? `${time.days} day${plural(time.days)} ` : ""}${
time.hours ? `${time.hours} hour${plural(time.hours)} ` : ""
}${time.minutes ? `${time.minutes} minute${plural(time.minutes)} ` : ""}${
time.seconds ? `${time.seconds} second${plural(time.seconds)} ` : ""
}`;
const escapables = {
"<": "<",
">": ">",
"&": "&",
"'": "'",
'"': """,
};
export const escapeHTML = (s: string) =>
s.replace(/<|>|&|"|'/g, (r) => escapables[r as keyof typeof escapables] || r);
export const dateFormatter = new Intl.DateTimeFormat("en-GB", {
dateStyle: "full",
timeStyle: "medium",
timeZone: "UTC",
});