Last active
August 26, 2016 06:36
-
-
Save Kirkkt/4c31bb4fe7b28278344ff21faa9c881b to your computer and use it in GitHub Desktop.
js-array-flattener
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
'use strict'; | |
/** | |
* run `node flattenArray.js` in order to run the tests | |
*/ | |
/** | |
* isDeepArray($array): returns whether $array is deep (containing subarrays): | |
* i.e.: | |
* [] -> false | |
* [1, 2, 3] -> false | |
* [[1], 2, 3] -> true | |
* [[[1], 2], 3] -> true | |
* [[[1], [[[2]]]], 3] -> true | |
* [1, [[[[2]]]], 3] -> true | |
*/ | |
const isDeepArray = function($array) { | |
if (!($array instanceof Array)) { | |
return false; | |
} | |
for (const item of $array) { | |
if (item instanceof Array) { | |
return true; | |
} | |
} | |
return false; | |
}; | |
/** | |
* flattenArray($array): returns an flattened array based on $array | |
* i.e.: | |
* [] -> [] | |
* [1, 2, 3] -> [1, 2, 3] | |
* [[1], 2, 3] -> [1, 2, 3] | |
* [[[1], 2], 3] -> [1, 2, 3] | |
* [[[1], [[[2]]]], 3] -> [1, 2, 3] | |
* [1, [[[[2]]]], 3] -> [1, 2, 3] | |
*/ | |
const flattenArray = function($array) { | |
if (!$array || $array.length === 0) { | |
return $array; | |
} | |
if (!isDeepArray($array)) { | |
return $array; | |
} | |
let flattenedArray = []; | |
for (const item of $array) { | |
flattenedArray = flattenedArray.concat(item); | |
} | |
return flattenArray(flattenedArray); | |
}; | |
// no test | |
const testIsDeepArray = function() { | |
const runTest = function(expectedOutput, input) { | |
const actual = isDeepArray(input); | |
if (expectedOutput !== actual) { | |
throw new Error('expecting: ', expectedOutput, '\ngot:', actual); | |
} | |
console.log('passed'); | |
}; | |
runTest(false, []); | |
runTest(false, [1, 2, 3]); | |
runTest(true, [[1], 2, 3]); | |
runTest(true, [[[1], 2], 3]); | |
runTest(true, [[[1], [[[2]]]], 3]); | |
runTest(true, [1, [[[[2]]]], 3]); | |
} | |
// no test | |
const assertArraysDeepEqual = function(expected, actual) { | |
const areArraysDeepEqual = function(expected, actual) { | |
if (expected.length !== actual.length) { | |
return false; | |
} | |
for (const i in expected) { | |
if (expected[i] !== actual[i]) { | |
return false; | |
} | |
} | |
return true; | |
}; | |
if (!areArraysDeepEqual(expected, actual)) { | |
throw new Error('arrays differ:\nexpecting: ' + | |
JSON.stringify(expectedOutput) + | |
'\ngot: ' + | |
JSON.stringify(flattenedArray) | |
); | |
} else { | |
console.log('passed'); | |
} | |
}; | |
// no test | |
const testFlattenArray = function() { | |
const runTest = function(expectedOutput, input) { | |
const flattenedArray = flattenArray(input); | |
assertArraysDeepEqual( | |
expectedOutput, | |
flattenedArray | |
); | |
}; | |
runTest([], []); | |
runTest([1, 2, 3], [1, 2, 3]); | |
runTest([1, 2, 3], [[1], 2, 3]); | |
runTest([1, 2, 3], [[[1], 2], 3]); | |
runTest([1, 2, 3], [[[1], [[[2]]]], 3]); | |
runTest([1, 2, 3], [1, [[[[2]]]], 3]); | |
}; | |
testIsDeepArray(); | |
testFlattenArray(); | |
module.exports = flattenArray; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment