-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
66 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// https://stackoverflow.com/a/48218209 | ||
/** | ||
* Merges a series of objects into a single object, deeply. | ||
* @param objects The objects to merge (objects later in the list take precedence). | ||
* @returns The merged object. | ||
*/ | ||
export function deepMerge<T extends Object>(...objects: T[]): T { | ||
const isRecord = (obj: unknown): obj is Record<string, unknown> => !!obj && typeof obj === "object"; | ||
|
||
return objects.reduce<Record<string, unknown>>((prev, obj) => { | ||
Object.keys(obj).forEach((key) => { | ||
const pVal = prev[key]; | ||
const oVal = (obj as Record<string, unknown>)[key]; | ||
|
||
if (Array.isArray(pVal) && Array.isArray(oVal)) { | ||
prev[key] = pVal.concat(...oVal); | ||
} else if (isRecord(pVal) && isRecord(oVal)) { | ||
prev[key] = deepMerge(pVal, oVal); | ||
} else { | ||
prev[key] = oVal; | ||
} | ||
}); | ||
|
||
return prev; | ||
}, {}) as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters