Skip to content

Instantly share code, notes, and snippets.

@resistdesign
Last active July 17, 2017 19:00
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 resistdesign/2aec3c0bd5bdff24e5019b642f08e150 to your computer and use it in GitHub Desktop.
Save resistdesign/2aec3c0bd5bdff24e5019b642f08e150 to your computer and use it in GitHub Desktop.
Flatten Int Array
.idea/
node_modules/

Flatten Int Array

Flatten an arbitrarily nested array of integers.

Prerequisits

NodeJS 8.x.x https://nodejs.org (It comes with NPM!)

Installation

npm i -S this is a demo so don't install it

Tests

Mocha Exports Interface with Babel Register

Run: npm test

SETUP

Move files prefixed with src_ to a directory named src and remove the prefix.

This is required because Gists do not allow a folder structure and the intention of this Gist is to reflect the structure of an actual production module.

Run npm i

{
"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
]);
}
}
};
@resistdesign
Copy link
Author

If you want, you can generate documentation with something like this: http://documentation.js.org

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment