Skip to content

Commit

Permalink
feat(useArraySome): new function
Browse files Browse the repository at this point in the history
  • Loading branch information
huynl-96 committed Jul 15, 2022
1 parent 3992bb3 commit fedda2d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/shared/index.ts
Expand Up @@ -33,6 +33,7 @@ export * from './tryOnUnmounted'
export * from './until'
export * from './useArrayFilter'
export * from './useArrayMap'
export * from './useArraySome'
export * from './useCounter'
export * from './useDateFormat'
export * from './useDebounceFn'
Expand Down
36 changes: 36 additions & 0 deletions packages/shared/useArraySome/index.md
@@ -0,0 +1,36 @@
---
category: Utilities
---

# useArraySome

Reactive `Array.some`

## Usage

### Use with array of multiple refs

```js
import { useArraySome } 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 = useArraySome(list, i => i > 10)
// result.value: false
item1.value = 11
// result.value: true
```

### Use with reactive array

```js
import { useArraySome } from '@vueuse/core'
const list = ref([0, 2, 4, 6, 8])
const result = useArraySome(list, i => i > 10)
// result.value: false
list.value.push(11)
// result.value: true
```
29 changes: 29 additions & 0 deletions packages/shared/useArraySome/index.test.ts
@@ -0,0 +1,29 @@
import { ref } from 'vue-demi'
import { useArraySome } from '../useArraySome'

describe('useArraySome', () => {
it('should be defined', () => {
expect(useArraySome).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 = useArraySome(list, i => i > 10)
expect(result.value).toBe(false)
item1.value = 11
expect(result.value).toBe(true)
})

it('should work with reactive array', () => {
const list = ref([0, 2, 4, 6, 8])
const result = useArraySome(list, i => i > 10)
expect(result.value).toBe(false)
list.value.push(11)
expect(result.value).toBe(true)
})
})
12 changes: 12 additions & 0 deletions packages/shared/useArraySome/index.ts
@@ -0,0 +1,12 @@
import type { MaybeComputedRef } from '@vueuse/shared'
import { resolveUnref } from '@vueuse/shared'
import type { ComputedRef } from 'vue-demi'
import { computed } from 'vue-demi'

export function useArraySome<T>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
fn: (element: T, index: number, array: MaybeComputedRef<T>[]) => boolean,
): ComputedRef<boolean> {
const cb = (element: MaybeComputedRef<T>, index: number, array: MaybeComputedRef<T>[]) => fn(resolveUnref(element), index, array)
return computed(() => resolveUnref(list).some(cb))
}

0 comments on commit fedda2d

Please sign in to comment.