Tags and tests
This commit is contained in:
@ -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;
|
||||||
}, []);
|
}, []);
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
3
test/filterNonUniqueBy/filterNonUniqueBy.js
Normal file
3
test/filterNonUniqueBy/filterNonUniqueBy.js
Normal 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;
|
||||||
32
test/filterNonUniqueBy/filterNonUniqueBy.test.js
Normal file
32
test/filterNonUniqueBy/filterNonUniqueBy.test.js
Normal 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} } ]);
|
||||||
|
});
|
||||||
6
test/uniqueElementsBy/uniqueElementsBy.js
Normal file
6
test/uniqueElementsBy/uniqueElementsBy.js
Normal 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;
|
||||||
32
test/uniqueElementsBy/uniqueElementsBy.test.js
Normal file
32
test/uniqueElementsBy/uniqueElementsBy.test.js
Normal 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} } ]);
|
||||||
|
});
|
||||||
6
test/uniqueElementsByRight/uniqueElementsByRight.js
Normal file
6
test/uniqueElementsByRight/uniqueElementsByRight.js
Normal 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;
|
||||||
32
test/uniqueElementsByRight/uniqueElementsByRight.test.js
Normal file
32
test/uniqueElementsByRight/uniqueElementsByRight.test.js
Normal 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} } ]);
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user