Skip to content

Commit 4c7ea69

Browse files
authored
fix: deepMerge | remove duplicates when merging arrays (#325)
* fix: deepMerge | remove duplicates when merging arrays * test: add unit tests for depMeerge function
1 parent c66f60f commit 4c7ea69

File tree

5 files changed

+63
-47
lines changed

5 files changed

+63
-47
lines changed

dist/plugin.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/ui.html

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

dist/ui.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utilities/deepMerge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const deepMerge = (target, source) => {
1919
const sourceValue = source[key]
2020
// merge both values
2121
if (Array.isArray(targetValue) && Array.isArray(sourceValue)) {
22-
target[key] = targetValue.concat(sourceValue)
22+
target[key] = [...new Set(targetValue.concat(sourceValue))]
2323
} else if (isObject(targetValue) && isObject(sourceValue)) {
2424
target[key] = deepMerge(Object.assign({}, targetValue), sourceValue)
2525
} else {

tests/unit/deepMerge.test.ts

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,60 @@
1+
// deepMerge.test.ts
12
import deepMerge from '@utils/deepMerge'
23

34
describe('deepMerge', () => {
4-
test('merge objects', () => {
5-
expect(deepMerge(
6-
{
7-
id: 'not visible',
8-
type: 'unique',
9-
nested: {
10-
stay: 'still here',
11-
override: 'not here',
12-
merged: [2]
13-
}
14-
},
15-
{
16-
id: 'visible',
17-
nested: {
18-
override: 'new value',
19-
merged: ['test']
20-
},
21-
description: 'from second'
22-
}
23-
)).toStrictEqual(
24-
{
25-
id: 'visible',
26-
type: 'unique',
27-
nested: {
28-
stay: 'still here',
29-
override: 'new value',
30-
merged: [2, 'test']
31-
},
32-
description: 'from second'
33-
}
34-
)
35-
})
36-
37-
test('argument 1 is string, return source', () => {
38-
expect(deepMerge('test', { value: 1 })).toStrictEqual({ value: 1 })
39-
})
40-
41-
test('argument 2 is string, return source', () => {
42-
expect(deepMerge({ value: 1 }, 'test')).toStrictEqual('test')
5+
it('should merge two objects', () => {
6+
const target = { a: 1, b: 2 }
7+
const source = { b: 3, c: 4 }
8+
const result = deepMerge(target, source)
9+
expect(result).toEqual({ a: 1, b: 3, c: 4 })
10+
})
11+
12+
it('should merge nested objects', () => {
13+
const target = { a: { b: 1 } }
14+
const source = { a: { c: 2 } }
15+
const result = deepMerge(target, source)
16+
expect(result).toEqual({ a: { b: 1, c: 2 } })
17+
})
18+
19+
it('should merge arrays without duplicates', () => {
20+
const target = { a: [1, 2] }
21+
const source = { a: [2, 3] }
22+
const result = deepMerge(target, source)
23+
expect(result).toEqual({ a: [1, 2, 3] })
24+
})
25+
26+
it('should overwrite non-object values', () => {
27+
const target = { a: 1 }
28+
const source = { a: 2 }
29+
const result = deepMerge(target, source)
30+
expect(result).toEqual({ a: 2 })
31+
})
32+
33+
it('should return source if target is not an object', () => {
34+
const target = null
35+
const source = { a: 1 }
36+
const result = deepMerge(target, source)
37+
expect(result).toEqual({ a: 1 })
38+
})
39+
40+
it('should return source if source is not an object', () => {
41+
const target = { a: 1 }
42+
const source = null
43+
const result = deepMerge(target, source)
44+
expect(result).toEqual(null)
45+
})
46+
47+
it('should handle empty objects', () => {
48+
const target = {}
49+
const source = {}
50+
const result = deepMerge(target, source)
51+
expect(result).toEqual({})
52+
})
53+
54+
it('should handle empty arrays', () => {
55+
const target = { a: [] }
56+
const source = { a: [] }
57+
const result = deepMerge(target, source)
58+
expect(result).toEqual({ a: [] })
4359
})
4460
})

0 commit comments

Comments
 (0)