Skip to content

Commit

Permalink
feat: lone values will now be passed to mergeOthers rather than just …
Browse files Browse the repository at this point in the history
…returned (#57)

Previously if a value had nothing to be merged with, it would just be used as the result. Now it is
instead passed to the `mergeOthers` function so that the result can be customized if wanted
  • Loading branch information
RebeccaStevens committed Feb 19, 2022
1 parent 3d96692 commit 9c24584
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
"@semantic-release/github": "^8.0.2",
"@semantic-release/npm": "^9.0.0",
"@semantic-release/release-notes-generator": "^10.0.3",
"@types/lodash": "^4.14.178",
"@types/node": "^17.0.16",
"@types/rollup-plugin-auto-external": "^2.0.2",
"@typescript-eslint/eslint-plugin": "^5.11.0",
Expand Down Expand Up @@ -128,6 +129,7 @@
"eslint-plugin-unicorn": "^40.1.0",
"husky": "^7.0.4",
"lint-staged": "^12.3.3",
"lodash": "^4.17.21",
"markdownlint-cli": "^0.31.1",
"marked": "^4.0.12",
"nyc": "^15.1.0",
Expand Down
31 changes: 16 additions & 15 deletions src/deepmerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,6 @@ export function deepmergeCustom<
* The customized deepmerge function.
*/
function customizedDeepmerge(...objects: ReadonlyArray<unknown>) {
if (objects.length === 0) {
return undefined;
}
if (objects.length === 1) {
return objects[0];
}

return mergeUnknowns<
ReadonlyArray<unknown>,
typeof utils,
Expand Down Expand Up @@ -186,6 +179,17 @@ function mergeUnknowns<
M,
MM extends Readonly<Record<PropertyKey, unknown>>
>(values: Ts, utils: U, meta: M | undefined): DeepMergeHKT<Ts, MF, M> {
if (values.length === 0) {
return undefined as DeepMergeHKT<Ts, MF, M>;
}
if (values.length === 1) {
return utils.mergeFunctions.mergeOthers(
values,
utils,
meta
) as DeepMergeHKT<Ts, MF, M>;
}

const type = getObjectType(values[0]);

// eslint-disable-next-line functional/no-conditional-statement -- add an early escape for better performance.
Expand Down Expand Up @@ -274,14 +278,11 @@ function mergeRecords<
parents: values,
} as unknown as MM);

result[key] =
propValues.length === 1
? propValues[0]
: mergeUnknowns<ReadonlyArray<unknown>, U, MF, M, MM>(
propValues,
utils,
updatedMeta
);
result[key] = mergeUnknowns<ReadonlyArray<unknown>, U, MF, M, MM>(
propValues,
utils,
updatedMeta
);
}

/* eslint-enable functional/no-loop-statement, functional/no-conditional-statement */
Expand Down
27 changes: 27 additions & 0 deletions tests/deepmerge-custom.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/consistent-type-definitions, @typescript-eslint/no-unused-vars */

import test from "ava";
import _ from "lodash";

import { deepmergeCustom } from "@/deepmerge";
import type {
Expand Down Expand Up @@ -563,3 +564,29 @@ test("custom merge with parents", (t) => {

t.deepEqual(merged, expected);
});

test("custom merge that clones", (t) => {
const x = { foo: { bar: { baz: { qux: [1, 2, 3] } } } };
const y = { bar: new Date("2021-02-02"), baz: { qux: 1 } };

const expected = {
foo: _.cloneDeep(x.foo),
bar: _.cloneDeep(y.bar),
baz: _.cloneDeep(y.baz),
} as const;

const customizedDeepmerge = deepmergeCustom({
mergeOthers: (values, utils) =>
_.cloneDeep(utils.defaultMergeFunctions.mergeOthers(values)),
});

const merged = customizedDeepmerge(x, y);

t.deepEqual(merged, expected);
t.not(merged.foo, x.foo);
t.not(merged.foo.bar, x.foo.bar);
t.not(merged.foo.bar.baz, x.foo.bar.baz);
t.not(merged.foo.bar.baz.qux, x.foo.bar.baz.qux);
t.not(merged.bar, y.bar);
t.not(merged.baz, y.baz);
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,11 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash@^4.14.178":
version "4.14.178"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.178.tgz#341f6d2247db528d4a13ddbb374bcdc80406f4f8"
integrity sha512-0d5Wd09ItQWH1qFbEyQ7oTQ3GZrMfth5JkbN3EvTKLXcHLRDSXeLnlvlOn0wvxVIwK5o2M8JzP/OWz7T3NRsbw==

"@types/mdast@^3.0.0":
version "3.0.9"
resolved "https://registry.yarnpkg.com/@types/mdast/-/mdast-3.0.9.tgz#3f7fa18faf9e567da9aa49e44ecc76ad33c359ce"
Expand Down

0 comments on commit 9c24584

Please sign in to comment.