Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package",
"name": "@adapty/core",
"version": "4.3.3",
"version": "4.3.4",
"description": "Platform-agnostic core for Adapty JS SDKs",
"license": "MIT",
"author": "Adapty team <support@adapty.io>",
Expand Down
87 changes: 85 additions & 2 deletions src/utils/merge-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,96 @@ describe('mergeOptions', () => {
expect(result).toEqual({ a: null, b: null, c: 3 });
});

it('should handle undefined values', () => {
it('should treat undefined as not provided and keep the default', () => {
const defaults = { a: 1, b: undefined };
const options = { a: undefined, c: 3 };

const result = mergeOptions(options, defaults);

expect(result).toEqual({ a: undefined, b: undefined, c: 3 });
expect(result).toStrictEqual({ a: 1, b: undefined, c: 3 });
});

it('should keep a nested object default when options pass undefined for it', () => {
const defaults = {
params: { fetchPolicy: 'reload', loadTimeoutMs: 5000 },
};

const result = mergeOptions({ id: 'p', params: undefined }, defaults);

expect(result).toStrictEqual({
id: 'p',
params: { fetchPolicy: 'reload', loadTimeoutMs: 5000 },
});
});

it('should keep nested defaults for undefined leaves', () => {
const defaults = {
params: { fetchPolicy: 'reload', loadTimeoutMs: 5000 },
};
const options = {
params: { fetchPolicy: undefined, loadTimeoutMs: 3000 },
};

const result = mergeOptions(options, defaults);

expect(result).toStrictEqual({
params: { fetchPolicy: 'reload', loadTimeoutMs: 3000 },
});
});

it('should omit undefined keys that have no default', () => {
const result = mergeOptions<Record<string, unknown>>(
{ x: undefined },
{ a: 1 },
);

expect(result).toStrictEqual({ a: 1 });
expect('x' in result).toBe(false);
});

it('should not pick up inherited props for undefined keys', () => {
const result = mergeOptions<object>({ toString: undefined }, { a: 1 });

expect(Object.prototype.hasOwnProperty.call(result, 'toString')).toBe(
false,
);
});

it('should return defaults when options is undefined', () => {
expect(mergeOptions(undefined, { a: 1 })).toStrictEqual({ a: 1 });
});

it('should keep a default array when options pass undefined for it', () => {
const result = mergeOptions({ items: undefined }, { items: [1, 2] });

expect(result).toStrictEqual({ items: [1, 2] });
});

it('should let null override defaults at any depth', () => {
const defaults = { params: { loadTimeoutMs: 5000 }, a: 1 };

expect(mergeOptions({ params: null }, defaults)).toStrictEqual({
params: null,
a: 1,
});
expect(
mergeOptions({ params: { loadTimeoutMs: null } }, defaults),
).toStrictEqual({
params: { loadTimeoutMs: null },
a: 1,
});
expect(mergeOptions(null, defaults)).toBeNull();
});

it('should not mutate defaults', () => {
const defaults = Object.freeze({
params: Object.freeze({ loadTimeoutMs: 5000 }),
});

mergeOptions({ params: { loadTimeoutMs: undefined } }, defaults);
mergeOptions({ params: undefined }, defaults);

expect(defaults).toStrictEqual({ params: { loadTimeoutMs: 5000 } });
});

it('should handle null in nested objects', () => {
Expand Down
22 changes: 20 additions & 2 deletions src/utils/merge-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ function deepMerge(
source: unknown,
seen = new Set(),
): unknown {
// Handle null/undefined cases
if (source === null || source === undefined) {
// undefined means "not provided": keep the default; null overrides it
if (source === undefined) {
return target;
}
if (source === null) {
return source;
}

Expand Down Expand Up @@ -60,6 +63,17 @@ function deepMerge(
continue;
}

// Not provided in source: keep the target value, or omit the key
if (source[key] === undefined) {
if (Object.prototype.hasOwnProperty.call(target, key)) {
if (seen.has(target[key])) {
throw new Error('Converting circular structure to JSON');
}
result[key] = target[key];
}
continue;
}

if (key in source) {
if (key in target) {
// Check for circular reference in values
Expand Down Expand Up @@ -98,6 +112,10 @@ function deepMerge(

/**
* Universal method for merging options with defaults
*
* Plain objects merge deeply; arrays and other values replace the default.
* `undefined` at any depth means "not provided" and keeps the default, while
* `null` is an explicit value that overrides it.
* @param options - input options
* @param defaults - default values
* @returns merged object with TResult type
Expand Down
Loading