Created
November 5, 2019 21:05
-
-
Save jcollum/910f507120a57045dd16699424511aca to your computer and use it in GitHub Desktop.
flatten
This file contains hidden or 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
/* | |
assumption: result is initialized in outer scope before first call | |
*/ | |
const flatten = (input, result) => { | |
// check inputs | |
if (!Array.isArray(input)) | |
throw new Error("Input to this function is required to be an array"); | |
input.forEach(element => { | |
// if element is an array, recurse it | |
if (Array.isArray(element)) flatten(element, result); | |
// check the input, make sure it's a number | |
else if (!Number.isInteger(+element)) | |
throw new Error( | |
`Element in array is expect to be an integer, got ${element}` | |
); | |
// all good, push to the result | |
else result.push(Number.parseInt(element)); | |
}); | |
// and done | |
}; | |
module.exports = flatten; |
This file contains hidden or 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
// run with node flatten.test.js | |
// result should say "Test complete. All passed." when done | |
const assert = require("assert"); | |
const flatten = require("../src/flatten"); | |
console.log("Test running..."); | |
// declare a variable to hold results of test (for logging) | |
let r; | |
let expected; | |
assert.throws(() => flatten(), "Empty input should throw error"); | |
assert.throws( | |
() => flatten(["a"]), | |
"Array with non integer should throw error" | |
); | |
assert.throws(() => flatten([{}]), "Array with non integer should throw error"); | |
assert.throws( | |
() => flatten(["1", "a"]), | |
"Array with non integer should throw error" | |
); | |
assert.throws( | |
() => flatten(["1", true]), | |
"Array with boolean should throw error" | |
); | |
assert.throws( | |
() => flatten(["1", () => {}]), | |
"Array with function should throw error" | |
); | |
assert.throws( | |
() => flatten(["1", new Symbol()]), | |
"Array with Symbol should throw error" | |
); | |
assert.throws( | |
() => flatten(["1", BigInt(4)]), | |
"Array with BigInt should throw error" | |
); | |
assert.throws( | |
() => flatten(["1", new Symbol()]), | |
"Array with Symbol should throw error" | |
); | |
assert.throws(() => flatten(["1", null]), "Array with null should throw error"); | |
assert.throws( | |
() => flatten(["1", undefined]), | |
"Array with undefined should throw error" | |
); | |
r = []; | |
flatten([1], r); | |
expected = [1]; | |
assert.deepStrictEqual( | |
r, | |
expected, | |
`single item input array did not match expected output, received ${r}; expected ${expected}` | |
); | |
r = []; | |
flatten([1, [2]], r); | |
expected = [1, 2]; | |
assert.deepStrictEqual( | |
r, | |
expected, | |
`1 level nested input array did not match expected output, received ${r}; expected ${expected}` | |
); | |
r = []; | |
expected = [1, 2, 3]; | |
flatten([1, [2, [3]]], r); | |
assert.deepStrictEqual( | |
r, | |
expected, | |
`2 level nested input array did not match expected output, received ${r}; expected ${expected}` | |
); | |
r = []; | |
expected = [1, 2, 3, 4, 5]; | |
flatten([1, [2, [3, [4]]], 5], r); | |
assert.deepStrictEqual( | |
r, | |
expected, | |
`3 level nested input array did not match expected output, received ${r}; expected ${expected}` | |
); | |
r = []; | |
expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; | |
flatten([1, [2, [3, [4, 5, [6], 7], 8], 9], 10], r); | |
assert.deepStrictEqual( | |
r, | |
expected, | |
`3 level nested/mixed input array did not match expected output, received ${r}; expected ${expected}` | |
); | |
r = []; | |
expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 50000, 5, 6]; | |
flatten([1, [2, [3, [4, 5, [6], 7], 8], 9], 10, 1, [[50000, [5], 6]]], r); | |
assert.deepStrictEqual( | |
r, | |
expected, | |
`3 level nested/mixed input array did not match expected output, received ${r}; expected ${expected}` | |
); | |
console.log("Test complete. All passed."); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment