Tags and tests

This commit is contained in:
Angelos Chalaris
2018-07-18 21:02:42 +03:00
parent 03ee6f73ca
commit b129b46c0d
8 changed files with 115 additions and 1 deletions

View File

@ -7,7 +7,7 @@ The comparator function takes two arguments: the values of the two elements bein
```js ```js
const uniqueElementsByRight = (arr, fn) => const uniqueElementsByRight = (arr, fn) =>
arr.reduce((acc, v) => { arr.reduceRight((acc, v) => {
if (!acc.some(x => fn(v, x))) acc.push(v); if (!acc.some(x => fn(v, x))) acc.push(v);
return acc; return acc;
}, []); }, []);

View File

@ -73,6 +73,7 @@ extendHex:utility,string
factorial:math,recursion factorial:math,recursion
fibonacci:math,array fibonacci:math,array
filterNonUnique:array filterNonUnique:array
filterNonUniqueBy:array,function
findKey:object,function findKey:object,function
findLast:array findLast:array
findLastIndex:array,function findLastIndex:array,function
@ -298,6 +299,8 @@ union:array,math
unionBy:array,function unionBy:array,function
unionWith:array,function unionWith:array,function
uniqueElements:array uniqueElements:array
uniqueElementsBy:array,function
uniqueElementsByRight:array,function
untildify:node,string untildify:node,string
unzip:array unzip:array
unzipWith:array,function,advanced unzipWith:array,function,advanced

View File

@ -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;

View File

@ -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} } ]);
});

View File

@ -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;

View File

@ -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} } ]);
});

View File

@ -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;

View File

@ -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} } ]);
});