]>
Commit | Line | Data |
---|---|---|
c55e3d72 C |
1 | function pick <O extends object, K extends keyof O> (object: O, keys: K[]): Pick<O, K> { |
2 | const result: any = {} | |
3 | ||
4 | for (const key of keys) { | |
5 | if (Object.prototype.hasOwnProperty.call(object, key)) { | |
6 | result[key] = object[key] | |
7 | } | |
8 | } | |
9 | ||
10 | return result | |
11 | } | |
12 | ||
bbd5aa7e C |
13 | function omit <O extends object, K extends keyof O> (object: O, keys: K[]): Exclude<O, K> { |
14 | const result: any = {} | |
15 | const keysSet = new Set(keys) as Set<string> | |
16 | ||
17 | for (const [ key, value ] of Object.entries(object)) { | |
18 | if (keysSet.has(key)) continue | |
19 | ||
20 | result[key] = value | |
21 | } | |
22 | ||
23 | return result | |
24 | } | |
25 | ||
0628157f C |
26 | function getKeys <O extends object, K extends keyof O> (object: O, keys: K[]): K[] { |
27 | return (Object.keys(object) as K[]).filter(k => keys.includes(k)) | |
28 | } | |
29 | ||
c55e3d72 C |
30 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { |
31 | return (a: any, b: any) => { | |
32 | if (a[key] < b[key]) { | |
33 | return order === 'asc' ? -1 : 1 | |
34 | } | |
35 | ||
36 | if (a[key] > b[key]) { | |
37 | return order === 'asc' ? 1 : -1 | |
38 | } | |
39 | ||
40 | return 0 | |
41 | } | |
42 | } | |
43 | ||
44 | export { | |
45 | pick, | |
bbd5aa7e | 46 | omit, |
0628157f | 47 | getKeys, |
c55e3d72 C |
48 | sortObjectComparator |
49 | } |