-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_util.ts
44 lines (40 loc) · 1.31 KB
/
_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
34
35
36
37
38
39
40
41
42
43
44
import { isRecord } from "@core/unknownutil/is/record";
import { isArray } from "@core/unknownutil/is/array";
import { isString } from "@core/unknownutil/is/string";
import { toCamelCase } from "@std/text/to-camel-case";
import { toSnakeCase } from "@std/text/to-snake-case";
import { mapEntries } from "@std/collections/map-entries";
import { filterValues } from "@std/collections/filter-values";
import { mapValues } from "@std/collections/map-values";
export function toCamelCaseDeep<T>(v: T): T {
if (isRecord(v)) {
return mapEntries(
v,
([k, v]) => [isString(k) ? toCamelCase(k) : k, toCamelCaseDeep(v)],
) as T;
} else if (isArray(v)) {
return v.map(toCamelCaseDeep) as T;
}
return v;
}
export function toSnakeCaseDeep<T>(v: T): T {
if (isRecord(v)) {
return mapEntries(
v,
([k, v]) => [isString(k) ? toSnakeCase(k) : k, toSnakeCaseDeep(v)],
) as T;
} else if (isArray(v)) {
return v.map(toSnakeCaseDeep) as T;
}
return v;
}
export function compactValues<T>(
v: Record<string, T>,
): Record<string, NonNullable<T>> {
return filterValues(v, (v) => v != null) as Record<string, NonNullable<T>>;
}
export function stringifyValues<T extends { toString: () => string }>(
v: Record<string, T>,
): Record<string, string> {
return mapValues(v, (v) => v.toString());
}