Flatten an arbitrarily nested array of integers.
NodeJS 8.x.x
https://nodejs.org (It comes with NPM!)
npm i -S this is a demo so don't install it
Mocha Exports Interface with Babel Register
Run: npm test
.idea/ | |
node_modules/ |
Flatten an arbitrarily nested array of integers.
NodeJS 8.x.x
https://nodejs.org (It comes with NPM!)
npm i -S this is a demo so don't install it
Mocha Exports Interface with Babel Register
Run: npm test
{ | |
"name": "flatten-int-array", | |
"version": "0.0.0", | |
"description": "Flatten an arbitrarily nested array of integers.", | |
"main": "src/index.jsx", | |
"scripts": { | |
"test": "mocha --require resistdesign-babel-register --ui exports ./src/**/*.spec.jsx" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git+ssh://git@gist.github.com/2aec3c0bd5bdff24e5019b642f08e150.git" | |
}, | |
"keywords": [ | |
"JavaScript", | |
"ES6", | |
"Array", | |
"Int", | |
"Flatten" | |
], | |
"author": "", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://gist.github.com/2aec3c0bd5bdff24e5019b642f08e150" | |
}, | |
"homepage": "https://gist.github.com/2aec3c0bd5bdff24e5019b642f08e150", | |
"devDependencies": { | |
"expect.js": "^0.3.1", | |
"resistdesign-babel-register": "^1.0.1" | |
} | |
} |
function isInt (value) { | |
return Number.isInteger(value); | |
} | |
/** | |
* Flatten an arbitrarily nested array of integers. | |
* @param {Array.<number|Array>} input The array to flatten. | |
* @returns {Array.<number>} The flattened array containing only integers. | |
* */ | |
export default function flattenIntArray (input) { | |
// Always return an array per the documented contract. | |
let output = []; | |
if (input instanceof Array) { | |
input.forEach(value => { | |
if (isInt(value)) { | |
// Value is an integer | |
output.push(value); | |
} else { | |
// Not an integer | |
output = [ | |
...output, | |
// Flat an clean possibly nested values | |
...flattenIntArray(value) | |
]; | |
} | |
}); | |
} | |
return output; | |
} |
import expect from 'expect.js'; | |
import flattenIntArray from './index'; | |
const MOCK_DATA = { | |
SIMPLE_INT_ARRAY: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], | |
MIXED_INT_FLOAT_ARRAY: [47.2, 99, 29.87, 5, 19, 87.0125, 22], | |
MIX_TYPE_ARRAY: [{}, 'hello', 21, 'K', 67, [], 19.75, '144', 'next', {}, 4], | |
MULTI_NESTED_INT_ARRAY: [ | |
22, | |
77, | |
112, | |
[ | |
17, | |
4729 | |
], | |
88, | |
[ | |
77865, | |
22425, | |
[ | |
879898, | |
3 | |
], | |
9992 | |
], | |
17, | |
[], | |
983472983 | |
] | |
}; | |
module.exports = { | |
'flattenIntArray': { | |
'should be a function': () => { | |
expect(flattenIntArray).to.be.a(Function); | |
}, | |
'should return an array of integers': () => { | |
const output = flattenIntArray( | |
MOCK_DATA.SIMPLE_INT_ARRAY | |
); | |
expect(output).to.be.an(Array); | |
expect(output).to.have.length( | |
MOCK_DATA.SIMPLE_INT_ARRAY.length | |
); | |
output.forEach((value, index) => { | |
const sourceValue = MOCK_DATA.SIMPLE_INT_ARRAY[index]; | |
expect(value).to.equal(sourceValue); | |
}); | |
}, | |
'should not include floats in output': () => { | |
const output = flattenIntArray( | |
MOCK_DATA.MIXED_INT_FLOAT_ARRAY | |
); | |
expect(output).to.be.an(Array); | |
expect(output).to.eql([99, 5, 19, 22]); | |
}, | |
'should not include non-numeric values in output': () => { | |
const output = flattenIntArray( | |
MOCK_DATA.MIX_TYPE_ARRAY | |
); | |
expect(output).to.be.an(Array); | |
expect(output).to.eql([21, 67, 4]); | |
}, | |
'should flatten an arbitrarily nested array of integers': () => { | |
const output = flattenIntArray( | |
MOCK_DATA.MULTI_NESTED_INT_ARRAY | |
); | |
expect(output).to.be.an(Array); | |
expect(output).to.eql([ | |
22, | |
77, | |
112, | |
17, | |
4729, | |
88, | |
77865, | |
22425, | |
879898, | |
3, | |
9992, | |
17, | |
983472983 | |
]); | |
} | |
} | |
}; |
If you want, you can generate documentation with something like this: http://documentation.js.org