Skip to content

Commit 71fd140

Browse files
authored
fix(useArrayFilter): match type to Array.prototype.filter (#4175)
1 parent f30ccf0 commit 71fd140

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

packages/shared/useArrayFilter/index.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ describe('useArrayFilter', () => {
2727
list.value.shift()
2828
expect(result.value).toStrictEqual([2, 4, 6, 8])
2929
})
30+
31+
it('should allow values other than boolean in fn', () => {
32+
const list = ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
33+
const result = useArrayFilter(list, i => i % 2)
34+
expect(result.value).toStrictEqual([1, 3, 5, 7, 9])
35+
})
3036
})

packages/shared/useArrayFilter/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@ import { toValue } from '../toValue'
1212
*
1313
* @returns a shallow copy of a portion of the given array, filtered down to just the elements from the given array that pass the test implemented by the provided function. If no elements pass the test, an empty array will be returned.
1414
*/
15+
export function useArrayFilter<T, S extends T>(
16+
list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,
17+
fn: (element: T, index: number, array: T[]) => element is S,
18+
): ComputedRef<S[]>
19+
export function useArrayFilter<T>(
20+
list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,
21+
fn: (element: T, index: number, array: T[]) => unknown,
22+
): ComputedRef<T[]>
1523
export function useArrayFilter<T>(
1624
list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,
17-
fn: (element: T, index: number, array: T[]) => boolean,
25+
fn: (element: T, index: number, array: T[]) => unknown,
1826
): ComputedRef<T[]> {
1927
return computed(() => toValue(list).map(i => toValue(i)).filter(fn))
2028
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy