-
-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathutils.ts
71 lines (60 loc) · 1.43 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
export function validDate(dt: Date | string | null | undefined): boolean {
if (!dt) {
return false;
}
// If it's a string, try to parse it
if (typeof dt === "string") {
if (dt.startsWith("0001")) {
return false;
}
const parsed = new Date(dt);
if (isNaN(parsed.getTime())) {
return false;
}
}
// If it's a date, check if it's valid
if (dt instanceof Date) {
if (dt.getFullYear() < 1000) {
return false;
}
}
return true;
}
export function fmtCurrency(value: number | string, currency = "USD", locale = "en-Us"): string {
if (typeof value === "string") {
value = parseFloat(value);
}
const formatter = new Intl.NumberFormat(locale, {
style: "currency",
currency,
minimumFractionDigits: 2,
});
return formatter.format(value);
}
export type MaybeUrlResult = {
isUrl: boolean;
url: string;
text: string;
};
export function maybeUrl(str: string): MaybeUrlResult {
const result: MaybeUrlResult = {
isUrl: str.startsWith("http://") || str.startsWith("https://"),
url: "",
text: "",
};
if (!result.isUrl && !str.startsWith("[")) {
return result;
}
if (str.startsWith("[")) {
const match = str.match(/\[(.*)\]\((.*)\)/);
if (match && match.length === 3) {
result.isUrl = true;
result.text = match[1];
result.url = match[2];
}
} else {
result.url = str;
result.text = "Link";
}
return result;
}