Skip to content

Commit

Permalink
Merge branch 'main' into audit-log-types
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx authored Jan 26, 2025
2 parents 0e7d933 + b7e0fe3 commit d259e4b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 12 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,12 @@ class GuildScheduledEventManager extends CachedManager {
* Options for setting a recurrence rule for a guild scheduled event.
* @typedef {Object} GuildScheduledEventRecurrenceRuleOptions
* @property {DateResolvable} startAt The time the recurrence rule interval starts at
* @property {?DateResolvable} endAt The time the recurrence rule interval ends at
* @property {GuildScheduledEventRecurrenceRuleFrequency} frequency How often the event occurs
* @property {number} interval The spacing between the events
* @property {?GuildScheduledEventRecurrenceRuleWeekday[]} byWeekday The days within a week to recur on
* @property {?GuildScheduledEventRecurrenceRuleNWeekday[]} byNWeekday The days within a week to recur on
* @property {?GuildScheduledEventRecurrenceRuleMonth[]} byMonth The months to recur on
* @property {?number[]} byMonthDay The days within a month to recur on
* @property {?number[]} byYearDay The days within a year to recur on
* @property {?number} count The total amount of times the event is allowed to recur before stopping
*/

/**
Expand Down
4 changes: 0 additions & 4 deletions packages/discord.js/src/util/Transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,12 @@ function _transformAPIMessageInteractionMetadata(client, messageInteractionMetad
function _transformGuildScheduledEventRecurrenceRule(recurrenceRule) {
return {
start: new Date(recurrenceRule.startAt).toISOString(),
// eslint-disable-next-line eqeqeq
end: recurrenceRule.endAt != null ? new Date(recurrenceRule.endAt).toISOString() : recurrenceRule.endAt,
frequency: recurrenceRule.frequency,
interval: recurrenceRule.interval,
by_weekday: recurrenceRule.byWeekday,
by_n_weekday: recurrenceRule.byNWeekday,
by_month: recurrenceRule.byMonth,
by_month_day: recurrenceRule.byMonthDay,
by_year_day: recurrenceRule.byYearDay,
count: recurrenceRule.count,
};
}

Expand Down
2 changes: 0 additions & 2 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5983,8 +5983,6 @@ type BaseGuildScheduledEventRecurrenceRuleOptions<
Extra extends {},
> = {
startAt: DateResolvable;
endAt?: DateResolvable;
count?: number;
interval: number;
frequency: Frequency;
} & Extra;
Expand Down

0 comments on commit d259e4b

Please sign in to comment.