Skip to content
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

refactor(collection): fix/silence linter warnings #9266

Merged
merged 3 commits into from
Mar 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions packages/collection/__tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function createTestCollection(): TestCollection<number> {

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`));
}
Expand Down
64 changes: 34 additions & 30 deletions packages/collection/src/collection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable id-length */
/* eslint-disable no-param-reassign */
/**
* @internal
Expand Down Expand Up @@ -62,7 +61,7 @@ export class Collection<K, V> extends Map<K, V> {
* @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));
}

/**
Expand All @@ -72,7 +71,7 @@ export class Collection<K, V> extends Map<K, V> {
* @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));
}

/**
Expand Down Expand Up @@ -563,8 +562,12 @@ export class Collection<K, V> extends Map<K, V> {
public each<T>(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<K, V>) => void, thisArg);
if (thisArg !== undefined) fn = fn.bind(thisArg);

for (const [key, value] of this) {
fn(value, key, this);
}

return this;
}

Expand Down Expand Up @@ -661,8 +664,8 @@ export class Collection<K, V> extends Map<K, V> {
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;
Expand All @@ -675,9 +678,9 @@ export class Collection<K, V> extends Map<K, V> {
*/
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))) {
coll.set(k, v);
for (const [key, value] of other) {
if (this.has(key) && Object.is(value, this.get(key))) {
coll.set(key, value);
}
}

Expand All @@ -691,9 +694,9 @@ export class Collection<K, V> extends Map<K, V> {
*/
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);
for (const [key, value] of this) {
if (!other.has(key) || !Object.is(value, other.get(key))) {
coll.set(key, value);
}
}

Expand All @@ -707,12 +710,12 @@ export class Collection<K, V> extends Map<K, V> {
*/
public difference<T>(other: ReadonlyCollection<K, T>): Collection<K, T | V> {
const coll = new this.constructor[Symbol.species]<K, T | V>();
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;
Expand Down Expand Up @@ -754,19 +757,20 @@ export class Collection<K, V> extends Map<K, V> {
): Collection<K, R> {
const coll = new this.constructor[Symbol.species]<K, R>();
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);
}
}

Expand Down Expand Up @@ -815,11 +819,11 @@ export class Collection<K, V> extends Map<K, V> {
combine: (firstValue: V, secondValue: V, key: K) => V,
): Collection<K, V> {
const coll = new Collection<K, V>();
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);
}
}

Expand Down