Skip to content

Commit

Permalink
fix: improve meta data typings (#61)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: MetaMetaData now must extends DeepMergeBuiltInMetaData
  • Loading branch information
RebeccaStevens committed Feb 23, 2022
1 parent aa2540f commit 9a881d3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 17 deletions.
3 changes: 3 additions & 0 deletions docs/deepmergeCustom.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ const customizedDeepmerge = deepmergeCustom<
if (previousMeta === undefined) {
return { keyPath: [] };
}
if (metaMeta.key === undefined) {
return previousMeta;
}
return {
...metaMeta,
keyPath: [...previousMeta.keyPath, metaMeta.key],
Expand Down
10 changes: 4 additions & 6 deletions src/deepmerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,7 @@ export function deepmergeCustom<
export function deepmergeCustom<
PMF extends Partial<DeepMergeMergeFunctionsURIs>,
MetaData,
MetaMetaData extends Readonly<
Record<PropertyKey, unknown>
> = DeepMergeBuiltInMetaData
MetaMetaData extends DeepMergeBuiltInMetaData = DeepMergeBuiltInMetaData
>(
options: DeepMergeOptions<MetaData, MetaMetaData>,
rootMetaData?: MetaData
Expand All @@ -100,7 +98,7 @@ export function deepmergeCustom<
export function deepmergeCustom<
PMF extends Partial<DeepMergeMergeFunctionsURIs>,
MetaData,
MetaMetaData extends Readonly<Record<PropertyKey, unknown>>
MetaMetaData extends DeepMergeBuiltInMetaData
>(
options: DeepMergeOptions<MetaData, MetaMetaData>,
rootMetaData?: MetaData
Expand Down Expand Up @@ -140,7 +138,7 @@ export function deepmergeCustom<
*
* @param options - The options the user specified
*/
function getUtils<M, MM extends Readonly<Record<PropertyKey, unknown>>>(
function getUtils<M, MM extends DeepMergeBuiltInMetaData>(
options: DeepMergeOptions<M, MM>,
customizedDeepmerge: DeepMergeMergeFunctionUtils<M, MM>["deepmerge"]
): DeepMergeMergeFunctionUtils<M, MM> {
Expand Down Expand Up @@ -177,7 +175,7 @@ function mergeUnknowns<
U extends DeepMergeMergeFunctionUtils<M, MM>,
MF extends DeepMergeMergeFunctionsURIs,
M,
MM extends Readonly<Record<PropertyKey, unknown>>
MM extends DeepMergeBuiltInMetaData
>(values: Ts, utils: U, meta: M | undefined): DeepMergeHKT<Ts, MF, M> {
if (values.length === 0) {
return undefined as DeepMergeHKT<Ts, MF, M>;
Expand Down
17 changes: 7 additions & 10 deletions src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,18 @@ import type { DeepMergeBuiltInMetaData } from "./merging";
*/
export type DeepMergeOptions<
M,
MM extends Record<PropertyKey, unknown> = DeepMergeBuiltInMetaData
> = Partial<DeepMergeOptionsFull<M, MM & Partial<DeepMergeBuiltInMetaData>>>;
MM extends Readonly<Record<PropertyKey, unknown>> = DeepMergeBuiltInMetaData
> = Partial<DeepMergeOptionsFull<M, MM & DeepMergeBuiltInMetaData>>;

type MetaDataUpdater<M, MM extends Record<PropertyKey, unknown>> = (
type MetaDataUpdater<M, MM extends DeepMergeBuiltInMetaData> = (
previousMeta: M | undefined,
metaMeta: MM
metaMeta: Readonly<Partial<MM>>
) => M;

/**
* All the options the user can pass to customize deepmerge.
*/
type DeepMergeOptionsFull<
M,
MM extends Record<PropertyKey, unknown>
> = Readonly<{
type DeepMergeOptionsFull<M, MM extends DeepMergeBuiltInMetaData> = Readonly<{
mergeRecords: DeepMergeMergeFunctions<M, MM>["mergeRecords"] | false;
mergeArrays: DeepMergeMergeFunctions<M, MM>["mergeArrays"] | false;
mergeMaps: DeepMergeMergeFunctions<M, MM>["mergeMaps"] | false;
Expand All @@ -35,7 +32,7 @@ type DeepMergeOptionsFull<
*/
type DeepMergeMergeFunctions<
M,
MM extends Record<PropertyKey, unknown>
MM extends DeepMergeBuiltInMetaData
> = Readonly<{
mergeRecords: <
Ts extends ReadonlyArray<Readonly<Record<PropertyKey, unknown>>>,
Expand Down Expand Up @@ -88,7 +85,7 @@ type DeepMergeMergeFunctions<
*/
export type DeepMergeMergeFunctionUtils<
M,
MM extends Record<PropertyKey, unknown>
MM extends DeepMergeBuiltInMetaData
> = Readonly<{
mergeFunctions: DeepMergeMergeFunctions<M, MM>;
defaultMergeFunctions: DeepMergeMergeFunctionsDefaults;
Expand Down
5 changes: 4 additions & 1 deletion tests/deepmerge-custom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ test("key path based merging", (t) => {
ReadonlyArray<PropertyKey>
>({
metaDataUpdater: (previousMeta = [], metaMeta) => {
if (metaMeta.key === undefined) {
return previousMeta;
}
return [...previousMeta, metaMeta.key];
},
mergeOthers: (values, utils, meta) => {
Expand Down Expand Up @@ -466,7 +469,7 @@ test("key path based array merging", (t) => {
) => {
const mergeSettings: DeepMergeOptions<
ReadonlyArray<unknown>,
Readonly<Partial<{ id: unknown }>>
Readonly<{ id: unknown }>
> = {
metaDataUpdater: (previousMeta = [], metaMeta) => {
return [...previousMeta, metaMeta.key ?? metaMeta.id];
Expand Down

0 comments on commit 9a881d3

Please sign in to comment.