Skip to content

Commit

Permalink
fix: deepMerge | remove duplicates when merging arrays (#325)
Browse files Browse the repository at this point in the history
* fix: deepMerge | remove duplicates when merging arrays

* test: add unit tests for depMeerge function
  • Loading branch information
0m4r authored Jan 17, 2025
1 parent c66f60f commit 4c7ea69
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 47 deletions.
2 changes: 1 addition & 1 deletion dist/plugin.js

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/ui.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions dist/ui.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/utilities/deepMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const deepMerge = (target, source) => {
const sourceValue = source[key]
// merge both values
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
target[key] = targetValue.concat(sourceValue)
target[key] = [...new Set(targetValue.concat(sourceValue))]
} else if (isObject(targetValue) && isObject(sourceValue)) {
target[key] = deepMerge(Object.assign({}, targetValue), sourceValue)
} else {
Expand Down
94 changes: 55 additions & 39 deletions tests/unit/deepMerge.test.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,60 @@
// deepMerge.test.ts
import deepMerge from '@utils/deepMerge'

describe('deepMerge', () => {
test('merge objects', () => {
expect(deepMerge(
{
id: 'not visible',
type: 'unique',
nested: {
stay: 'still here',
override: 'not here',
merged: [2]
}
},
{
id: 'visible',
nested: {
override: 'new value',
merged: ['test']
},
description: 'from second'
}
)).toStrictEqual(
{
id: 'visible',
type: 'unique',
nested: {
stay: 'still here',
override: 'new value',
merged: [2, 'test']
},
description: 'from second'
}
)
})

test('argument 1 is string, return source', () => {
expect(deepMerge('test', { value: 1 })).toStrictEqual({ value: 1 })
})

test('argument 2 is string, return source', () => {
expect(deepMerge({ value: 1 }, 'test')).toStrictEqual('test')
it('should merge two objects', () => {
const target = { a: 1, b: 2 }
const source = { b: 3, c: 4 }
const result = deepMerge(target, source)
expect(result).toEqual({ a: 1, b: 3, c: 4 })
})

it('should merge nested objects', () => {
const target = { a: { b: 1 } }
const source = { a: { c: 2 } }
const result = deepMerge(target, source)
expect(result).toEqual({ a: { b: 1, c: 2 } })
})

it('should merge arrays without duplicates', () => {
const target = { a: [1, 2] }
const source = { a: [2, 3] }
const result = deepMerge(target, source)
expect(result).toEqual({ a: [1, 2, 3] })
})

it('should overwrite non-object values', () => {
const target = { a: 1 }
const source = { a: 2 }
const result = deepMerge(target, source)
expect(result).toEqual({ a: 2 })
})

it('should return source if target is not an object', () => {
const target = null
const source = { a: 1 }
const result = deepMerge(target, source)
expect(result).toEqual({ a: 1 })
})

it('should return source if source is not an object', () => {
const target = { a: 1 }
const source = null
const result = deepMerge(target, source)
expect(result).toEqual(null)
})

it('should handle empty objects', () => {
const target = {}
const source = {}
const result = deepMerge(target, source)
expect(result).toEqual({})
})

it('should handle empty arrays', () => {
const target = { a: [] }
const source = { a: [] }
const result = deepMerge(target, source)
expect(result).toEqual({ a: [] })
})
})

0 comments on commit 4c7ea69

Please sign in to comment.