Skip to content

Commit

Permalink
feat(useArrayFindIndex): new function (#1973)
Browse files Browse the repository at this point in the history
  • Loading branch information
huynl-96 committed Jul 21, 2022
1 parent 4c9b6d2 commit c15209e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/shared/index.ts
Expand Up @@ -34,6 +34,7 @@ export * from './until'
export * from './useArrayEvery'
export * from './useArrayFilter'
export * from './useArrayFind'
export * from './useArrayFindIndex'
export * from './useArrayJoin'
export * from './useArrayMap'
export * from './useArrayReduce'
Expand Down
36 changes: 36 additions & 0 deletions packages/shared/useArrayFindIndex/index.md
@@ -0,0 +1,36 @@
---
category: Array
---

# useArrayFindIndex

Reactive `Array.findIndex`

## Usage

### Use with array of multiple refs

```js
import { useArrayFindIndex } 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 = useArrayFindIndex(list, i => i % 2 === 0)
// result.value: 0
item1.value = 1
// result.value: 1
```

### Use with reactive array

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

describe('useArrayFindIndex', () => {
it('should be defined', () => {
expect(useArrayFindIndex).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 = useArrayFindIndex(list, i => i % 2 === 0)
expect(result.value).toBe(0)
item1.value = 1
expect(result.value).toBe(1)
item2.value = 3
expect(result.value).toBe(2)
item3.value = 5
expect(result.value).toBe(3)
item4.value = 7
expect(result.value).toBe(4)
item5.value = 9
expect(result.value).toBe(-1)
})

it('should work with reactive array', () => {
const list = ref([0, 2, 4, 6, 8])
const result = useArrayFindIndex(list, i => i % 2 === 0)
expect(result.value).toBe(0)
list.value.unshift(-1)
expect(result.value).toBe(1)
})
})
11 changes: 11 additions & 0 deletions packages/shared/useArrayFindIndex/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 useArrayFindIndex<T>(
list: MaybeComputedRef<MaybeComputedRef<T>[]>,
fn: (element: T, index: number, array: MaybeComputedRef<T>[]) => unknown,
): ComputedRef<number> {
return computed(() => resolveUnref(list).findIndex((element, index, array) => fn(resolveUnref(element), index, array)))
}
2 changes: 1 addition & 1 deletion packages/shared/useArrayJoin/index.md
@@ -1,5 +1,5 @@
---
category: Utilities
category: Array
---

# useArrayJoin
Expand Down

0 comments on commit c15209e

Please sign in to comment.