Created
May 4, 2019 22:20
-
-
Save GregIngelmo/a54e371545d4b9fe293353c8c23b6cea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6 */ | |
const assert = require('assert'); | |
// sugar for FlatMapper.prototype.flatMapp | |
class FlatMapper { | |
constructor() { | |
// do stuff | |
} | |
/** | |
* Flatten an array of arrays | |
* @param {Array} arr the array to flatten | |
* @param {Dictionary} opts | |
* @returns {Array} | |
*/ | |
flatMap(arr, opts ) { | |
if (!opts) { | |
opts = { removeEmpty: true }; | |
} | |
if (!arr || arr.length === 0) { | |
return []; | |
} | |
// return arr.flatMap( (x) => x ); | |
let flatArray = []; | |
for (let i=0; i < arr.length; i++) { | |
let val = arr[i]; | |
// check if the element is an array, if it us unwind it | |
if (Array.isArray(val)) { | |
for (let j=0; j < val.length; j++) { | |
this.appendToArray(flatArray, val[j], opts); | |
} | |
} else { | |
this.appendToArray(flatArray, val, opts); | |
} | |
} | |
return flatArray; | |
} | |
appendToArray(arr, val, opts) { | |
if (opts && opts.removeEmpty && | |
(val === null || typeof val === 'undefined')) { | |
return; | |
} | |
arr.push(val); | |
} | |
} | |
// Copy pasta from SO | |
// Ruby makes this so easy :| | |
// Ruby: [1,2,3,4] == [1,2,3,4] == true | |
function arraysEqual(a, b) { | |
if (a === b) return true; | |
if (a == null || b == null) return false; | |
if (a.length != b.length) return false; | |
// If you don't care about the order of the elements inside | |
// the array, you should sort both arrays here. | |
// Please note that calling sort on an array will modify that array. | |
// you might want to clone your array first. | |
for (var i = 0; i < a.length; ++i) { | |
if (a[i] !== b[i]) return false; | |
} | |
return true; | |
} | |
// Mocha recommends against using fat arrow methos | |
// because they prevent the rebinding of this, | |
// but let's live dangerously and use them anyway | |
describe('FlatMapper', () => { | |
let mapper = new FlatMapper(); | |
let tableValuedTest = [ | |
{ | |
data: null, | |
resp: [] | |
}, | |
{ | |
data: [[1], [2,3,4], [5], [6]], | |
resp: [1,2,3,4,5,6] | |
}, | |
{ | |
data: [[1], [2,3,4], [5], [6], 7], | |
resp: [1,2,3,4,5,6,7] | |
}, | |
{ | |
data: [[1], null, [2, null, 3,4], undefined, [5], [6], 7], | |
resp: [1,2,3,4,5,6,7] | |
}, | |
{ | |
data: [[1], null, [2, null, 3,4], undefined, [5], [6], 7], | |
resp: [1,null, 2,null,3,4,undefined, 5,6,7], | |
opts: {removeEmpty: false} | |
} | |
]; | |
it('Should flatten an array of arrays', () => { | |
for (let test of tableValuedTest) { | |
let flatArray = mapper.flatMap(test.data, test.opts); | |
// console.log(flatArray); | |
assert.equal(true, arraysEqual(flatArray, test.resp)); | |
} | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment