Skip to content

Commit

Permalink
refactor: unrequire unused types and values
Browse files Browse the repository at this point in the history
BREAKING CHANGE: some types have changed for `deepmergeCustom`
  • Loading branch information
RebeccaStevens committed Feb 19, 2022
1 parent bb94749 commit c78e373
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 53 deletions.
41 changes: 13 additions & 28 deletions src/deepmerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
objectHasProperty,
} from "./utils";

const defaultOptions = {
const defaultMergeFunctions = {
mergeMaps,
mergeSets,
mergeArrays,
Expand All @@ -29,7 +29,7 @@ const defaultOptions = {
/**
* The default merge functions.
*/
export type DeepMergeMergeFunctionsDefaults = typeof defaultOptions;
export type DeepMergeMergeFunctionsDefaults = typeof defaultMergeFunctions;

/**
* Deeply merge objects.
Expand Down Expand Up @@ -93,9 +93,9 @@ function getUtils(
customizedDeepmerge: DeepMergeMergeFunctionUtils["deepmerge"]
): DeepMergeMergeFunctionUtils {
return {
defaultMergeFunctions: defaultOptions,
defaultMergeFunctions,
mergeFunctions: {
...defaultOptions,
...defaultMergeFunctions,
...Object.fromEntries(
Object.entries(options).map(([key, option]) =>
option === false ? [key, leaf] : [key, option]
Expand Down Expand Up @@ -209,9 +209,8 @@ function mergeRecords<
*/
function mergeArrays<
Ts extends ReadonlyArray<ReadonlyArray<unknown>>,
U extends DeepMergeMergeFunctionUtils,
MF extends DeepMergeMergeFunctionsURIs
>(values: Ts, utils: U) {
>(values: Ts) {
return values.flat() as DeepMergeArraysDefaultHKT<Ts, MF>;
}

Expand All @@ -220,15 +219,10 @@ function mergeArrays<
*
* @param values - The sets.
*/
function mergeSets<
Ts extends ReadonlyArray<Readonly<ReadonlySet<unknown>>>,
U extends DeepMergeMergeFunctionUtils,
MF extends DeepMergeMergeFunctionsURIs
>(values: Ts, utils: U) {
return new Set(getIterableOfIterables(values)) as DeepMergeSetsDefaultHKT<
Ts,
MF
>;
function mergeSets<Ts extends ReadonlyArray<Readonly<ReadonlySet<unknown>>>>(
values: Ts
) {
return new Set(getIterableOfIterables(values)) as DeepMergeSetsDefaultHKT<Ts>;
}

/**
Expand All @@ -237,25 +231,16 @@ function mergeSets<
* @param values - The maps.
*/
function mergeMaps<
Ts extends ReadonlyArray<Readonly<ReadonlyMap<unknown, unknown>>>,
U extends DeepMergeMergeFunctionUtils,
MF extends DeepMergeMergeFunctionsURIs
>(values: Ts, utils: U) {
return new Map(getIterableOfIterables(values)) as DeepMergeMapsDefaultHKT<
Ts,
MF
>;
Ts extends ReadonlyArray<Readonly<ReadonlyMap<unknown, unknown>>>
>(values: Ts) {
return new Map(getIterableOfIterables(values)) as DeepMergeMapsDefaultHKT<Ts>;
}

/**
* Merge "other" things.
*
* @param values - The values.
*/
function leaf<
Ts extends ReadonlyArray<unknown>,
U extends DeepMergeMergeFunctionUtils,
MF extends DeepMergeMergeFunctionsURIs
>(values: Ts, utils: U) {
function leaf<Ts extends ReadonlyArray<unknown>>(values: Ts) {
return values[values.length - 1];
}
15 changes: 7 additions & 8 deletions src/types/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,17 @@ type DeepMergeArraysDefaultHKTHelper<
/**
* Deep merge 2 sets.
*/
export type DeepMergeSetsDefaultHKT<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
> = Set<UnionSetValues<Ts>>;
export type DeepMergeSetsDefaultHKT<Ts extends ReadonlyArray<unknown>> = Set<
UnionSetValues<Ts>
>;

/**
* Deep merge 2 maps.
*/
export type DeepMergeMapsDefaultHKT<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
> = Map<UnionMapKeys<Ts>, UnionMapValues<Ts>>;
export type DeepMergeMapsDefaultHKT<Ts extends ReadonlyArray<unknown>> = Map<
UnionMapKeys<Ts>,
UnionMapValues<Ts>
>;

/**
* Get the merge functions with defaults apply from the given subset.
Expand Down
12 changes: 5 additions & 7 deletions src/types/merging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export interface DeepMergeMergeFunctionURItoKind<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
> {
readonly DeepMergeLeafURI: DeepMergeLeafHKT<Ts, MF>;
readonly DeepMergeLeafURI: DeepMergeLeafHKT<Ts>;
readonly DeepMergeRecordsDefaultURI: DeepMergeRecordsDefaultHKT<Ts, MF>;
readonly DeepMergeArraysDefaultURI: DeepMergeArraysDefaultHKT<Ts, MF>;
readonly DeepMergeSetsDefaultURI: DeepMergeSetsDefaultHKT<Ts, MF>;
readonly DeepMergeMapsDefaultURI: DeepMergeMapsDefaultHKT<Ts, MF>;
readonly DeepMergeSetsDefaultURI: DeepMergeSetsDefaultHKT<Ts>;
readonly DeepMergeMapsDefaultURI: DeepMergeMapsDefaultHKT<Ts>;
}

/**
Expand Down Expand Up @@ -145,10 +145,8 @@ export type DeepMergeLeafURI = "DeepMergeLeafURI";
/**
* Get the leaf type from 2 types that can't be merged.
*/
export type DeepMergeLeafHKT<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
> = DeepMergeLeaf<Ts>;
export type DeepMergeLeafHKT<Ts extends ReadonlyArray<unknown>> =
DeepMergeLeaf<Ts>;

/**
* Get the leaf type from many types that can't be merged.
Expand Down
1 change: 1 addition & 0 deletions tests/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@typescript-eslint/no-unsafe-member-access": "off",
"@typescript-eslint/no-unsafe-return": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"eslint-comments/disable-enable-pair": "off",
"functional/immutable-data": "off",
"functional/no-class": "off",
"functional/no-conditional-statement": "off",
Expand Down
12 changes: 4 additions & 8 deletions tests/deepmerge-custom.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-unused-vars */

import test from "ava";

import { deepmergeCustom } from "@/deepmerge";
Expand All @@ -9,7 +11,6 @@ import type {
} from "@/deepmerge";

declare module "ava" {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface DeepEqualAssertion {
/**
* Assert that `actual` is [deeply equal](https://github.com/concordancejs/concordance#comparison-details) to
Expand Down Expand Up @@ -56,7 +57,7 @@ test("custom merge strings", (t) => {
if (values.every((value) => typeof value === "string")) {
return values.join(" ");
}
return utils.defaultMergeFunctions.mergeOthers(values, utils);
return utils.defaultMergeFunctions.mergeOthers(values);
},
});

Expand All @@ -66,7 +67,6 @@ test("custom merge strings", (t) => {
});

declare module "../src/types" {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface DeepMergeMergeFunctionURItoKind<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
Expand Down Expand Up @@ -111,7 +111,6 @@ test("custom merge arrays", (t) => {
});

declare module "../src/types" {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface DeepMergeMergeFunctionURItoKind<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
Expand Down Expand Up @@ -186,7 +185,6 @@ test("custom merge arrays of records", (t) => {
});

declare module "../src/types" {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface DeepMergeMergeFunctionURItoKind<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
Expand Down Expand Up @@ -236,7 +234,6 @@ test("custom merge records", (t) => {
});

declare module "../src/types" {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface DeepMergeMergeFunctionURItoKind<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
Expand Down Expand Up @@ -265,7 +262,6 @@ test("custom don't merge arrays", (t) => {
});

declare module "../src/types" {
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
interface DeepMergeMergeFunctionURItoKind<
Ts extends ReadonlyArray<unknown>,
MF extends DeepMergeMergeFunctionsURIs
Expand Down Expand Up @@ -296,7 +292,7 @@ test("custom merge dates", (t) => {
if (values.every((value) => value instanceof Date)) {
return values;
}
return utils.defaultMergeFunctions.mergeOthers(values, utils);
return utils.defaultMergeFunctions.mergeOthers(values);
},
});

Expand Down
4 changes: 2 additions & 2 deletions tests/types/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ const merged1: {
foo: Set<string | number>;
bar: Map<string | number, string | number>;
} =
// $ExpectType { foo: DeepMergeSetsDefaultHKT<[Set<number>, Set<string>], Readonly<{ DeepMergeRecordsURI: "DeepMergeRecordsDefaultURI"; DeepMergeArraysURI: "DeepMergeArraysDefaultURI"; DeepMergeSetsURI: "DeepMergeSetsDefaultURI"; DeepMergeMapsURI: "DeepMergeMapsDefaultURI"; DeepMergeOthersURI: "DeepMergeLeafURI"; }>>; bar: DeepMergeMapsDefaultHKT<[Map<string, string>, Map<number, number>], Readonly<{ DeepMergeRecordsURI: "DeepMergeRecordsDefaultURI"; DeepMergeArraysURI: "DeepMergeArraysDefaultURI"; DeepMergeSetsURI: "DeepMergeSetsDefaultURI"; DeepMergeMapsURI: "DeepMergeMapsDefaultURI"; DeepMergeOthersURI: "DeepMergeLeafURI"; }>>; }
// $ExpectType { foo: DeepMergeSetsDefaultHKT<[Set<number>, Set<string>]>; bar: DeepMergeMapsDefaultHKT<[Map<string, string>, Map<number, number>]>; }
deepmerge(j, k);

const l = new Map([[1, new Map([[1, a]])]]);
Expand All @@ -154,7 +154,7 @@ const merged2: Map<
| Map<number, { foo: string; baz: { quux: string[] }; garply: number }>
| Map<number, { foo: string; baz: { corge: number }; grault: number }>
> =
// $ExpectType DeepMergeMapsDefaultHKT<[Map<number, Map<number, { foo: string; baz: { quux: string[]; }; garply: number; }>>, Map<number, Map<number, { foo: string; baz: { corge: number; }; grault: number; }>>], Readonly<{ DeepMergeRecordsURI: "DeepMergeRecordsDefaultURI"; DeepMergeArraysURI: "DeepMergeArraysDefaultURI"; DeepMergeSetsURI: "DeepMergeSetsDefaultURI"; DeepMergeMapsURI: "DeepMergeMapsDefaultURI"; DeepMergeOthersURI: "DeepMergeLeafURI"; }>>
// $ExpectType DeepMergeMapsDefaultHKT<[Map<number, Map<number, { foo: string; baz: { quux: string[]; }; garply: number; }>>, Map<number, Map<number, { foo: string; baz: { corge: number; }; grault: number; }>>]>
deepmerge(l, m);

const first = { first: true };
Expand Down

0 comments on commit c78e373

Please sign in to comment.