-
Notifications
You must be signed in to change notification settings - Fork 518
Expand file tree
/
Copy pathobject.ts
More file actions
121 lines (104 loc) · 3.12 KB
/
object.ts
File metadata and controls
121 lines (104 loc) · 3.12 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { isEqual, mapValues, union } from './lodash-replacements'
export const removeUndefinedProps = <T extends object>(
obj: T,
): {
[K in keyof T as T[K] extends undefined ? never : K]: Exclude<T[K], undefined>
} => {
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 as T, (_, key: keyof T) => obj[key])
return !isEqual(currValues, partial as any)
}
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 as any,
partialValue as any,
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
}
/**
* Asserts that a condition is true. If the condition is false, it throws an error with the provided message.
* @param condition The condition to check
* @param message The error message to display if the condition is false
* @throws {Error} If the condition is false
*/
export function assert(condition: boolean, message: string): asserts condition {
if (!condition) {
throw new Error(`Assertion failed: ${message}`)
}
}