-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: provide meta data to custom merge functions and allow it to be …
…customized fix #33
1 parent
edae6c4
commit 18c05b6
Showing
9 changed files
with
627 additions
and
122 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,97 @@ | ||
import type { DeepMergeMergeFunctionsDefaults } from "@/deepmerge"; | ||
|
||
import type { DeepMergeBuiltInMetaData } from "./merging"; | ||
|
||
/** | ||
* The options the user can pass to customize deepmerge. | ||
*/ | ||
export type DeepMergeOptions = Partial<DeepMergeOptionsFull>; | ||
export type DeepMergeOptions< | ||
M, | ||
MM extends Partial<DeepMergeBuiltInMetaData> = DeepMergeBuiltInMetaData | ||
> = Partial<DeepMergeOptionsFull<M, MM>>; | ||
|
||
type MetaDataUpdater<M, MM extends Partial<DeepMergeBuiltInMetaData>> = ( | ||
previousMeta: M, | ||
metaMeta: MM | ||
) => M; | ||
|
||
/** | ||
* All the options the user can pass to customize deepmerge. | ||
*/ | ||
type DeepMergeOptionsFull = Readonly<{ | ||
mergeRecords: DeepMergeMergeFunctions["mergeRecords"] | false; | ||
mergeArrays: DeepMergeMergeFunctions["mergeArrays"] | false; | ||
mergeMaps: DeepMergeMergeFunctions["mergeMaps"] | false; | ||
mergeSets: DeepMergeMergeFunctions["mergeSets"] | false; | ||
mergeOthers: DeepMergeMergeFunctions["mergeOthers"]; | ||
type DeepMergeOptionsFull< | ||
M, | ||
MM extends Partial<DeepMergeBuiltInMetaData> | ||
> = Readonly<{ | ||
mergeRecords: DeepMergeMergeFunctions<M, MM>["mergeRecords"] | false; | ||
mergeArrays: DeepMergeMergeFunctions<M, MM>["mergeArrays"] | false; | ||
mergeMaps: DeepMergeMergeFunctions<M, MM>["mergeMaps"] | false; | ||
mergeSets: DeepMergeMergeFunctions<M, MM>["mergeSets"] | false; | ||
mergeOthers: DeepMergeMergeFunctions<M, MM>["mergeOthers"]; | ||
metaDataUpdater: MetaDataUpdater<M, MM>; | ||
}>; | ||
|
||
/** | ||
* All the merge functions that deepmerge uses. | ||
*/ | ||
type DeepMergeMergeFunctions = Readonly<{ | ||
type DeepMergeMergeFunctions< | ||
M, | ||
MM extends Partial<DeepMergeBuiltInMetaData> | ||
> = Readonly<{ | ||
mergeRecords: < | ||
Ts extends ReadonlyArray<Readonly<Record<PropertyKey, unknown>>>, | ||
U extends DeepMergeMergeFunctionUtils | ||
U extends DeepMergeMergeFunctionUtils<M, MM> | ||
>( | ||
records: Ts, | ||
utils: U | ||
utils: U, | ||
meta: M | ||
) => unknown; | ||
|
||
mergeArrays: < | ||
Ts extends ReadonlyArray<ReadonlyArray<unknown>>, | ||
U extends DeepMergeMergeFunctionUtils | ||
U extends DeepMergeMergeFunctionUtils<M, MM> | ||
>( | ||
records: Ts, | ||
utils: U | ||
utils: U, | ||
meta: M | ||
) => unknown; | ||
|
||
mergeMaps: < | ||
Ts extends ReadonlyArray<Readonly<ReadonlyMap<unknown, unknown>>>, | ||
U extends DeepMergeMergeFunctionUtils | ||
U extends DeepMergeMergeFunctionUtils<M, MM> | ||
>( | ||
records: Ts, | ||
utils: U | ||
utils: U, | ||
meta: M | ||
) => unknown; | ||
|
||
mergeSets: < | ||
Ts extends ReadonlyArray<Readonly<ReadonlySet<unknown>>>, | ||
U extends DeepMergeMergeFunctionUtils | ||
U extends DeepMergeMergeFunctionUtils<M, MM> | ||
>( | ||
records: Ts, | ||
utils: U | ||
utils: U, | ||
meta: M | ||
) => unknown; | ||
|
||
mergeOthers: < | ||
Ts extends ReadonlyArray<unknown>, | ||
U extends DeepMergeMergeFunctionUtils | ||
U extends DeepMergeMergeFunctionUtils<M, MM> | ||
>( | ||
records: Ts, | ||
utils: U | ||
utils: U, | ||
meta: M | ||
) => unknown; | ||
}>; | ||
|
||
/** | ||
* The utils provided to the merge functions. | ||
*/ | ||
export type DeepMergeMergeFunctionUtils = Readonly<{ | ||
mergeFunctions: DeepMergeMergeFunctions; | ||
export type DeepMergeMergeFunctionUtils< | ||
M, | ||
MM extends Partial<DeepMergeBuiltInMetaData> | ||
> = Readonly<{ | ||
mergeFunctions: DeepMergeMergeFunctions<M, MM>; | ||
defaultMergeFunctions: DeepMergeMergeFunctionsDefaults; | ||
metaDataUpdater: MetaDataUpdater<M, MM>; | ||
deepmerge: <Ts extends ReadonlyArray<unknown>>(...values: Ts) => unknown; | ||
}>; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export function areAllNumbers( | ||
values: ReadonlyArray<unknown> | ||
): values is ReadonlyArray<number> { | ||
return values.every((value) => typeof value === "number"); | ||
} | ||
|
||
export function hasProp<T, K extends PropertyKey>( | ||
value: T, | ||
prop: K | ||
): value is T & Record<K, unknown> { | ||
return typeof value === "object" && value !== null && prop in value; | ||
} |