From b129b46c0d158bf3da0d765053628d271d2a0571 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Wed, 18 Jul 2018 21:02:42 +0300 Subject: [PATCH] Tags and tests --- snippets/uniqueElementsByRight.md | 2 +- tag_database | 3 ++ test/filterNonUniqueBy/filterNonUniqueBy.js | 3 ++ .../filterNonUniqueBy.test.js | 32 +++++++++++++++++++ test/uniqueElementsBy/uniqueElementsBy.js | 6 ++++ .../uniqueElementsBy/uniqueElementsBy.test.js | 32 +++++++++++++++++++ .../uniqueElementsByRight.js | 6 ++++ .../uniqueElementsByRight.test.js | 32 +++++++++++++++++++ 8 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 test/filterNonUniqueBy/filterNonUniqueBy.js create mode 100644 test/filterNonUniqueBy/filterNonUniqueBy.test.js create mode 100644 test/uniqueElementsBy/uniqueElementsBy.js create mode 100644 test/uniqueElementsBy/uniqueElementsBy.test.js create mode 100644 test/uniqueElementsByRight/uniqueElementsByRight.js create mode 100644 test/uniqueElementsByRight/uniqueElementsByRight.test.js diff --git a/snippets/uniqueElementsByRight.md b/snippets/uniqueElementsByRight.md index 42b351438..791c9621a 100644 --- a/snippets/uniqueElementsByRight.md +++ b/snippets/uniqueElementsByRight.md @@ -7,7 +7,7 @@ The comparator function takes two arguments: the values of the two elements bein ```js const uniqueElementsByRight = (arr, fn) => - arr.reduce((acc, v) => { + arr.reduceRight((acc, v) => { if (!acc.some(x => fn(v, x))) acc.push(v); return acc; }, []); diff --git a/tag_database b/tag_database index 0f5c9037d..bb2dfb23c 100644 --- a/tag_database +++ b/tag_database @@ -73,6 +73,7 @@ extendHex:utility,string factorial:math,recursion fibonacci:math,array filterNonUnique:array +filterNonUniqueBy:array,function findKey:object,function findLast:array findLastIndex:array,function @@ -298,6 +299,8 @@ union:array,math unionBy:array,function unionWith:array,function uniqueElements:array +uniqueElementsBy:array,function +uniqueElementsByRight:array,function untildify:node,string unzip:array unzipWith:array,function,advanced diff --git a/test/filterNonUniqueBy/filterNonUniqueBy.js b/test/filterNonUniqueBy/filterNonUniqueBy.js new file mode 100644 index 000000000..00bf31797 --- /dev/null +++ b/test/filterNonUniqueBy/filterNonUniqueBy.js @@ -0,0 +1,3 @@ +const filterNonUniqueBy = (arr, fn) => +arr.filter((v, i) => arr.every((x, j) => i == j == fn(v, x, i, j))); +module.exports = filterNonUniqueBy; \ No newline at end of file diff --git a/test/filterNonUniqueBy/filterNonUniqueBy.test.js b/test/filterNonUniqueBy/filterNonUniqueBy.test.js new file mode 100644 index 000000000..b73fb8f26 --- /dev/null +++ b/test/filterNonUniqueBy/filterNonUniqueBy.test.js @@ -0,0 +1,32 @@ +const expect = require('expect'); +const filterNonUniqueBy = require('./filterNonUniqueBy.js'); + +test('filterNonUniqueBy is a Function', () => { + expect(filterNonUniqueBy).toBeInstanceOf(Function); +}); + +test('filterNonUniqueBy works for properties', () => { + expect(filterNonUniqueBy( + [ + { id: 0, value: 'a' }, + { id: 1, value: 'b' }, + { id: 2, value: 'c' }, + { id: 1, value: 'd' }, + { id: 0, value: 'e' }, + ], + (a, b) => a.id == b.id + )).toEqual([ { id: 2, value: 'c' } ]); +}); + +test('filterNonUniqueBy works for nested properties', () => { + expect(filterNonUniqueBy( + [ + { id: 0, value: 'a', n: {p: 0} }, + { id: 1, value: 'b', n: {p: 1} }, + { id: 2, value: 'c', n: {p: 2} }, + { id: 1, value: 'd', n: {p: 0} }, + { id: 0, value: 'e', n: {p: 1} }, + ], + (a, b) => a.id == b.id + )).toEqual([ { id: 2, value: 'c', n: {p: 2} } ]); +}); diff --git a/test/uniqueElementsBy/uniqueElementsBy.js b/test/uniqueElementsBy/uniqueElementsBy.js new file mode 100644 index 000000000..60634739e --- /dev/null +++ b/test/uniqueElementsBy/uniqueElementsBy.js @@ -0,0 +1,6 @@ +const uniqueElementsBy = (arr, fn) => +arr.reduce((acc, v) => { +if (!acc.some(x => fn(v, x))) acc.push(v); +return acc; +}, []); +module.exports = uniqueElementsBy; \ No newline at end of file diff --git a/test/uniqueElementsBy/uniqueElementsBy.test.js b/test/uniqueElementsBy/uniqueElementsBy.test.js new file mode 100644 index 000000000..aa5293336 --- /dev/null +++ b/test/uniqueElementsBy/uniqueElementsBy.test.js @@ -0,0 +1,32 @@ +const expect = require('expect'); +const uniqueElementsBy = require('./uniqueElementsBy.js'); + +test('uniqueElementsBy is a Function', () => { + expect(uniqueElementsBy).toBeInstanceOf(Function); +}); + +test('uniqueElementsBy works for properties', () => { + expect(uniqueElementsBy( + [ + { id: 0, value: 'a' }, + { id: 1, value: 'b' }, + { id: 2, value: 'c' }, + { id: 1, value: 'd' }, + { id: 0, value: 'e' }, + ], + (a, b) => a.id == b.id + )).toEqual([ { id: 0, value: 'a' }, { id: 1, value: 'b' }, { id: 2, value: 'c' } ]); +}); + +test('uniqueElementsBy works for nested properties', () => { + expect(uniqueElementsBy( + [ + { id: 0, value: 'a', n: {p: 0} }, + { id: 1, value: 'b', n: {p: 1} }, + { id: 2, value: 'c', n: {p: 2} }, + { id: 1, value: 'd', n: {p: 0} }, + { id: 0, value: 'e', n: {p: 1} }, + ], + (a, b) => a.id == b.id + )).toEqual([ { id: 0, value: 'a', n: {p: 0} }, { id: 1, value: 'b', n: {p: 1} }, { id: 2, value: 'c', n: {p: 2} } ]); +}); diff --git a/test/uniqueElementsByRight/uniqueElementsByRight.js b/test/uniqueElementsByRight/uniqueElementsByRight.js new file mode 100644 index 000000000..3b6975a4b --- /dev/null +++ b/test/uniqueElementsByRight/uniqueElementsByRight.js @@ -0,0 +1,6 @@ +const uniqueElementsByRight = (arr, fn) => +arr.reduceRight((acc, v) => { +if (!acc.some(x => fn(v, x))) acc.push(v); +return acc; +}, []); +module.exports = uniqueElementsByRight; \ No newline at end of file diff --git a/test/uniqueElementsByRight/uniqueElementsByRight.test.js b/test/uniqueElementsByRight/uniqueElementsByRight.test.js new file mode 100644 index 000000000..aeed58d36 --- /dev/null +++ b/test/uniqueElementsByRight/uniqueElementsByRight.test.js @@ -0,0 +1,32 @@ +const expect = require('expect'); +const uniqueElementsByRight = require('./uniqueElementsByRight.js'); + +test('uniqueElementsByRight is a Function', () => { + expect(uniqueElementsByRight).toBeInstanceOf(Function); +}); + +test('uniqueElementsByRight works for properties', () => { + expect(uniqueElementsByRight( + [ + { id: 0, value: 'a' }, + { id: 1, value: 'b' }, + { id: 2, value: 'c' }, + { id: 1, value: 'd' }, + { id: 0, value: 'e' }, + ], + (a, b) => a.id == b.id + )).toEqual([ { id: 0, value: 'e' }, { id: 1, value: 'd' }, { id: 2, value: 'c' } ]); +}); + +test('uniqueElementsByRight works for nested properties', () => { + expect(uniqueElementsByRight( + [ + { id: 0, value: 'a', n: {p: 0} }, + { id: 1, value: 'b', n: {p: 1} }, + { id: 2, value: 'c', n: {p: 2} }, + { id: 1, value: 'd', n: {p: 0} }, + { id: 0, value: 'e', n: {p: 1} }, + ], + (a, b) => a.id == b.id + )).toEqual([ { id: 0, value: 'e', n: {p: 1} }, { id: 1, value: 'd', n: {p: 0} }, { id: 2, value: 'c', n: {p: 2} } ]); +});