Skip to content

feat(collection): honour subclassing via @@species in static methods #10723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions packages/collection/__tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,58 @@ describe('findLastKey() tests', () => {
}, null);
});
});

describe('subclassing tests', () => {
class DerivedCollection<Key, Value> extends Collection<Key, Value> {}

test('constructor[Symbol.species]', () => {
expect(DerivedCollection[Symbol.species]).toStrictEqual(DerivedCollection);
});

describe('methods that construct new collections return subclassed objects', () => {
const coll = new DerivedCollection();

test('filter()', () => {
expect(coll.filter(Boolean)).toBeInstanceOf(DerivedCollection);
});
test('partition()', () => {
for (const partition of coll.partition(Boolean)) {
expect(partition).toBeInstanceOf(DerivedCollection);
}
});
test('flatMap()', () => {
expect(coll.flatMap(() => new Collection())).toBeInstanceOf(DerivedCollection);
});
test('mapValues()', () => {
expect(coll.mapValues(Object)).toBeInstanceOf(DerivedCollection);
});
test('clone()', () => {
expect(coll.clone()).toBeInstanceOf(DerivedCollection);
});
test('intersection()', () => {
expect(coll.intersection(new Collection())).toBeInstanceOf(DerivedCollection);
});
test('union()', () => {
expect(coll.union(new Collection())).toBeInstanceOf(DerivedCollection);
});
test('difference()', () => {
expect(coll.difference(new Collection())).toBeInstanceOf(DerivedCollection);
});
test('symmetricDifference()', () => {
expect(coll.symmetricDifference(new Collection())).toBeInstanceOf(DerivedCollection);
});
test('merge()', () => {
const fn = () => ({ keep: false }) as const; // eslint-disable-line unicorn/consistent-function-scoping
expect(coll.merge(new Collection(), fn, fn, fn)).toBeInstanceOf(DerivedCollection);
});
test('toReversed()', () => {
expect(coll.toReversed()).toBeInstanceOf(DerivedCollection);
});
test('toSorted()', () => {
expect(coll.toSorted()).toBeInstanceOf(DerivedCollection);
});
test('Collection.combineEntries()', () => {
expect(DerivedCollection.combineEntries([], Object)).toBeInstanceOf(DerivedCollection);
});
});
});
11 changes: 8 additions & 3 deletions packages/collection/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export type ReadonlyCollection<Key, Value> = Omit<

export interface Collection<Key, Value> {
/**
* Ambient declaration to allow `this.constructor[@@species]` in class methods.
* Ambient declaration to allow references to `this.constructor` in class methods.
*
* @internal
*/
constructor: typeof Collection & { readonly [Symbol.species]: typeof Collection };
constructor: typeof Collection;
}

/**
Expand Down Expand Up @@ -1076,7 +1076,7 @@ export class Collection<Key, Value> extends Map<Key, Value> {
entries: Iterable<[Key, Value]>,
combine: (firstValue: Value, secondValue: Value, key: Key) => Value,
): Collection<Key, Value> {
const coll = new Collection<Key, Value>();
const coll = new this[Symbol.species]<Key, Value>();
for (const [key, value] of entries) {
if (coll.has(key)) {
coll.set(key, combine(coll.get(key)!, value, key));
Expand All @@ -1087,6 +1087,11 @@ export class Collection<Key, Value> extends Map<Key, Value> {

return coll;
}

/**
* @internal
*/
declare public static readonly [Symbol.species]: typeof Collection;
}

/**
Expand Down
Loading