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

feat(useArrayUnique): add a custom function to deduplicate #2612

Merged
merged 5 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/shared/useArrayUnique/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ const list = reactive([1, 2, 2, 3])
const result = useArrayUnique(list)
// result.value: [1, 2, 3]

result.value.push(1)
result.push(1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason of this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry here should be list.push I have modified it in the latest commit

// result.value: [1, 2, 3]
```

### Use with custom function

```js
import { useArrayUnique } from '@vueuse/core'

const list = reactive([
{ id: 1, name: 'foo' },
{ id: 2, name: 'bar' },
{ id: 1, name: 'baz' },
])

const result = useArrayUnique(list, (a, b) => a.id === b.id)
// result.value: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]

list.push({ id: 1, name: 'qux' })
// result.value: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }]
```
29 changes: 28 additions & 1 deletion packages/shared/useArrayUnique/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { reactive } from 'vue'
import { ref } from 'vue-demi'
import { useArrayUnique } from '../useArrayUnique'

Expand All @@ -19,11 +20,37 @@ describe('useArraySome', () => {
expect(result.value.length).toBe(3)
})

it('should work with reactive array', () => {
it('should work with ref array', () => {
const list = ref([1, 2, 2, 3])
const result = useArrayUnique(list)
expect(result.value.length).toBe(3)
list.value.push(1)
expect(result.value.length).toBe(3)
})

it('should work with reactive array', () => {
const list = reactive([1, 2, 2, 3])
const result = useArrayUnique(list)
expect(result.value.length).toBe(3)
list.push(1)
expect(result.value.length).toBe(3)
})

it('should work with array of reactive and custom compare function', () => {
const list = reactive([
{
id: 1,
name: 'foo',
}, {
id: 2,
name: 'bar',
},
{
id: 1,
name: 'baz',
},
])
const result = useArrayUnique(list, (a, b) => a.id === b.id)
expect(result.value.length).toBe(2)
})
})
23 changes: 21 additions & 2 deletions packages/shared/useArrayUnique/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,32 @@ import { computed } from 'vue-demi'
import type { MaybeComputedRef } from '../utils'
import { resolveUnref } from '../resolveUnref'

function uniq<T>(array: T[]) {
return Array.from(new Set(array))
}

function uniqueElementsBy<T>(
array: T[],
fn: (a: T, b: T, array: T[]) => boolean,
) {
return array.reduce<T[]>((acc, v) => {
if (!acc.some(x => fn(v, x, array)))
acc.push(v)
return acc
}, [])
}

/**
* reactive unique array
* @see https://vueuse.org/useArrayUnique
* @param {Array} list - the array was called upon.
* @param fn - the function to compare
* @returns {Array} A computed ref that returns a unique array of items.
*/
export function useArrayUnique<T>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>): ComputedRef<T[]> {
return computed<T[]>(() => [...new Set<T>(resolveUnref(list).map(element => resolveUnref(element)))])
list: MaybeComputedRef<MaybeComputedRef<T>[]>, fn?: (a: T, b: T, array: T[]) => boolean): ComputedRef<T[]> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
list: MaybeComputedRef<MaybeComputedRef<T>[]>, fn?: (a: T, b: T, array: T[]) => boolean): ComputedRef<T[]> {
list: MaybeComputedRef<MaybeComputedRef<T>[]>, compareFn?: (a: T, b: T, array: T[]) => boolean): ComputedRef<T[]> {

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I have renamed to compareFn.

return computed<T[]>(() => {
const resolvedList = resolveUnref(list).map(element => resolveUnref(element))
return fn ? uniqueElementsBy(resolvedList, fn) : uniq(resolvedList)
})
}