Created
March 6, 2017 20:07
-
-
Save Problematic/80b09bee082e32dc754be230b3c1a2d5 to your computer and use it in GitHub Desktop.
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
function flatten (arr) { | |
if (!(arr instanceof Array)) { | |
return arr; | |
} | |
return arr.reduce((acc, val) => acc.concat(flatten(val)), []); | |
} | |
module.exports = flatten; |
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 assert = require('assert'); | |
const flatten = require('./flatten'); | |
assert.equal(flatten("foo"), "foo"); | |
assert.deepEqual(flatten({foo: 'bar'}), {foo: 'bar'}) | |
assert.deepEqual(flatten([1]), [1]); | |
assert.deepEqual(flatten([1, {foo: 'bar'}]), [1, {foo: 'bar'}]); | |
assert.deepEqual(flatten([1, "foo"]), [1, "foo"]); | |
assert.deepEqual(flatten([1, [2]]), [1, 2]); | |
assert.deepEqual(flatten([1, [2], 3]), [1, 2, 3]); | |
assert.deepEqual(flatten([1, [2], [3, [4, 5]]]), [1, 2, 3, 4, 5]); | |
assert.deepEqual( | |
flatten([1, [2], 3, [[[4], [5, [6, 7]], [8, 9, [10]]]], 11]), | |
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment