Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deepMerge | remove duplicates when merging arrays #325

Merged
merged 2 commits into from
Jan 17, 2025
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 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: [] })
})
})
Loading