Skip to content

Commit

Permalink
feat(useArrayFilter): new function (#1905)
Browse files Browse the repository at this point in the history
  • Loading branch information
huynl-96 committed Jul 14, 2022
1 parent 011afb2 commit 50e3520
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/shared/index.ts
Expand Up @@ -31,6 +31,7 @@ export * from './tryOnMounted'
export * from './tryOnScopeDispose'
export * from './tryOnUnmounted'
export * from './until'
export * from './useArrayFilter'
export * from './useCounter'
export * from './useDateFormat'
export * from './useDebounceFn'
Expand Down
36 changes: 36 additions & 0 deletions packages/shared/useArrayFilter/index.md
@@ -0,0 +1,36 @@
---
category: Utilities
---

# useArrayFilter

Reactive `Array.filter`

## Usage

### Use with array of multiple refs

```js
import { useArrayFilter } from '@vueuse/core'
const item1 = ref(0)
const item2 = ref(2)
const item3 = ref(4)
const item4 = ref(6)
const item5 = ref(8)
const list = [item1, item2, item3, item4, item5]
const result = useArrayFilter(list, i => i % 2 === 0)
// result.value: [0, 2, 4, 6, 8]
item2.value = 1
// result.value: [0, 4, 6, 8]
```

### Use with reactive array

```js
import { useArrayFilter } from '@vueuse/core'
const list = ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
const result = useArrayFilter(list, i => i % 2 === 0)
// result.value: [0, 2, 4, 6, 8]
list.value.shift()
// result.value: [2, 4, 6, 8]
```
29 changes: 29 additions & 0 deletions packages/shared/useArrayFilter/index.test.ts
@@ -0,0 +1,29 @@
import { ref } from 'vue-demi'
import { useArrayFilter } from '../useArrayFilter'

describe('useArrayFilter', () => {
it('should be defined', () => {
expect(useArrayFilter).toBeDefined()
})

it('should work with array of refs', () => {
const item1 = ref(0)
const item2 = ref(2)
const item3 = ref(4)
const item4 = ref(6)
const item5 = ref(8)
const list = [item1, item2, item3, item4, item5]
const result = useArrayFilter(list, i => i % 2 === 0)
expect(result.value).toStrictEqual([0, 2, 4, 6, 8])
item2.value = 1
expect(result.value).toStrictEqual([0, 4, 6, 8])
})

it('should work with reactive array', () => {
const list = ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
const result = useArrayFilter(list, i => i % 2 === 0)
expect(result.value).toStrictEqual([0, 2, 4, 6, 8])
list.value.shift()
expect(result.value).toStrictEqual([2, 4, 6, 8])
})
})
11 changes: 11 additions & 0 deletions packages/shared/useArrayFilter/index.ts
@@ -0,0 +1,11 @@
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveUnref } from '@vueuse/shared'
import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'

export function useArrayFilter<T>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
fn: (element: T, index: number, array: T[]) => boolean,
): ComputedRef<T[]> {
return computed(() => resolveUnref(list).map(i => resolveUnref(i)).filter(fn))
}

0 comments on commit 50e3520

Please sign in to comment.