Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix)[flatten-array]: Enable Flattening of all nested arrays in the array #577

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
30 changes: 21 additions & 9 deletions packages/array-flatten/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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<stack.length; i++) {
if (Array.isArray(stack[i])) {
stackContainsArray = true;
}
}
superhit0 marked this conversation as resolved.
Show resolved Hide resolved

if (Array.isArray(item) && depth > 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) {
Expand Down
30 changes: 21 additions & 9 deletions packages/array-flatten/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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<stack.length; i++) {
if (Array.isArray(stack[i])) {
stackContainsArray = true;
}
}

if (Array.isArray(item) && depth > 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) {
Expand Down
10 changes: 5 additions & 5 deletions test/array-flatten/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down