From 291f36cd736b5dea058145a1335bf7c78ec1d81d Mon Sep 17 00:00:00 2001 From: Synbulat Biishev Date: Sun, 20 Nov 2022 00:34:26 +0300 Subject: [PATCH] feat: add `Collection#subtract()` (#8393) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add `Collection#missing()` * test: add test for `Collection#missing()` * chore: rename `missing` to `complement` * docs: fix name * test: fix test name Co-authored-by: Almeida * chore: sort alphabetically * fix: edit condition Co-authored-by: Almeida * refactor: rename to `subtract` * docs: fix description Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> * fix: change condition * fix: resolved eslint formatting error Co-authored-by: Almeida Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> Co-authored-by: Aura Román --- .../collection/__tests__/collection.test.ts | 21 +++++++++++++++---- packages/collection/src/collection.ts | 16 ++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/packages/collection/__tests__/collection.test.ts b/packages/collection/__tests__/collection.test.ts index b2d01213f096..73d3adaf1530 100644 --- a/packages/collection/__tests__/collection.test.ts +++ b/packages/collection/__tests__/collection.test.ts @@ -3,17 +3,17 @@ import { describe, test, expect } from 'vitest'; import { Collection } from '../src/index.js'; -type TestCollection = Collection; +type TestCollection = Collection; -function createCollection(): TestCollection { +function createCollection(): TestCollection { return new Collection(); } -function createCollectionFrom(...entries: [key: string, value: number][]): TestCollection { +function createCollectionFrom(...entries: [key: string, value: V][]): TestCollection { return new Collection(entries); } -function createTestCollection(): TestCollection { +function createTestCollection(): TestCollection { return createCollectionFrom(['a', 1], ['b', 2], ['c', 3]); } @@ -770,6 +770,19 @@ describe('sort() tests', () => { }); }); +describe('subtract() tests', () => { + const coll1 = createCollectionFrom(['a', 1], ['b', 2], ['c', 3], ['d', undefined]); + const coll2 = createCollectionFrom(['b', 2], ['c', 0]); + + test('it returns a new collection', () => { + const c = coll1.subtract(coll2); + expect(c).toBeInstanceOf(Collection); + expect(c.size).toStrictEqual(3); + + expect(c).toStrictEqual(createCollectionFrom(['a', 1], ['c', 3], ['d', undefined])); + }); +}); + describe('sweep() test', () => { const coll = createTestCollection(); diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index c8a14cdea52f..425fcd0fab2c 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -684,6 +684,22 @@ export class Collection extends Map { return coll; } + /** + * The subtract method returns a new structure containing items where the keys and values of the original structure are not present in the other. + * + * @param other - The other Collection to filter against + */ + public subtract(other: ReadonlyCollection): Collection { + const coll = new this.constructor[Symbol.species](); + for (const [k, v] of this) { + if (!other.has(k) || !Object.is(v, other.get(k))) { + coll.set(k, v); + } + } + + return coll; + } + /** * The difference method returns a new structure containing items where the key is present in one of the original structures but not the other. *