Skip to content

Commit 8d5cb9e

Browse files
lfhwnqe43081j
andauthored
fix(useArrayDifference): support numeric and symbol property keys (#5696)
Co-authored-by: James Garbutt <43081j@users.noreply.github.com>
1 parent 043da37 commit 8d5cb9e

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

‎packages/shared/useArrayDifference/index.test.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,26 @@ import { ref as deepRef } from 'vue'
33
import { useArrayDifference } from './index'
44

55
describe('useArrayDifference', () => {
6+
it('should compare numeric property keys', () => {
7+
const list = deepRef([{ 0: 'a' }, { 0: 'b' }])
8+
const values = deepRef([{ 0: 'b' }])
9+
const result = useArrayDifference(list, values, 0)
10+
11+
expect(result.value).toEqual([{ 0: 'a' }])
12+
values.value.push({ 0: 'a' })
13+
expect(result.value).toEqual([])
14+
})
15+
16+
it('should compare symbol property keys in symmetric mode', () => {
17+
const key = Symbol('id')
18+
const first = { [key]: 1 }
19+
const second = { [key]: 2 }
20+
const third = { [key]: 3 }
21+
22+
expect(useArrayDifference([first, second], [{ [key]: 2 }, third], key, { symmetric: true }).value)
23+
.toEqual([first, third])
24+
})
25+
626
it('should be defined', () => {
727
expect(useArrayDifference).toBeDefined()
828
})

‎packages/shared/useArrayDifference/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export function useArrayDifference<T>(...args: any[]): UseArrayDifferenceReturn<
4747
symmetric = false,
4848
} = args[3] ?? {}
4949

50-
if (typeof compareFn === 'string') {
50+
if (typeof compareFn !== 'function') {
5151
const key = compareFn as keyof T
5252
compareFn = (value: T, othVal: T) => value[key] === othVal[key]
5353
}

0 commit comments

Comments
 (0)