Skip to content

Commit

Permalink
feat(select): let condition be undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jun 24, 2024
1 parent bac48d1 commit d115036
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,13 @@ export const objectify = <T, Key extends string | number | symbol, Value = T>(
export const select = <T, K>(
array: readonly T[],
mapper: (item: T, index: number) => K,
condition: (item: T, index: number) => boolean
condition?: (item: T, index: number) => boolean
) => {
if (!array) return []
let mapped: K
return array.reduce((acc, item, index) => {
if (!condition(item, index)) return acc
acc.push(mapper(item, index))
if (condition) condition(item, index) && acc.push(mapper(item, index))
else (mapped = mapper(item, index)) != null && acc.push(mapped)
return acc
}, [] as K[])
}
Expand Down

0 comments on commit d115036

Please sign in to comment.