Skip to content

Instantly share code, notes, and snippets.

@radekstepan
Last active March 26, 2016 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save radekstepan/299bb37ce9bd3497909c to your computer and use it in GitHub Desktop.
Save radekstepan/299bb37ce9bd3497909c to your computer and use it in GitHub Desktop.
array-flatten
/node_modules
*.log
.DS_Store
language: node_js

#array-flatten

Will flatten an array of arbitrarily nested arrays of integers into a flat array of integers.

$ nvm use
$ npm install
$ npm test
{
"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 <dev@radekstepan.com>",
"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();
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment