diff --git a/packages/collection/__tests__/collection.test.ts b/packages/collection/__tests__/collection.test.ts index 73d3adaf1..a052a391f 100644 --- a/packages/collection/__tests__/collection.test.ts +++ b/packages/collection/__tests__/collection.test.ts @@ -19,6 +19,7 @@ function createTestCollection(): TestCollection { function expectInvalidFunctionError(cb: () => unknown, val?: unknown): void { expect(() => { + // eslint-disable-next-line promise/prefer-await-to-callbacks cb(); }).toThrowError(new TypeError(`${val} is not a function`)); } diff --git a/packages/collection/src/collection.ts b/packages/collection/src/collection.ts index 8b29f9032..5a8a8ead7 100644 --- a/packages/collection/src/collection.ts +++ b/packages/collection/src/collection.ts @@ -1,4 +1,3 @@ -/* eslint-disable id-length */ /* eslint-disable no-param-reassign */ /** * @internal @@ -62,7 +61,7 @@ export class Collection extends Map { * @returns `true` if all of the elements exist, `false` if at least one does not exist. */ public hasAll(...keys: K[]) { - return keys.every((k) => super.has(k)); + return keys.every((key) => super.has(key)); } /** @@ -72,7 +71,7 @@ export class Collection extends Map { * @returns `true` if any of the elements exist, `false` if none exist. */ public hasAny(...keys: K[]) { - return keys.some((k) => super.has(k)); + return keys.some((key) => super.has(key)); } /** @@ -563,8 +562,12 @@ export class Collection extends Map { public each(fn: (this: T, value: V, key: K, collection: this) => void, thisArg: T): this; public each(fn: (value: V, key: K, collection: this) => void, thisArg?: unknown): this { if (typeof fn !== 'function') throw new TypeError(`${fn} is not a function`); - // eslint-disable-next-line unicorn/no-array-method-this-argument - this.forEach(fn as (value: V, key: K, map: Map) => void, thisArg); + if (thisArg !== undefined) fn = fn.bind(thisArg); + + for (const [key, value] of this) { + fn(value, key, this); + } + return this; } @@ -661,8 +664,8 @@ export class Collection extends Map { super.clear(); // Set the new entries - for (const [k, v] of entries) { - super.set(k, v); + for (const [key, value] of entries) { + super.set(key, value); } return this; @@ -675,9 +678,9 @@ export class Collection extends Map { */ public intersect(other: ReadonlyCollection): Collection { const coll = new this.constructor[Symbol.species](); - for (const [k, v] of other) { - if (this.has(k) && Object.is(v, this.get(k))) { - coll.set(k, v); + for (const [key, value] of other) { + if (this.has(key) && Object.is(value, this.get(key))) { + coll.set(key, value); } } @@ -691,9 +694,9 @@ export class Collection extends Map { */ 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); + for (const [key, value] of this) { + if (!other.has(key) || !Object.is(value, other.get(key))) { + coll.set(key, value); } } @@ -707,12 +710,12 @@ export class Collection extends Map { */ public difference(other: ReadonlyCollection): Collection { const coll = new this.constructor[Symbol.species](); - for (const [k, v] of other) { - if (!this.has(k)) coll.set(k, v); + for (const [key, value] of other) { + if (!this.has(key)) coll.set(key, value); } - for (const [k, v] of this) { - if (!other.has(k)) coll.set(k, v); + for (const [key, value] of this) { + if (!other.has(key)) coll.set(key, value); } return coll; @@ -754,19 +757,20 @@ export class Collection extends Map { ): Collection { const coll = new this.constructor[Symbol.species](); const keys = new Set([...this.keys(), ...other.keys()]); - for (const k of keys) { - const hasInSelf = this.has(k); - const hasInOther = other.has(k); + + for (const key of keys) { + const hasInSelf = this.has(key); + const hasInOther = other.has(key); if (hasInSelf && hasInOther) { - const r = whenInBoth(this.get(k)!, other.get(k)!, k); - if (r.keep) coll.set(k, r.value); + const result = whenInBoth(this.get(key)!, other.get(key)!, key); + if (result.keep) coll.set(key, result.value); } else if (hasInSelf) { - const r = whenInSelf(this.get(k)!, k); - if (r.keep) coll.set(k, r.value); + const result = whenInSelf(this.get(key)!, key); + if (result.keep) coll.set(key, result.value); } else if (hasInOther) { - const r = whenInOther(other.get(k)!, k); - if (r.keep) coll.set(k, r.value); + const result = whenInOther(other.get(key)!, key); + if (result.keep) coll.set(key, result.value); } } @@ -815,11 +819,11 @@ export class Collection extends Map { combine: (firstValue: V, secondValue: V, key: K) => V, ): Collection { const coll = new Collection(); - for (const [k, v] of entries) { - if (coll.has(k)) { - coll.set(k, combine(coll.get(k)!, v, k)); + for (const [key, value] of entries) { + if (coll.has(key)) { + coll.set(key, combine(coll.get(key)!, value, key)); } else { - coll.set(k, v); + coll.set(key, value); } }