Skip to content

Commit

Permalink
fix: merge function nested structure fix when using custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
KiraLT committed Jan 1, 2023
1 parent 229608a commit c75f63d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions spec/object.spec.ts
Expand Up @@ -115,6 +115,10 @@ describe('merge', () => {
it('supports custom array merge function', () => {
expect(merge([1, 2], [3, 4], { arrayPolicy: (a, b) => b.concat(a) })).toEqual([3, 4, 1, 2])
})

it('skip nulls', () => {
expect(merge({ a: 1 }, { a: null }, { skipNulls: true })).toEqual({ a: 1 })
})
})

describe('clone', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/object.ts
Expand Up @@ -126,15 +126,19 @@ export function merge<T>(
options?: {
/**
* When `source` has `null` or `undefined` value, do not overwrite `target`
*
* @default false
*/
skipNulls?: false
skipNulls?: boolean
/**
* Array merge policy, default is `overwrite`
*
* Available policies:
* * `overwrite` - always replace `target` array with `source`
* * `merge` - merge `target` and `source` array values
* * `(target, source) => source` - custom array merge function
*
* @default 'overwrite'
*/
arrayPolicy?:
| 'overwrite'
Expand All @@ -147,7 +151,7 @@ export function merge<T>(
if (isPlainObject(target) && isPlainObject(source)) {
return Object.entries(source).reduce(
(prev, [key, value]) => {
prev[key] = merge(prev[key], value)
prev[key] = merge(prev[key], value, options)
return prev
},
{ ...target }
Expand Down

0 comments on commit c75f63d

Please sign in to comment.