|
1
|
/** |
|
2
|
* Pick properties from an object and copy them to a new object |
|
3
|
* @param obj |
|
4
|
* @param keys |
|
5
|
*/ |
|
6
|
export function pick(obj: Record<string, any>, keys: string[]) { |
|
7
|
const ret: Record<string, any> = {} |
|
8
|
|
|
9
|
keys.forEach((k) => { |
|
10
|
if (k in obj) { |
|
11
|
ret[k] = obj[k] |
|
12
|
} |
|
13
|
}) |
|
14
|
|
|
15
|
return ret |
|
16
|
} |
|
17
|
|
|
18
|
/** |
|
19
|
* Remove properties that are `null` or `undefined` from the object. |
|
20
|
* @param obj |
|
21
|
*/ |
|
22
|
export function cleanNonValues(obj: Record<string, any>) { |
|
23
|
const ret: Record<string, any> = {} |
|
24
|
|
|
25
|
for (const key in obj) { |
|
26
|
if (obj[key] !== null && typeof obj[key] !== "undefined") { |
|
27
|
ret[key] = obj[key] |
|
28
|
} |
|
29
|
} |
|
30
|
|
|
31
|
return ret |
|
32
|
} |
|
33
|
|
|
34
|
/** |
|
35
|
* Convert a string to camel case |
|
36
|
* @param str |
|
37
|
* @returns camel case string |
|
38
|
*/ |
|
39
|
export function toCamelCase(str: string): string { |
|
40
|
return /^([a-zA-Z]+)(([A-Z]([a-z]+))+)$/.test(str) |
|
41
|
? str |
|
42
|
: str |
|
43
|
.toLowerCase() |
|
44
|
.replace(/[^a-zA-Z0-9]+(.)/g, (_, chr) => chr.toUpperCase()) |
|
45
|
} |