Skip to content

Commit

Permalink
feat!: undefined will no longer replace defined values by default
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed May 10, 2024
1 parent f54235e commit 80ff1de
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/defaults/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export function mergeMaps<
}

/**
* Get the last value in the given array.
* Get the last non-undefined value in the given array.
*/
export function mergeOthers<Ts extends ReadonlyArray<unknown>>(values: Ts) {
return values.at(-1);
return values.findLast((value) => value !== undefined);
}
4 changes: 3 additions & 1 deletion src/types/merging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
type EveryIsMap,
type EveryIsRecord,
type EveryIsSet,
type Is,
type IsNever,
type IsTuple,
type Or,
} from "./utils";

/**
Expand Down Expand Up @@ -169,7 +171,7 @@ export type DeepMergeLeaf<Ts extends ReadonlyArray<unknown>> =
: Ts extends readonly [infer T]
? T
: Ts extends readonly [...infer Rest, infer Tail]
? IsNever<Tail> extends true
? Or<IsNever<Tail>, Is<Tail, undefined>> extends true
? Rest extends ReadonlyArray<unknown>
? DeepMergeLeaf<Rest>
: never
Expand Down
2 changes: 1 addition & 1 deletion tests/deepmerge.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ const i = {
};

const test13 = deepmerge(a, i);
expectType<{ foo: undefined; baz: { quux: string[] }; garply: number }>(test13);
expectType<{ foo: string; baz: { quux: string[] }; garply: number }>(test13);

const j = {
foo: new Set([1, 2]),
Expand Down
6 changes: 3 additions & 3 deletions tests/deepmerge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,18 +300,18 @@ describe("deepmerge", () => {
const x = { key1: undefined };
const y = { key1: { subkey: `one` } };

const expected = { key1: { subkey: `one` } };
const expected = { key1: y.key1 };

const merged = deepmerge(x, y);

expect(merged).toStrictEqual(expected);
});

it(`replaces records with undefined`, () => {
it(`doesn't replaces records with undefined`, () => {
const x = { key1: { subkey: `one` } };
const y = { key1: undefined };

const expected = { key1: undefined };
const expected = { key1: x.key1 };

const merged = deepmerge(x, y);

Expand Down

0 comments on commit 80ff1de

Please sign in to comment.