Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Jul 9, 2024
1 parent 18c1efd commit 527d081
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/array/toArray.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function toArray<T, U = never>(
value: T,
): T extends readonly (infer U)[] ? U[] : [T]

export function toArray<T>(value: T | readonly T[]): any {
return Array.isArray(value) ? [...value] : [value]
}
11 changes: 11 additions & 0 deletions src/array/toArrayIfExists.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function toArrayIfExists<T>(
value: T | readonly T[],
): T extends any[]
? T[number][]
: T extends readonly any[]
? readonly T[number][]
: Exclude<T, null | undefined>[] | undefined

export function toArrayIfExists<T>(value: T | readonly T[]): any {
return Array.isArray(value) ? [...value] : value != null ? [value] : undefined
}

0 comments on commit 527d081

Please sign in to comment.