Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 4 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,11 @@ module.exports = function flatten (array) {
// only process `value` if it has some elements.
if (value.length > 0) {

// to provide the `value` array to splice() we need to add the
// splice() args to its front.
// these args tell it to splice at `i` and delete what's at `i`.
value.unshift(i, 1)

// NOTE:
// This is an in-place change; it mutates `array`.
// prepend our value with [i,1] to tell splice to
// start splicing at `i` and delete the element at `i`.
// NOTE: This is an in-place change; it mutates `array`.
// To avoid this, wrap your array like: flatten([myarray])
array.splice.apply(array, value)

// take (i, 1) back off the `value` front
// so it remains "unchanged".
value.splice(0, 2)
array.splice.apply(array, [i,1].concat(value))
} else {
// remove an empty array from `array`
array.splice(i, 1)
Expand Down