From dfb54a297723a987089512f05739df61024d7642 Mon Sep 17 00:00:00 2001 From: Abhimanyu Singh Date: Sun, 4 Feb 2024 11:37:52 +0530 Subject: [PATCH 1/4] (fix)[flatten-array]: Enable Flattening of all nested arrays in the array --- packages/array-flatten/index.cjs | 30 +++++++++++++++++++++--------- test/array-flatten/index.cjs | 10 +++++----- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/packages/array-flatten/index.cjs b/packages/array-flatten/index.cjs index 78f053369..b11ffdd27 100644 --- a/packages/array-flatten/index.cjs +++ b/packages/array-flatten/index.cjs @@ -7,20 +7,32 @@ module.exports = flatten; function flattenHelper(arr, depth) { var stack = arr.slice(); - var result = []; + var stackContainsArray = false; - while (stack.length) { - var item = stack.pop(); + for (var i=0; i 0) { - stack.push.apply(stack, item); - depth--; - } else { - result.push(item); + for (; depth > 0 && stackContainsArray; depth--) { + stackContainsArray = false; + var nextStack = [] + + while (stack.length) { + var item = stack.shift(); + if (Array.isArray(item)) { + stackContainsArray = true; + nextStack.push.apply(nextStack, item); + } else { + nextStack.push(item); + } } + + stack = nextStack; } - return result.reverse(); + return stack; } function flatten(arr, depth) { diff --git a/test/array-flatten/index.cjs b/test/array-flatten/index.cjs index babcf5a48..cd0ce8660 100644 --- a/test/array-flatten/index.cjs +++ b/test/array-flatten/index.cjs @@ -40,12 +40,12 @@ test('returns a new array with all sub-arrays items concatenated into it up to t function(t) { t.plan(5); - var arr = [1, [2, [3, [4, [5]]]]]; + var arr = [[1, [2, 3], 4], [5, [6, [7, [8]]]]]; t.deepEqual(flatten(arr, 0), arr); - t.deepEqual(flatten(arr, 1), [1, 2, [3, [4, [5]]]]); - t.deepEqual(flatten(arr, 2), [1, 2, 3, [4, [5]]]); - t.deepEqual(flatten(arr, 3), [1, 2, 3, 4, [5]]); - t.deepEqual(flatten(arr, 4), [1, 2, 3, 4, 5]); + t.deepEqual(flatten(arr, 1), [1, [2, 3], 4, 5, [6, [7, [8]]]]); + t.deepEqual(flatten(arr, 2), [1, 2, 3, 4, 5, 6, [7, [8]]]); + t.deepEqual(flatten(arr, 3), [1, 2, 3, 4, 5, 6, 7, [8]]); + t.deepEqual(flatten(arr, 4), [1, 2, 3, 4, 5, 6, 7, 8]); t.end(); }); From 25fa02b123372abf06a5060faeb37235510cbf76 Mon Sep 17 00:00:00 2001 From: Abhimanyu Singh Date: Sun, 4 Feb 2024 11:40:40 +0530 Subject: [PATCH 2/4] Update mjs file --- packages/array-flatten/index.mjs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/array-flatten/index.mjs b/packages/array-flatten/index.mjs index f109b5d9c..1d1056b0b 100644 --- a/packages/array-flatten/index.mjs +++ b/packages/array-flatten/index.mjs @@ -7,20 +7,32 @@ var arrayFlatten = flatten; function flattenHelper(arr, depth) { var stack = arr.slice(); - var result = []; + var stackContainsArray = false; - while (stack.length) { - var item = stack.pop(); + for (var i=0; i 0) { - stack.push.apply(stack, item); - depth--; - } else { - result.push(item); + for (; depth > 0 && stackContainsArray; depth--) { + stackContainsArray = false; + var nextStack = []; + + while (stack.length) { + var item = stack.shift(); + if (Array.isArray(item)) { + stackContainsArray = true; + nextStack.push.apply(nextStack, item); + } else { + nextStack.push(item); + } } + + stack = nextStack; } - return result.reverse(); + return stack; } function flatten(arr, depth) { From dd2b0698195247feacb27d0d19039cfbffaef0b3 Mon Sep 17 00:00:00 2001 From: Abhimanyu Singh Date: Sun, 4 Feb 2024 12:16:46 +0530 Subject: [PATCH 3/4] Add break in first loop to optimise --- packages/array-flatten/index.cjs | 3 ++- packages/array-flatten/index.mjs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/array-flatten/index.cjs b/packages/array-flatten/index.cjs index b11ffdd27..61445365e 100644 --- a/packages/array-flatten/index.cjs +++ b/packages/array-flatten/index.cjs @@ -9,9 +9,10 @@ function flattenHelper(arr, depth) { var stack = arr.slice(); var stackContainsArray = false; - for (var i=0; i Date: Sun, 4 Feb 2024 14:11:22 +0530 Subject: [PATCH 4/4] Skip additional for loop --- packages/array-flatten/index.cjs | 9 +-------- packages/array-flatten/index.mjs | 9 +-------- 2 files changed, 2 insertions(+), 16 deletions(-) diff --git a/packages/array-flatten/index.cjs b/packages/array-flatten/index.cjs index 61445365e..f81a4727f 100644 --- a/packages/array-flatten/index.cjs +++ b/packages/array-flatten/index.cjs @@ -7,14 +7,7 @@ module.exports = flatten; function flattenHelper(arr, depth) { var stack = arr.slice(); - var stackContainsArray = false; - - for (var i = 0; i < stack.length; i++) { - if (Array.isArray(stack[i])) { - stackContainsArray = true; - break; - } - } + var stackContainsArray = true; for (; depth > 0 && stackContainsArray; depth--) { stackContainsArray = false; diff --git a/packages/array-flatten/index.mjs b/packages/array-flatten/index.mjs index 2f6594e31..4e992be88 100644 --- a/packages/array-flatten/index.mjs +++ b/packages/array-flatten/index.mjs @@ -7,14 +7,7 @@ var arrayFlatten = flatten; function flattenHelper(arr, depth) { var stack = arr.slice(); - var stackContainsArray = false; - - for (var i = 0; i < stack.length; i++) { - if (Array.isArray(stack[i])) { - stackContainsArray = true; - break; - } - } + var stackContainsArray = true; for (; depth > 0 && stackContainsArray; depth--) { stackContainsArray = false;