feat(Collection): add merging functions (#7299)

Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
1Computer1
2022-01-26 15:46:31 -05:00
committed by GitHub
parent e1ecc1a80a
commit e4bd07b239
2 changed files with 172 additions and 0 deletions

View File

@@ -460,3 +460,95 @@ describe('ensure() tests', () => {
expect(coll.size).toStrictEqual(2);
});
});
describe('merge() tests', () => {
const cL = new Collection([
['L', 1],
['LR', 2],
]);
const cR = new Collection([
['R', 3],
['LR', 4],
]);
test('merges two collection, with all keys together', () => {
const c = cL.merge(
cR,
(x) => ({ keep: true, value: `L${x}` }),
(y) => ({ keep: true, value: `R${y}` }),
(x, y) => ({ keep: true, value: `LR${x},${y}` }),
);
expect(c.get('L')).toStrictEqual('L1');
expect(c.get('R')).toStrictEqual('R3');
expect(c.get('LR')).toStrictEqual('LR2,4');
expect(c.size).toStrictEqual(3);
});
test('merges two collection, removing left entries', () => {
const c = cL.merge(
cR,
() => ({ keep: false }),
(y) => ({ keep: true, value: `R${y}` }),
(x, y) => ({ keep: true, value: `LR${x},${y}` }),
);
expect(c.get('R')).toStrictEqual('R3');
expect(c.get('LR')).toStrictEqual('LR2,4');
expect(c.size).toStrictEqual(2);
});
test('merges two collection, removing right entries', () => {
const c = cL.merge(
cR,
(x) => ({ keep: true, value: `L${x}` }),
() => ({ keep: false }),
(x, y) => ({ keep: true, value: `LR${x},${y}` }),
);
expect(c.get('L')).toStrictEqual('L1');
expect(c.get('LR')).toStrictEqual('LR2,4');
expect(c.size).toStrictEqual(2);
});
test('merges two collection, removing in-both entries', () => {
const c = cL.merge(
cR,
(x) => ({ keep: true, value: `L${x}` }),
(y) => ({ keep: true, value: `R${y}` }),
() => ({ keep: false }),
);
expect(c.get('L')).toStrictEqual('L1');
expect(c.get('R')).toStrictEqual('R3');
expect(c.size).toStrictEqual(2);
});
});
describe('combineEntries() tests', () => {
test('it adds entries together', () => {
const c = Collection.combineEntries(
[
['a', 1],
['b', 2],
['a', 2],
],
(x, y) => x + y,
);
expect([...c]).toStrictEqual([
['a', 3],
['b', 2],
]);
});
test('it really goes through all the entries', () => {
const c = Collection.combineEntries(
[
['a', [1]],
['b', [2]],
['a', [2]],
],
(x, y) => x.concat(y),
);
expect([...c]).toStrictEqual([
['a', [1, 2]],
['b', [2]],
]);
});
});