diff options
author | Raphael <mail@raphaelkabo.com> | 2024-02-26 15:06:01 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-26 15:06:01 +0000 |
commit | de688444d167fdb80c6e88b8ba837405ba7651a6 (patch) | |
tree | 051fa37a2fe30052254bf79a60bd870667fcc0b7 /src/util/object.ts | |
parent | afd9fc4477fff90e5db917f350d99c3d01fba2bd (diff) | |
parent | 1275280a9e3a31f6080079d564a8fb9e1847db8b (diff) |
Merge pull request #135 from lowercasename/rk/public-events
Events and groups optionally visible on front page
Diffstat (limited to 'src/util/object.ts')
-rw-r--r-- | src/util/object.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/util/object.ts b/src/util/object.ts new file mode 100644 index 0000000..1ecc89b --- /dev/null +++ b/src/util/object.ts @@ -0,0 +1,30 @@ +/** + * Simple object check. + */ +export function isObject(item: any) { + return item && typeof item === "object" && !Array.isArray(item); +} + +/** + * Deep merge two objects. + */ +export function deepMerge<T>( + target: Record<any, any>, + ...sources: Record<any, any>[] +): T { + if (!sources.length) return target; + const source = sources.shift(); + + if (isObject(target) && isObject(source)) { + for (const key in source) { + if (isObject(source[key])) { + if (!target[key]) Object.assign(target, { [key]: {} }); + deepMerge(target[key], source[key]); + } else { + Object.assign(target, { [key]: source[key] }); + } + } + } + + return deepMerge(target, ...sources) as T; +} |