]>
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 | ||
13 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | |
14 | return (a: any, b: any) => { | |
15 | if (a[key] < b[key]) { | |
16 | return order === 'asc' ? -1 : 1 | |
17 | } | |
18 | ||
19 | if (a[key] > b[key]) { | |
20 | return order === 'asc' ? 1 : -1 | |
21 | } | |
22 | ||
23 | return 0 | |
24 | } | |
25 | } | |
26 | ||
27 | export { | |
28 | pick, | |
29 | sortObjectComparator | |
30 | } |