diff options
Diffstat (limited to 'shared/core-utils/common/object.ts')
-rw-r--r-- | shared/core-utils/common/object.ts | 86 |
1 files changed, 0 insertions, 86 deletions
diff --git a/shared/core-utils/common/object.ts b/shared/core-utils/common/object.ts deleted file mode 100644 index 1276bfcc7..000000000 --- a/shared/core-utils/common/object.ts +++ /dev/null | |||
@@ -1,86 +0,0 @@ | |||
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 objectKeysTyped <O extends object, K extends keyof O> (object: O): K[] { | ||
27 | return (Object.keys(object) as K[]) | ||
28 | } | ||
29 | |||
30 | function getKeys <O extends object, K extends keyof O> (object: O, keys: K[]): K[] { | ||
31 | return (Object.keys(object) as K[]).filter(k => keys.includes(k)) | ||
32 | } | ||
33 | |||
34 | function hasKey <T extends object> (obj: T, k: keyof any): k is keyof T { | ||
35 | return k in obj | ||
36 | } | ||
37 | |||
38 | function sortObjectComparator (key: string, order: 'asc' | 'desc') { | ||
39 | return (a: any, b: any) => { | ||
40 | if (a[key] < b[key]) { | ||
41 | return order === 'asc' ? -1 : 1 | ||
42 | } | ||
43 | |||
44 | if (a[key] > b[key]) { | ||
45 | return order === 'asc' ? 1 : -1 | ||
46 | } | ||
47 | |||
48 | return 0 | ||
49 | } | ||
50 | } | ||
51 | |||
52 | function shallowCopy <T> (o: T): T { | ||
53 | return Object.assign(Object.create(Object.getPrototypeOf(o)), o) | ||
54 | } | ||
55 | |||
56 | function simpleObjectsDeepEqual (a: any, b: any) { | ||
57 | if (a === b) return true | ||
58 | |||
59 | if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) { | ||
60 | return false | ||
61 | } | ||
62 | |||
63 | const keysA = Object.keys(a) | ||
64 | const keysB = Object.keys(b) | ||
65 | |||
66 | if (keysA.length !== keysB.length) return false | ||
67 | |||
68 | for (const key of keysA) { | ||
69 | if (!keysB.includes(key)) return false | ||
70 | |||
71 | if (!simpleObjectsDeepEqual(a[key], b[key])) return false | ||
72 | } | ||
73 | |||
74 | return true | ||
75 | } | ||
76 | |||
77 | export { | ||
78 | pick, | ||
79 | omit, | ||
80 | objectKeysTyped, | ||
81 | getKeys, | ||
82 | hasKey, | ||
83 | shallowCopy, | ||
84 | sortObjectComparator, | ||
85 | simpleObjectsDeepEqual | ||
86 | } | ||