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
|
declare global {
interface Date {
toFileNameString(dateOnly?: boolean): string;
}
}
Object.defineProperty(Date.prototype, "toFileNameString", {
value: function (dateOnly?: boolean) {
const str = (this as Date).toISOString();
return dateOnly === true
? str.slice(0, str.indexOf("T"))
: str.replaceAll(/:|\./g, "-");
},
});
export function transformProperties<T extends object, N>(
object: T,
transformer: (value: T[keyof T], key: keyof T) => N,
): { [k in keyof T]: N } {
return Object.fromEntries(
Object.entries(object).map((
[k, v],
) => [k, transformer(v, k as keyof T)]),
) as { [k in keyof T]: N };
}
export function withDefaults<T extends object>(
partial: Partial<T> | null | undefined,
defaults: T,
): T {
const result: Record<PropertyKey, unknown> = {};
for (const [key, value] of Object.entries(defaults)) {
result[key] = partial?.[key as keyof T] ?? value;
}
return result as T;
}
|