Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |