-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathobject.ts
More file actions
136 lines (117 loc) · 3.65 KB
/
object.ts
File metadata and controls
136 lines (117 loc) · 3.65 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import isEqual from 'lodash/isEqual'
// Using native implementations instead of lodash to reduce bundle size
// isEqual is kept from lodash as it handles deep equality complex cases
type RemoveUndefined<T extends object> = {
[K in keyof T as T[K] extends undefined ? never : K]: Exclude<T[K], undefined>
}
export const removeUndefinedProps = <T extends object>(
obj: T,
): RemoveUndefined<T> => {
const newObj: Record<string, unknown> = {}
for (const key of Object.keys(obj)) {
const value = obj[key as keyof T]
if (value !== undefined) {
newObj[key] = value
}
}
return newObj as RemoveUndefined<T>
}
export const removeNullOrUndefinedProps = <T extends object>(
obj: T,
exceptions?: string[],
): T => {
const newObj: Record<string, unknown> = {}
for (const key of Object.keys(obj)) {
const value = obj[key as keyof T]
if (
(value !== undefined && value !== null) ||
(exceptions ?? []).includes(key)
) {
newObj[key] = value
}
}
return newObj as T
}
export const addObjects = <T extends { [key: string]: number }>(
obj1: T,
obj2: T,
): T => {
// Native implementation replaces lodash union
const keys = [...new Set([...Object.keys(obj1), ...Object.keys(obj2)])]
const newObj: { [key: string]: number } = {}
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,
): T => {
// Native implementation replaces lodash union
const keys = [...new Set([...Object.keys(obj1), ...Object.keys(obj2)])]
const newObj: { [key: string]: number } = {}
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>) => {
// Native implementation replaces lodash mapValues
const currValues = Object.fromEntries(
Object.keys(partial).map((key) => [key, obj[key as keyof T]])
)
return JSON.stringify(currValues) !== JSON.stringify(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
}
/**
* 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}`)
}
}