Skip to content

Instantly share code, notes, and snippets.

@fostyfost
Last active November 11, 2021 14:17
Show Gist options
  • Save fostyfost/7163466940238593640b2b205f32ade6 to your computer and use it in GitHub Desktop.
Save fostyfost/7163466940238593640b2b205f32ade6 to your computer and use it in GitHub Desktop.
Array Flat
type TupleRecursive<T> = T | Tuple<T>
export type Tuple<T> = TupleRecursive<T>[]
const isArray = Array.isArray
export const flat = <T>(tuple: Tuple<T>): T[] => {
const result: T[] = []
const flatten = (tuple: Tuple<T>): void => {
tuple.forEach(item => {
if (isArray(item)) {
flatten(item)
} else {
result.push(item)
}
})
}
if (isArray(tuple)) {
flatten(tuple)
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment