From 02327955926573f7d75cf13be9f5bb35bcff58c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=AA=E6=B5=AA=E6=B5=AA?= <675483520@qq.com> Date: Fri, 10 Aug 2018 15:47:20 +0800 Subject: [PATCH 1/3] Update symmetricDifference No const One time new Set() One line code style --- snippets/symmetricDifference.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/snippets/symmetricDifference.md b/snippets/symmetricDifference.md index 6f13806e4..1f76f8f25 100644 --- a/snippets/symmetricDifference.md +++ b/snippets/symmetricDifference.md @@ -5,11 +5,9 @@ Returns the symmetric difference between two arrays. Create a `Set` from each array, then use `Array.filter()` on each of them to only keep values not contained in the other. ```js -const symmetricDifference = (a, b) => { - const sA = new Set(a), - sB = new Set(b); - return [...a.filter(x => !sB.has(x)), ...b.filter(x => !sA.has(x))]; -}; +const symmetricDifference = (a, b) => [ + ...new Set([...a.filter(v => !b.includes(v)), ...b.filter(v => !a.includes(v))]) +]; ``` ```js From 4de6aceb044a0f38961f2709cf29be17e79c4c27 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 17 Aug 2018 08:37:08 +0300 Subject: [PATCH 2/3] Added uniqueSymmetricDifference and updated symmetricDifference --- snippets/symmetricDifference.md | 11 +++++++---- snippets/uniqueSymmetricDifference.md | 16 ++++++++++++++++ tag_database | 1 + .../uniqueSymmetricDifference.js | 4 ++++ .../uniqueSymmetricDifference.test.js | 12 ++++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 snippets/uniqueSymmetricDifference.md create mode 100644 test/uniqueSymmetricDifference/uniqueSymmetricDifference.js create mode 100644 test/uniqueSymmetricDifference/uniqueSymmetricDifference.test.js diff --git a/snippets/symmetricDifference.md b/snippets/symmetricDifference.md index 1f76f8f25..2414218bd 100644 --- a/snippets/symmetricDifference.md +++ b/snippets/symmetricDifference.md @@ -5,11 +5,14 @@ Returns the symmetric difference between two arrays. Create a `Set` from each array, then use `Array.filter()` on each of them to only keep values not contained in the other. ```js -const symmetricDifference = (a, b) => [ - ...new Set([...a.filter(v => !b.includes(v)), ...b.filter(v => !a.includes(v))]) -]; +const symmetricDifference = (a, b) => { + const sA = new Set(a), + sB = new Set(b); + return [...a.filter(x => !sB.has(x)), ...b.filter(x => !sA.has(x))]; +}; ``` ```js -symmetricDifference([1, 2, 3], [1, 2, 4]); // [3,4] +symmetricDifference([1, 2, 3], [1, 2, 4]); // [3, 4] +symmetricDifference([1, 2, 2], [1, 3, 1]); // [2, 2, 3] ``` diff --git a/snippets/uniqueSymmetricDifference.md b/snippets/uniqueSymmetricDifference.md new file mode 100644 index 000000000..8d2f22468 --- /dev/null +++ b/snippets/uniqueSymmetricDifference.md @@ -0,0 +1,16 @@ +### uniqueSymmetricDifference + +Returns the unique symmetric difference between two arrays. + +Use `Array.filter()` and `Array.includes()` on each array to remove values contained in the other, then create a `Set` from the results, removing duplicate values. + +```js +const uniqueSymmetricDifference = (a, b) => [ + ...new Set([...a.filter(v => !b.includes(v)), ...b.filter(v => !a.includes(v))]) +]; +``` + +```js +uniqueSymmetricDifference([1, 2, 3], [1, 2, 4]); // [3, 4] +uniqueSymmetricDifference([1, 2, 2], [1, 3, 1]); // [2, 3] +``` diff --git a/tag_database b/tag_database index 92f0c81ad..25701b373 100644 --- a/tag_database +++ b/tag_database @@ -302,6 +302,7 @@ unionWith:array,function uniqueElements:array uniqueElementsBy:array,function uniqueElementsByRight:array,function +uniqueSymmetricDifference:array,math untildify:node,string unzip:array unzipWith:array,function,advanced diff --git a/test/uniqueSymmetricDifference/uniqueSymmetricDifference.js b/test/uniqueSymmetricDifference/uniqueSymmetricDifference.js new file mode 100644 index 000000000..62cb18a5f --- /dev/null +++ b/test/uniqueSymmetricDifference/uniqueSymmetricDifference.js @@ -0,0 +1,4 @@ +const uniqueSymmetricDifference = (a, b) => [ + ...new Set([...a.filter(v => !b.includes(v)), ...b.filter(v => !a.includes(v))]) +]; +module.exports = uniqueSymmetricDifference; diff --git a/test/uniqueSymmetricDifference/uniqueSymmetricDifference.test.js b/test/uniqueSymmetricDifference/uniqueSymmetricDifference.test.js new file mode 100644 index 000000000..788d1f65e --- /dev/null +++ b/test/uniqueSymmetricDifference/uniqueSymmetricDifference.test.js @@ -0,0 +1,12 @@ +const expect = require('expect'); +const uniqueSymmetricDifference = require('./uniqueSymmetricDifference.js'); + +test('uniqueSymmetricDifference is a Function', () => { + expect(uniqueSymmetricDifference).toBeInstanceOf(Function); +}); +test('Returns the symmetric difference between two arrays.', () => { + expect(uniqueSymmetricDifference([1, 2, 3], [1, 2, 4])).toEqual([3, 4]); +}); +test('Does not return duplicates from one array', () => { + expect(uniqueSymmetricDifference([1, 2, 2], [1, 3, 1])).toEqual([2, 3]); +}); From 609f80850ff94b034650a8bb476325129308c957 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Fri, 17 Aug 2018 08:39:04 +0300 Subject: [PATCH 3/3] Improved descriptions --- snippets/symmetricDifference.md | 2 +- snippets/uniqueSymmetricDifference.md | 2 +- test/symmetricDifference/symmetricDifference.test.js | 3 +++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/snippets/symmetricDifference.md b/snippets/symmetricDifference.md index 2414218bd..854af0f30 100644 --- a/snippets/symmetricDifference.md +++ b/snippets/symmetricDifference.md @@ -1,6 +1,6 @@ ### symmetricDifference -Returns the symmetric difference between two arrays. +Returns the symmetric difference between two arrays, without filtering out duplicate values. Create a `Set` from each array, then use `Array.filter()` on each of them to only keep values not contained in the other. diff --git a/snippets/uniqueSymmetricDifference.md b/snippets/uniqueSymmetricDifference.md index 8d2f22468..99afdadfe 100644 --- a/snippets/uniqueSymmetricDifference.md +++ b/snippets/uniqueSymmetricDifference.md @@ -1,6 +1,6 @@ ### uniqueSymmetricDifference -Returns the unique symmetric difference between two arrays. +Returns the unique symmetric difference between two arrays, not containing duplicate values from either array. Use `Array.filter()` and `Array.includes()` on each array to remove values contained in the other, then create a `Set` from the results, removing duplicate values. diff --git a/test/symmetricDifference/symmetricDifference.test.js b/test/symmetricDifference/symmetricDifference.test.js index 7fc364488..c96e3b5a6 100644 --- a/test/symmetricDifference/symmetricDifference.test.js +++ b/test/symmetricDifference/symmetricDifference.test.js @@ -7,3 +7,6 @@ test('symmetricDifference is a Function', () => { test('Returns the symmetric difference between two arrays.', () => { expect(symmetricDifference([1, 2, 3], [1, 2, 4])).toEqual([3, 4]); }); +test('Returns duplicates from one array', () => { + expect(symmetricDifference([1, 2, 2], [1, 3, 1])).toEqual([2, 2, 3]); +});