Skip to content

Instantly share code, notes, and snippets.

@conmute
Created February 24, 2019 19:34
Show Gist options
  • Save conmute/8e4e10a66079418c9c068f9a8d7d726e to your computer and use it in GitHub Desktop.
Save conmute/8e4e10a66079418c9c068f9a8d7d726e to your computer and use it in GitHub Desktop.
`[[1,2,[3]],4] => [1,2,3,4]` full example
import { flatten } from './list'
test('Should return array if not an array', () => {
assert(flatten(1)).toMatchObject([1])
})
test('Should return array if an array', () => {
assert(flatten([1])).toMatchObject([1])
})
test('Should return array if an nested array', () => {
assert(flatten([1, [2], [3, [4]]])).toMatchObject([1, 2, 3, 4])
})
export const flatten = (l) => l.reduce ? l.reduce((acc, x) => acc.concat(fn(x)), []) : [l]
import { flatten } from './list'
flatten([[1,2,[3]],4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment