Tags and tests
This commit is contained in:
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