Created
October 8, 2018 17:37
-
-
Save farynaio/2ccfa3e8c00972bf78f568bde050c4e6 to your computer and use it in GitHub Desktop.
Node.js 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 { expect } = require("chai") | |
function flatten(array) { | |
return array.reduce((acc, val) => acc.concat(Array.isArray(val) ? flatten(val) : val), []) | |
} | |
describe("#flatten", () => { | |
it("should return flattened array", () => { | |
const input = [[1, 2, [3]], 4] | |
expect(flatten(input)).to.deep.equal([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
{ | |
"name": "flatten", | |
"version": "1.0.0", | |
"description": "Node.js", | |
"scripts": { | |
"test": "mocha index.js" | |
}, | |
"keywords": [ | |
"Javascript", | |
"Mocha", | |
"Chai" | |
], | |
"author": "Adam Faryna", | |
"license": "MIT", | |
"dependencies": { | |
"chai": "^4.2.0", | |
"mocha": "^5.2.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment