diff --git a/snippets/flatten.md b/snippets/flatten.md index a31bbe2d0..63bec4b0d 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -1,13 +1,20 @@ ### flatten -Flattens an array. +Flattens an array up to the specified depth. -Use a new array, `Array.concat()` and the spread operator (`...`) to cause a shallow denesting of any contained arrays. +Use recursion, decrementing `depth` by 1 for each level of depth. +Use `Array.reduce()` and `Array.concat()` to merge elements or arrays. +Base case, for `depth` equal to `1` stops recursion. +Omit the second argument, `depth` to flatten only to a depth of `1` (single flatten). ```js -const flatten = arr => [].concat(...arr); +const flatten = (arr, depth = 1) => + depth != 1 + ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), []) + : arr.reduce((a, v) => a.concat(v), []); ``` ```js -flatten([1, [2], 3, 4]); // [1,2,3,4] +flatten([1, [2], 3, 4]); // [1, 2, 3, 4] +flatten([1, [2, [3, [4, 5], 6], 7], 8], 2); // [1, 2, 3, [4, 5], 6, 7, 8] ``` diff --git a/test/flatten/flatten.js b/test/flatten/flatten.js index 845b9c916..727e22a60 100644 --- a/test/flatten/flatten.js +++ b/test/flatten/flatten.js @@ -1 +1,4 @@ -module.exports = flatten = arr => [].concat(...arr); \ No newline at end of file +module.exports = flatten = (arr, depth = 1) => +depth != 1 +? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flatten(v, depth - 1) : v), []) +: arr.reduce((a, v) => a.concat(v), []); \ No newline at end of file diff --git a/test/flatten/flatten.test.js b/test/flatten/flatten.test.js index 61ae1f055..e7741039f 100644 --- a/test/flatten/flatten.test.js +++ b/test/flatten/flatten.test.js @@ -6,6 +6,7 @@ test('Testing flatten', (t) => { //Please go to https://github.com/substack/tape t.true(typeof flatten === 'function', 'flatten is a Function'); t.deepEqual(flatten([1, [2], 3, 4]), [1, 2, 3, 4], "Flattens an array"); + t.deepEqual(flatten([1, [2, [3, [4, 5], 6], 7], 8], 2), [1, 2, 3, [4, 5], 6, 7, 8], "Flattens an array"); //t.deepEqual(flatten(args..), 'Expected'); //t.equal(flatten(args..), 'Expected'); //t.false(flatten(args..), 'Expected'); diff --git a/test/flattenDepth/flattenDepth.js b/test/flattenDepth/flattenDepth.js deleted file mode 100644 index 9e4606a75..000000000 --- a/test/flattenDepth/flattenDepth.js +++ /dev/null @@ -1,4 +0,0 @@ -module.exports = flattenDepth = (arr, depth = 1) => -depth != 1 -? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), []) -: arr.reduce((a, v) => a.concat(v), []); \ No newline at end of file diff --git a/test/flattenDepth/flattenDepth.test.js b/test/flattenDepth/flattenDepth.test.js deleted file mode 100644 index c8794979b..000000000 --- a/test/flattenDepth/flattenDepth.test.js +++ /dev/null @@ -1,14 +0,0 @@ -const test = require('tape'); -const flattenDepth = require('./flattenDepth.js'); - -test('Testing flattenDepth', (t) => { - //For more information on all the methods supported by tape - //Please go to https://github.com/substack/tape - t.true(typeof flattenDepth === 'function', 'flattenDepth is a Function'); - t.deepEqual(flattenDepth([1, [2], 3, 4]), [1, 2, 3, 4], "Flattens an array up to the specified depth"); - //t.deepEqual(flattenDepth(args..), 'Expected'); - //t.equal(flattenDepth(args..), 'Expected'); - //t.false(flattenDepth(args..), 'Expected'); - //t.throws(flattenDepth(args..), 'Expected'); - t.end(); -}); \ No newline at end of file