diff --git a/package.json b/package.json index b06f67d..26fe00b 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/utils/merge-options.test.ts b/src/utils/merge-options.test.ts index 51b722a..6eda97d 100644 --- a/src/utils/merge-options.test.ts +++ b/src/utils/merge-options.test.ts @@ -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>( + { 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({ 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', () => { diff --git a/src/utils/merge-options.ts b/src/utils/merge-options.ts index c12e222..938ff39 100644 --- a/src/utils/merge-options.ts +++ b/src/utils/merge-options.ts @@ -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; } @@ -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 @@ -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