Last active
January 20, 2020 03:20
-
-
Save jasmo2/9f3098f8c65db3fa846b7d364a0692e1 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 isFlattenable(value) { | |
return Array.isArray(value) | |
} | |
function flattenImplementation (array, depth = Infinity, isStrict, result = []) { | |
let index = -1 | |
let length = array.length | |
while (++index < length) { | |
var value = array[index] | |
if (depth > 0 && isFlattenable(value)) { | |
if (depth > 1) { | |
flattenImplementation(value, depth - 1, isStrict, result) | |
} else { | |
result.push(value) | |
} | |
} else if (!isStrict) { | |
result[result.length] = value | |
} | |
} | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment