From ba13cd5a9b47eaa7fe4108a3f09d7a1b5c52ce26 Mon Sep 17 00:00:00 2001 From: Calum Patrick Date: Sat, 16 Nov 2019 23:46:08 -0500 Subject: [PATCH] replace usage of unshift with concat --- lib/index.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/lib/index.js b/lib/index.js index cd27d60..769803b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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)