From 72e51661b2d985a1d7f0c9393cca5f7af339773c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Thu, 11 Jan 2018 09:40:22 +0100 Subject: [PATCH 1/5] update flatten to use same syntax as flattenDepth. Delete flattenDepth. --- snippets/flatten.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/snippets/flatten.md b/snippets/flatten.md index a31bbe2d0..708703f58 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -1,11 +1,17 @@ ### 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 element, `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 From 41001626c72b3e684fa4855e7dafdb67d711bf7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Thu, 11 Jan 2018 09:42:11 +0100 Subject: [PATCH 2/5] prettify example --- snippets/flatten.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/flatten.md b/snippets/flatten.md index 708703f58..a4ca535b4 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -15,5 +15,5 @@ const flatten = (arr, depth = 1) => ``` ```js -flatten([1, [2], 3, 4]); // [1,2,3,4] +flatten([1, [2], 3, 4]); // [1, 2, 3, 4] ``` From ce4540f71d77938edfacd7c1b82bb9cf45d19111 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Thu, 11 Jan 2018 09:51:49 +0100 Subject: [PATCH 3/5] run tdd, update test cases for flatten. --- snippets/flatten.md | 1 + test/flatten/flatten.js | 5 ++++- test/flatten/flatten.test.js | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/snippets/flatten.md b/snippets/flatten.md index a4ca535b4..2154971a9 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -16,4 +16,5 @@ const flatten = (arr, depth = 1) => ```js 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'); From 498fe1797735285293e480b63d555aac3fec2acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Thu, 11 Jan 2018 09:55:24 +0100 Subject: [PATCH 4/5] remove flattenDepth tests --- test/flattenDepth/flattenDepth.js | 4 ---- test/flattenDepth/flattenDepth.test.js | 14 -------------- 2 files changed, 18 deletions(-) delete mode 100644 test/flattenDepth/flattenDepth.js delete mode 100644 test/flattenDepth/flattenDepth.test.js 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 From b2628f8ff34b3d17cfd392ee493efa1d36b9fa88 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 11 Jan 2018 10:56:14 +0200 Subject: [PATCH 5/5] Update flatten.md --- snippets/flatten.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/snippets/flatten.md b/snippets/flatten.md index 2154971a9..63bec4b0d 100644 --- a/snippets/flatten.md +++ b/snippets/flatten.md @@ -5,7 +5,7 @@ Flattens an array up to the specified depth. 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 element, `depth` to flatten only to a depth of `1` (single flatten). +Omit the second argument, `depth` to flatten only to a depth of `1` (single flatten). ```js const flatten = (arr, depth = 1) =>