]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/core-utils/common/object.ts
Fix server lint
[github/Chocobozzz/PeerTube.git] / shared / core-utils / common / object.ts
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 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
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
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,
46 omit,
47 getKeys,
48 sortObjectComparator
49 }