Skip to content

Instantly share code, notes, and snippets.

@thardy
Last active May 31, 2019 12:50
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 thardy/3a98b2360b6a85734ae359bea6104467 to your computer and use it in GitHub Desktop.
Save thardy/3a98b2360b6a85734ae359bea6104467 to your computer and use it in GitHub Desktop.
Software Solutions Architect - Remote Application
'use strict';
class FlattenService {
constructor() {}
flattenArray(arrayToBeFlattened, flatArray) {
for (let item of arrayToBeFlattened) {
if (Array.isArray(item)) {
this.flattenArray(item, flatArray);
}
else {
flatArray.push(item);
}
}
}
}
module.exports = FlattenService;
const FlattenService = require('./flattenService');
const chai = require('chai');
const expect = chai.expect;
describe ('flattenArray Works', function () {
let flattenService = {};
before (() => {
flattenService = new FlattenService();
});
after (() => {
});
it ('can flatten an Array of Arrays of numbers', () => {
const myArray = [[1,2,[3]],4];
let flatArray = [];
flattenService.flattenArray(myArray, flatArray);
expect(flatArray).to.deep.equal([1,2,3,4]);
});
// just tinkering - the requirements didn't specifically ask for distinct, but it's easy to add by
// changing the flatArray.push(item); to if (!flatArray.find((x) => x === item)) { flatArray.push(item); }
// it ('can flatten an Array of Arrays of numbers into distinct array of numbers', () => {
// const myArray = [[1,2,[3,1]],4,2];
// let flatArray = [];
//
// flattenService.flattenArray(myArray, flatArray);
//
// expect(flatArray).to.deep.equal([1,2,3,4]);
// });
it ('can flatten an Array of Arrays of strings', () => {
const myArray = [['red','blue',['green']],'yellow'];
let flatArray = [];
flattenService.flattenArray(myArray, flatArray);
expect(flatArray).to.deep.equal(['red','blue','green','yellow']);
});
});
{
"name": "javascripttesting",
"version": "1.0.0",
"description": "",
"main": "src/flattenService.js",
"scripts": {
"test": "./node_modules/.bin/mocha src/**/*.spec.js"
},
"author": "",
"license": "ISC",
"dependencies": {},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^6.1.4"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment