Skip to content

Instantly share code, notes, and snippets.

@alysson-pina
Last active December 29, 2019 13:28
Show Gist options
  • Save alysson-pina/f89ae7dd8e8c977a379d988f72ae497a to your computer and use it in GitHub Desktop.
Save alysson-pina/f89ae7dd8e8c977a379d988f72ae497a to your computer and use it in GitHub Desktop.
Flatten Array Of Integers without using .flat or .flatMap (also provided v2 that accepts arrays of any primitive value)
/**
* Exports function to flatten array of nested arrays into a single level array
* @param {Array} array of arrays of integers (any depth)
* @return {Array} array of integers (one level)
*/
const flatten = arr => arr.reduce( (acc, i) => `${acc + ' ' + i}` ).replace(/,/g, ' ').split(' ').map( i => parseInt(i, 10) );
export default flatten;

$ npm install --save babel-cli babel-preset-es2015 $ npm install --save-dev jasmine .babelrc:

{
  "presets": ["es2015"]
}

package.json:

…
"scripts": {
  "test": "babel-node spec/run.js"
},

spec/run.js:

import Jasmine from 'jasmine'

var jasmine = new Jasmine()
jasmine.loadConfigFile('spec/support/jasmine.json')
jasmine.execute()
/**
* Jasmine Unit Tests
*/
import flatten from './flatten';
describe('Unit tests', () => {
it('should return 1 level array if array is 1 level depth ', () => {
const array = [1,2,3,4,5,6];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat()));
});
it('should return array of one level for 2-level depth array', () => {
const array = [ [1,2,3], [4,5,6] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat()));
});
it('should return array of one level for 3-level depth array', () => {
const array = [ [1,[2,3]], [4,5,6] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(2)));
});
it('should return array of one level for 4-level depth array', () => {
const array = [ [1,[2,[3]]], [4,[5,[6]]] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(3)));
});
it('should return array of one level for 9-level depth array', () => {
const array = [ [1,[2,[[[[[[3]]]]]]]], [4,[5,[6]]] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(8)));
});
});
/**
* Exports function to flatten array of nested arrays into a single level array
* @param {Array} array of arrays of any primitive value (any depth)
* @return {Array} array of given primitive value (one level)
*/
const flattenV2 = arr => {
const newArr = [...arr]; // copy array to not mutate original
const result = []; // passed by reference, stores results iteratively
flatRecursive(newArr, result);
return result;
};
const flatRecursive = (arr, result) => {
if( arr.length === 0 ){ // end of recursion
return;
}
const elem = arr.shift();
if(typeof elem !== 'object'){
result.push(elem);
} else {
flatRecursive(elem, result);
}
flatRecursive(arr, result);
};
export default flattenV2;
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>jasmine unit tests for flaten function</title>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jasmine/1.3.1/jasmine-html.min.js"></script>
</head>
<body>
<script type="text/javascript">
window.onload=function(){
// source code
const flatten = arr => arr.reduce( (acc, i) => `${acc + ' ' + i}` ).replace(/,/g, ' ').split(' ').map( i => parseInt(i, 10) );
// specs code
describe('Unit tests v1', () => {
it('should return 1 level array if array is 1 level depth ', () => {
const array = [1,2,3,4,5,6];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat()));
});
it('should return array of one level for 2-level depth array', () => {
const array = [ [1,2,3], [4,5,6] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat()));
});
it('should return array of one level for 3-level depth array', () => {
const array = [ [1,[2,3]], [4,5,6] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(2)));
});
it('should return array of one level for 4-level depth array', () => {
const array = [ [1,[2,[3]]], [4,[5,[6]]] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(3)));
});
it('should return array of one level for 9-level depth array', () => {
const array = [ [1,[2,[[[[[[3]]]]]]]], [4,[5,[6]]] ];
expect(flatten(array)).toEqual(jasmine.objectContaining(array.flat(8)));
});
});
// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment