Skip to content

Commit

Permalink
feat: add Collection#subtract() (#8393)
Browse files Browse the repository at this point in the history
* 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 <almeidx@pm.me>

* chore: sort alphabetically

* fix: edit condition

Co-authored-by: Almeida <almeidx@pm.me>

* 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 <almeidx@pm.me>
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 <kyradiscord@gmail.com>
  • Loading branch information
5 people committed Nov 19, 2022
1 parent b2fabd1 commit 291f36c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/collection/__tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
import { describe, test, expect } from 'vitest';
import { Collection } from '../src/index.js';

type TestCollection = Collection<string, number>;
type TestCollection<V> = Collection<string, V>;

function createCollection(): TestCollection {
function createCollection<V = number>(): TestCollection<V> {
return new Collection();
}

function createCollectionFrom(...entries: [key: string, value: number][]): TestCollection {
function createCollectionFrom<V = number>(...entries: [key: string, value: V][]): TestCollection<V> {
return new Collection(entries);
}

function createTestCollection(): TestCollection {
function createTestCollection(): TestCollection<number> {
return createCollectionFrom(['a', 1], ['b', 2], ['c', 3]);
}

Expand Down Expand Up @@ -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();

Expand Down
16 changes: 16 additions & 0 deletions packages/collection/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,22 @@ export class Collection<K, V> extends Map<K, V> {
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<T>(other: ReadonlyCollection<K, T>): Collection<K, V> {
const coll = new this.constructor[Symbol.species]<K, V>();
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.
*
Expand Down

0 comments on commit 291f36c

Please sign in to comment.