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 19, 2024
1 parent 69e9ba3 commit 9c86605
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/defaults/into.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,17 @@ export function mergeMaps<
}

/**
* Set the target to the last value.
* Set the target to the last non-undefined value.
*/
export function mergeOthers<Ts extends ReadonlyArray<unknown>>(
m_target: Reference<unknown>,
values: Ts,
) {
m_target.value = values.at(-1);
for (let i = values.length - 1; i >= 0; i--) {
if (values[i] !== undefined) {
m_target.value = values[i];
return;
}
}
m_target.value = undefined;
}
9 changes: 7 additions & 2 deletions src/defaults/vanilla.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,13 @@ 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);
for (let i = values.length - 1; i >= 0; i--) {
if (values[i] !== undefined) {
return values[i];
}
}
return 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
4 changes: 2 additions & 2 deletions tests/deepmerge-into.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ describe("deepmergeInto", () => {
expect(x).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: { subkey: `one` } };

deepmergeInto(x, y);

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 9c86605

Please sign in to comment.