Last active
June 18, 2019 19:58
-
-
Save jasonknight/16c512f9047de38bb2b542ad72c57a8a 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
function flatten(array) { | |
// Because we are going to do this recursively, | |
// we start with the "end", in this case, it's any | |
// none-array value, we put it into an array, because | |
// we are recursively "merging" | |
if ( ! Array.isArray(array) ) | |
return [array]; | |
var results = []; | |
for ( var i in array ) { | |
results = results.concat( | |
flatten( array[i] ) | |
); | |
} | |
return results; | |
} | |
// So we could do this on the Array.prototype, | |
// and that's fine, but most likely that example | |
// exists online everywhere, so let's just do this | |
// ourselves the functional way. | |
function arrayCompare(a1,a2) { | |
if ( a1.length != a2.length ) | |
return false; | |
for ( var i in a1 ) { | |
if ( a2[i] != a1[i] ) | |
return false; | |
} | |
return true; | |
} | |
var tests = [ | |
{ | |
have: [1,[2,[3]],4], | |
want: [1,2,3,4] | |
}, | |
{ | |
have: [[[[[[[[[[[1]]]]]]]]]],[2,[3]],4,5,[[[6]]]], | |
want: [1,2,3,4,5,6] | |
}, | |
{ | |
have: [], | |
want: [] | |
}, | |
// This last one might need some explanation. There are a few ways to handle | |
// non-array values being passed in. One is to throw an exception, or return an | |
// error, and another is to be consistent, this function returns a flat array, | |
// null == [null]. It's a design choice. Is passing null really an exception? | |
{ | |
have: null, | |
want: [null] | |
} | |
]; | |
for ( var t in tests ) { | |
var test = tests[t]; | |
if ( !arrayCompare(flatten(test.have),test.want) ) { | |
console.log("Failed to flatten", test.have, "into", test.want, "got", flatten(test.have)); | |
} else { | |
console.log(test.have, "passed"); | |
} | |
} | |
console.log("Begging flattenWithException testing"); | |
// SO some people might take umbridge that I accepted null, and think it's just out | |
// of laziness so: | |
function flattenWithException(array,passThrough) { | |
if ( ! Array.isArray(array) && !passThrough ) | |
throw 'ArgumentException: must pass an array'; | |
// Because we are going to do this recursively, | |
// we start with the "end", in this case, it's any | |
// none-array value, we put it into an array, because | |
// we are recursively "merging" | |
if ( ! Array.isArray(array) && passThrough ) | |
return [array]; | |
var results = []; | |
for ( var i in array ) { | |
results = results.concat( | |
flatten( array[i], true ) | |
); | |
} | |
return results; | |
} | |
var tests = [ | |
{ | |
have: [1,[2,[3]],4], | |
want: [1,2,3,4], | |
exception: null, | |
}, | |
{ | |
have: [[[[[[[[[[[1]]]]]]]]]],[2,[3]],4,5,[[[6]]]], | |
want: [1,2,3,4,5,6], | |
exception: null, | |
}, | |
{ | |
have: [], | |
want: [], | |
exception: null, | |
}, | |
// Now we test to see if there was an exception, just for completeness. | |
{ | |
have: null, | |
want: null, | |
exception: 'ArgumentException: must pass an array', | |
} | |
]; | |
for ( var t in tests ) { | |
var test = tests[t]; | |
try { | |
if ( !arrayCompare(flattenWithException(test.have),test.want) ) { | |
console.log("Failed to flatten", test.have, "into", test.want, "got", flatten(test.have)); | |
} else { | |
console.log(test.have, "passed"); | |
} | |
} catch (except) { | |
if ( test.exception && test.exception == except ) { | |
console.log(test.have, "passed with exception"); | |
} else { | |
console.log("Failed to throw exception", test.exception, "for", test.have); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment