#array-flatten
Will flatten an array of arbitrarily nested arrays of integers into a flat array of integers.
$ nvm use
$ npm install
$ npm test
/node_modules | |
*.log | |
.DS_Store |
v4.3.1 |
language: node_js |
{ | |
"simple": [ | |
[ 1, [ 2, 3 ] ], | |
[ 1, 2, 3 ] | |
], | |
"citrus": [ | |
[ [ 1, 2, [ 3 ] ], 4 ], | |
[ 1, 2, 3, 4 ] | |
], | |
"flat": [ | |
5, | |
[ 5 ] | |
], | |
"empty": [ | |
[], | |
[] | |
], | |
"rabbit-hole": [ | |
[ [ [] ] ], | |
[] | |
] | |
} |
"use strict"; | |
let lib = (input, output) => { | |
if (Array.isArray(input)) { | |
for (let i of input) lib(i, output); | |
} else { | |
output.push(input); | |
} | |
return output; | |
}; | |
module.exports = (input) => lib(input, []); |
{ | |
"name": "array-flatten", | |
"version": "1.0.0", | |
"author": "Radek Stepan <[email protected]>", | |
"main": "index.js", | |
"devDependencies": { | |
"chai": "^3.5.0", | |
"mocha": "^2.4.5" | |
}, | |
"scripts": { | |
"test": "./node_modules/.bin/mocha test.js --ui exports --bail --reporter spec" | |
} | |
} |
"use strict"; | |
let assert = require('chai').assert; | |
let lib = require('./index.js'); | |
let fix = require('./fixtures.json'); | |
for (let i in fix) { | |
exports[`test ${i}`] = (done) => { | |
assert.deepEqual(lib(fix[i][0]), fix[i][1]); | |
done(); | |
}; | |
} |