mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
types: add ReadonlyCollection (#7245)
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
@@ -9,6 +9,12 @@ export interface CollectionConstructor {
|
||||
readonly [Symbol.species]: CollectionConstructor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents an immutable version of a collection
|
||||
*/
|
||||
export type ReadonlyCollection<K, V> = ReadonlyMap<K, V> &
|
||||
Omit<Collection<K, V>, 'forEach' | 'ensure' | 'reverse' | 'sweep' | 'sort' | 'get' | 'set' | 'delete'>;
|
||||
|
||||
/**
|
||||
* Separate interface for the constructor so that emitted js does not have a constructor that overwrites itself
|
||||
*
|
||||
@@ -562,7 +568,7 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
* @example
|
||||
* const newColl = someColl.clone();
|
||||
*/
|
||||
public clone() {
|
||||
public clone(): Collection<K, V> {
|
||||
return new this.constructor[Symbol.species](this);
|
||||
}
|
||||
|
||||
@@ -574,7 +580,7 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
* @example
|
||||
* const newColl = someColl.concat(someOtherColl, anotherColl, ohBoyAColl);
|
||||
*/
|
||||
public concat(...collections: Collection<K, V>[]) {
|
||||
public concat(...collections: ReadonlyCollection<K, V>[]) {
|
||||
const newColl = this.clone();
|
||||
for (const coll of collections) {
|
||||
for (const [key, val] of coll) newColl.set(key, val);
|
||||
@@ -591,7 +597,7 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
*
|
||||
* @returns Whether the collections have identical contents
|
||||
*/
|
||||
public equals(collection: Collection<K, V>) {
|
||||
public equals(collection: ReadonlyCollection<K, V>) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
||||
if (!collection) return false; // runtime check
|
||||
if (this === collection) return true;
|
||||
@@ -634,7 +640,7 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
*
|
||||
* @param other The other Collection to filter against
|
||||
*/
|
||||
public intersect<T>(other: Collection<K, T>): Collection<K, T> {
|
||||
public intersect<T>(other: ReadonlyCollection<K, T>): Collection<K, T> {
|
||||
const coll = new this.constructor[Symbol.species]<K, T>();
|
||||
for (const [k, v] of other) {
|
||||
if (this.has(k) && Object.is(v, this.get(k))) {
|
||||
@@ -649,7 +655,7 @@ export class Collection<K, V> extends Map<K, V> {
|
||||
*
|
||||
* @param other The other Collection to filter against
|
||||
*/
|
||||
public difference<T>(other: Collection<K, T>): Collection<K, V | T> {
|
||||
public difference<T>(other: ReadonlyCollection<K, T>): Collection<K, V | T> {
|
||||
const coll = new this.constructor[Symbol.species]<K, V | T>();
|
||||
for (const [k, v] of other) {
|
||||
if (!this.has(k)) coll.set(k, v);
|
||||
|
||||
Reference in New Issue
Block a user