Created
January 10, 2019 08:33
-
-
Save sdebaun/04c41a881d132f417cc1c8801be63a94 to your computer and use it in GitHub Desktop.
using jest, ts-jest
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
const flatten = (arr: Array<any>): Array<any> => | |
arr.reduce((a, x) => a.concat(flattenOrArray(x)), []) | |
const flattenOrArray = (x: any) => | |
Array.isArray(x) ? flatten(x) : [x] | |
test('no nesting', () => { | |
expect(flatten([1, 2, 3])).toEqual([1, 2, 3]) | |
}) | |
test('1 level', () => { | |
expect(flatten([[1, 2], 3])).toEqual([1, 2, 3]) | |
}) | |
test('3 level', () => { | |
expect(flatten([[1, 2], 3, [4, [5, 6]]])).toEqual([1, 2, 3, 4, 5, 6]) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment