2022-05-22 08:36:05 +00:00
|
|
|
import { union } from 'lodash'
|
2022-03-15 22:27:51 +00:00
|
|
|
|
2022-02-17 23:00:19 +00:00
|
|
|
export const removeUndefinedProps = <T>(obj: T): T => {
|
2022-05-26 00:12:36 +00:00
|
|
|
const newObj: any = {}
|
2022-02-17 23:00:19 +00:00
|
|
|
|
2022-05-26 00:12:36 +00:00
|
|
|
for (const key of Object.keys(obj)) {
|
2022-02-17 23:00:19 +00:00
|
|
|
if ((obj as any)[key] !== undefined) newObj[key] = (obj as any)[key]
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObj
|
|
|
|
}
|
2022-03-15 22:27:51 +00:00
|
|
|
|
|
|
|
export const addObjects = <T extends { [key: string]: number }>(
|
|
|
|
obj1: T,
|
|
|
|
obj2: T
|
|
|
|
) => {
|
2022-05-22 08:36:05 +00:00
|
|
|
const keys = union(Object.keys(obj1), Object.keys(obj2))
|
2022-03-15 22:27:51 +00:00
|
|
|
const newObj = {} as any
|
|
|
|
|
2022-05-26 00:12:36 +00:00
|
|
|
for (const key of keys) {
|
2022-03-15 22:27:51 +00:00
|
|
|
newObj[key] = (obj1[key] ?? 0) + (obj2[key] ?? 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return newObj as T
|
|
|
|
}
|
2022-06-08 18:00:49 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|