Created
February 24, 2019 19:34
-
-
Save conmute/8e4e10a66079418c9c068f9a8d7d726e to your computer and use it in GitHub Desktop.
`[[1,2,[3]],4] => [1,2,3,4]` full example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const flatten = (l) => l.reduce ? l.reduce((acc, x) => acc.concat(fn(x)), []) : [l] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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