-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathobject.ts
More file actions
102 lines (86 loc) · 2.55 KB
/
object.ts
File metadata and controls
102 lines (86 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { isEqual, mapValues, union } from 'lodash'
export const removeUndefinedProps = <T extends object>(obj: T): T => {
const newObj: any = {}
for (const key of Object.keys(obj)) {
if ((obj as any)[key] !== undefined) newObj[key] = (obj as any)[key]
}
return newObj
}
export const removeNullOrUndefinedProps = <T extends object>(
obj: T,
exceptions?: string[]
): T => {
const newObj: any = {}
for (const key of Object.keys(obj)) {
if (
((obj as any)[key] !== undefined && (obj as any)[key] !== null) ||
(exceptions ?? []).includes(key)
)
newObj[key] = (obj as any)[key]
}
return newObj
}
export const addObjects = <T extends { [key: string]: number }>(
obj1: T,
obj2: T
) => {
const keys = union(Object.keys(obj1), Object.keys(obj2))
const newObj = {} as any
for (const key of keys) {
newObj[key] = (obj1[key] ?? 0) + (obj2[key] ?? 0)
}
return newObj as T
}
export const subtractObjects = <T extends { [key: string]: number }>(
obj1: T,
obj2: T
) => {
const keys = union(Object.keys(obj1), Object.keys(obj2))
const newObj = {} as any
for (const key of keys) {
newObj[key] = (obj1[key] ?? 0) - (obj2[key] ?? 0)
}
return newObj as T
}
export const hasChanges = <T extends object>(obj: T, partial: Partial<T>) => {
const currValues = mapValues(partial, (_, key: keyof T) => obj[key])
return !isEqual(currValues, partial)
}
export const hasSignificantDeepChanges = <T extends object>(
obj: T,
partial: Partial<T>,
epsilonForNumbers: number
): boolean => {
const compareValues = (currValue: any, partialValue: any): boolean => {
if (typeof currValue === 'number' && typeof partialValue === 'number') {
return Math.abs(currValue - partialValue) > epsilonForNumbers
}
if (typeof currValue === 'object' && typeof partialValue === 'object') {
return hasSignificantDeepChanges(
currValue,
partialValue,
epsilonForNumbers
)
}
return !isEqual(currValue, partialValue)
}
for (const key in partial) {
if (Object.prototype.hasOwnProperty.call(partial, key)) {
if (compareValues(obj[key], partial[key])) {
return true
}
}
}
return false
}
export const filterObject = <T extends object>(obj: T, predicate: (value: any, key: keyof T) => boolean): { [P in keyof T]: T[P] } => {
const result = {} as { [P in keyof T]: T[P] }
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
if (predicate(obj[key], key)) {
result[key] = obj[key]
}
}
}
return result
}