Skip to content

Instantly share code, notes, and snippets.

@jasmo2
Last active January 20, 2020 03:20
Show Gist options
  • Save jasmo2/9f3098f8c65db3fa846b7d364a0692e1 to your computer and use it in GitHub Desktop.
Save jasmo2/9f3098f8c65db3fa846b7d364a0692e1 to your computer and use it in GitHub Desktop.
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