mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-09-12 06:06:28 -05:00
Migrate Prettier/Eslint/Stylelint setup to Biome (#1772)
* Initial * CSS lint * Test CI * Add 1v1, 2v2, and 3v3 Tags (#1771) * Initial * CSS lint * Test CI * Rename step --------- Co-authored-by: xi <104683822+ximk@users.noreply.github.com>
This commit is contained in:
@@ -1,263 +1,263 @@
|
||||
import clone from "just-clone";
|
||||
import type {
|
||||
CrudInterface,
|
||||
OmitId,
|
||||
Table,
|
||||
Database,
|
||||
CrudInterface,
|
||||
Database,
|
||||
OmitId,
|
||||
Table,
|
||||
} from "~/modules/brackets-manager";
|
||||
|
||||
export class InMemoryDatabase implements CrudInterface {
|
||||
protected data: Database = {
|
||||
stage: [],
|
||||
group: [],
|
||||
round: [],
|
||||
match: [],
|
||||
};
|
||||
protected data: Database = {
|
||||
stage: [],
|
||||
group: [],
|
||||
round: [],
|
||||
match: [],
|
||||
};
|
||||
|
||||
/**
|
||||
* @param data "import" data from external
|
||||
*/
|
||||
setData(data: Database): void {
|
||||
this.data = data;
|
||||
}
|
||||
/**
|
||||
* @param data "import" data from external
|
||||
*/
|
||||
setData(data: Database): void {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param partial Filter
|
||||
*/
|
||||
makeFilter(partial: any): (entry: any) => boolean {
|
||||
return (entry: any): boolean => {
|
||||
let result = true;
|
||||
for (const key of Object.keys(partial))
|
||||
result = result && entry[key] === partial[key];
|
||||
/**
|
||||
* @param partial Filter
|
||||
*/
|
||||
makeFilter(partial: any): (entry: any) => boolean {
|
||||
return (entry: any): boolean => {
|
||||
let result = true;
|
||||
for (const key of Object.keys(partial))
|
||||
result = result && entry[key] === partial[key];
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
return result;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Clearing all of the data
|
||||
*/
|
||||
reset(): void {
|
||||
this.data = {
|
||||
stage: [],
|
||||
group: [],
|
||||
round: [],
|
||||
match: [],
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Clearing all of the data
|
||||
*/
|
||||
reset(): void {
|
||||
this.data = {
|
||||
stage: [],
|
||||
group: [],
|
||||
round: [],
|
||||
match: [],
|
||||
};
|
||||
}
|
||||
|
||||
insert<T>(table: Table, value: OmitId<T>): number;
|
||||
/**
|
||||
* Inserts multiple values in the database.
|
||||
*
|
||||
* @param table Where to insert.
|
||||
* @param values What to insert.
|
||||
*/
|
||||
insert<T>(table: Table, values: OmitId<T>[]): boolean;
|
||||
insert<T>(table: Table, value: OmitId<T>): number;
|
||||
/**
|
||||
* Inserts multiple values in the database.
|
||||
*
|
||||
* @param table Where to insert.
|
||||
* @param values What to insert.
|
||||
*/
|
||||
insert<T>(table: Table, values: OmitId<T>[]): boolean;
|
||||
|
||||
/**
|
||||
* Implementation of insert
|
||||
*
|
||||
* @param table Where to insert.
|
||||
* @param values What to insert.
|
||||
*/
|
||||
insert<T>(table: Table, values: OmitId<T> | OmitId<T>[]): number | boolean {
|
||||
let id =
|
||||
this.data[table].length > 0
|
||||
? Math.max(...this.data[table].map((d) => d.id)) + 1
|
||||
: 0;
|
||||
/**
|
||||
* Implementation of insert
|
||||
*
|
||||
* @param table Where to insert.
|
||||
* @param values What to insert.
|
||||
*/
|
||||
insert<T>(table: Table, values: OmitId<T> | OmitId<T>[]): number | boolean {
|
||||
let id =
|
||||
this.data[table].length > 0
|
||||
? Math.max(...this.data[table].map((d) => d.id)) + 1
|
||||
: 0;
|
||||
|
||||
if (!Array.isArray(values)) {
|
||||
try {
|
||||
// @ts-expect-error idc
|
||||
if (values.id) {
|
||||
// @ts-expect-error idc
|
||||
this.data[table][values.id] = values;
|
||||
} else {
|
||||
// @ts-expect-error imported
|
||||
this.data[table].push({ id, ...values });
|
||||
}
|
||||
} catch (error) {
|
||||
return -1;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
if (!Array.isArray(values)) {
|
||||
try {
|
||||
// @ts-expect-error idc
|
||||
if (values.id) {
|
||||
// @ts-expect-error idc
|
||||
this.data[table][values.id] = values;
|
||||
} else {
|
||||
// @ts-expect-error imported
|
||||
this.data[table].push({ id, ...values });
|
||||
}
|
||||
} catch (error) {
|
||||
return -1;
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
try {
|
||||
values.forEach((object) => {
|
||||
// @ts-expect-error idc
|
||||
if (object.id) {
|
||||
// @ts-expect-error idc
|
||||
this.data[table][object.id] = object;
|
||||
} else {
|
||||
// @ts-expect-error imported
|
||||
this.data[table].push({ id: id++, ...object });
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
for (const object of values) {
|
||||
// @ts-expect-error idc
|
||||
if (object.id) {
|
||||
// @ts-expect-error idc
|
||||
this.data[table][object.id] = object;
|
||||
} else {
|
||||
// @ts-expect-error imported
|
||||
this.data[table].push({ id: id++, ...object });
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all data from a table in the database.
|
||||
*
|
||||
* @param table Where to get from.
|
||||
*/
|
||||
select<T>(table: Table): T[] | null;
|
||||
/**
|
||||
* Gets specific data from a table in the database.
|
||||
*
|
||||
* @param table Where to get from.
|
||||
* @param id What to get.
|
||||
*/
|
||||
select<T>(table: Table, id: number): T | null;
|
||||
/**
|
||||
* Gets data from a table in the database with a filter.
|
||||
*
|
||||
* @param table Where to get from.
|
||||
* @param filter An object to filter data.
|
||||
*/
|
||||
select<T>(table: Table, filter: Partial<T>): T[] | null;
|
||||
/**
|
||||
* Gets all data from a table in the database.
|
||||
*
|
||||
* @param table Where to get from.
|
||||
*/
|
||||
select<T>(table: Table): T[] | null;
|
||||
/**
|
||||
* Gets specific data from a table in the database.
|
||||
*
|
||||
* @param table Where to get from.
|
||||
* @param id What to get.
|
||||
*/
|
||||
select<T>(table: Table, id: number): T | null;
|
||||
/**
|
||||
* Gets data from a table in the database with a filter.
|
||||
*
|
||||
* @param table Where to get from.
|
||||
* @param filter An object to filter data.
|
||||
*/
|
||||
select<T>(table: Table, filter: Partial<T>): T[] | null;
|
||||
|
||||
/**
|
||||
* @param table Where to get from.
|
||||
* @param arg Arg.
|
||||
*/
|
||||
select<T>(table: Table, arg?: number | Partial<T>): T[] | null {
|
||||
try {
|
||||
if (arg === undefined) {
|
||||
// @ts-expect-error imported
|
||||
return this.data[table].map(clone);
|
||||
}
|
||||
/**
|
||||
* @param table Where to get from.
|
||||
* @param arg Arg.
|
||||
*/
|
||||
select<T>(table: Table, arg?: number | Partial<T>): T[] | null {
|
||||
try {
|
||||
if (arg === undefined) {
|
||||
// @ts-expect-error imported
|
||||
return this.data[table].map(clone);
|
||||
}
|
||||
|
||||
if (typeof arg === "number") {
|
||||
// @ts-expect-error imported
|
||||
return clone(this.data[table].find((d) => d?.id === arg));
|
||||
}
|
||||
if (typeof arg === "number") {
|
||||
// @ts-expect-error imported
|
||||
return clone(this.data[table].find((d) => d?.id === arg));
|
||||
}
|
||||
|
||||
// @ts-expect-error imported
|
||||
return this.data[table].filter(this.makeFilter(arg)).map(clone);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
// @ts-expect-error imported
|
||||
return this.data[table].filter(this.makeFilter(arg)).map(clone);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates data in a table.
|
||||
*
|
||||
* @param table Where to update.
|
||||
* @param id What to update.
|
||||
* @param value How to update.
|
||||
*/
|
||||
/**
|
||||
* Updates data in a table.
|
||||
*
|
||||
* @param table Where to update.
|
||||
* @param id What to update.
|
||||
* @param value How to update.
|
||||
*/
|
||||
|
||||
update<T>(table: Table, id: number, value: T): boolean;
|
||||
update<T>(table: Table, id: number, value: T): boolean;
|
||||
|
||||
/**
|
||||
* Updates data in a table.
|
||||
*
|
||||
* @param table Where to update.
|
||||
* @param filter An object to filter data.
|
||||
* @param value How to update.
|
||||
*/
|
||||
update<T>(table: Table, filter: Partial<T>, value: Partial<T>): boolean;
|
||||
/**
|
||||
* Updates data in a table.
|
||||
*
|
||||
* @param table Where to update.
|
||||
* @param filter An object to filter data.
|
||||
* @param value How to update.
|
||||
*/
|
||||
update<T>(table: Table, filter: Partial<T>, value: Partial<T>): boolean;
|
||||
|
||||
/**
|
||||
* Updates data in a table.
|
||||
*
|
||||
* @param table Where to update.
|
||||
* @param arg
|
||||
* @param value How to update.
|
||||
*/
|
||||
update<T>(
|
||||
table: Table,
|
||||
arg: number | Partial<T>,
|
||||
value?: Partial<T>,
|
||||
): boolean {
|
||||
if (typeof arg === "number") {
|
||||
if (
|
||||
this.data[table][arg] &&
|
||||
value &&
|
||||
// @ts-expect-error idc
|
||||
this.data[table][arg].id !== value.id
|
||||
) {
|
||||
throw new Error("ID mismatch.");
|
||||
}
|
||||
try {
|
||||
// @ts-expect-error imported
|
||||
this.data[table][arg] = value;
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Updates data in a table.
|
||||
*
|
||||
* @param table Where to update.
|
||||
* @param arg
|
||||
* @param value How to update.
|
||||
*/
|
||||
update<T>(
|
||||
table: Table,
|
||||
arg: number | Partial<T>,
|
||||
value?: Partial<T>,
|
||||
): boolean {
|
||||
if (typeof arg === "number") {
|
||||
if (
|
||||
this.data[table][arg] &&
|
||||
value &&
|
||||
// @ts-expect-error idc
|
||||
this.data[table][arg].id !== value.id
|
||||
) {
|
||||
throw new Error("ID mismatch.");
|
||||
}
|
||||
try {
|
||||
// @ts-expect-error imported
|
||||
this.data[table][arg] = value;
|
||||
return true;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const values = this.data[table].filter(this.makeFilter(arg));
|
||||
if (!values) {
|
||||
return false;
|
||||
}
|
||||
const values = this.data[table].filter(this.makeFilter(arg));
|
||||
if (!values) {
|
||||
return false;
|
||||
}
|
||||
|
||||
values.forEach((v: { id: any }) => {
|
||||
const existing = this.data[table][v.id];
|
||||
for (const key in value) {
|
||||
if (
|
||||
// @ts-expect-error imported
|
||||
existing[key] &&
|
||||
typeof existing[key] === "object" &&
|
||||
typeof value[key] === "object"
|
||||
) {
|
||||
Object.assign(existing[key], value[key]); // For opponent objects, this does a deep merge of level 2.
|
||||
} else {
|
||||
// @ts-expect-error imported
|
||||
existing[key] = value[key]; // Otherwise, do a simple value assignment.
|
||||
}
|
||||
}
|
||||
this.data[table][v.id] = existing;
|
||||
});
|
||||
for (const v of values) {
|
||||
const existing = this.data[table][v.id];
|
||||
for (const key in value) {
|
||||
if (
|
||||
// @ts-expect-error imported
|
||||
existing[key] &&
|
||||
typeof existing[key] === "object" &&
|
||||
typeof value[key] === "object"
|
||||
) {
|
||||
Object.assign(existing[key], value[key]); // For opponent objects, this does a deep merge of level 2.
|
||||
} else {
|
||||
// @ts-expect-error imported
|
||||
existing[key] = value[key]; // Otherwise, do a simple value assignment.
|
||||
}
|
||||
}
|
||||
this.data[table][v.id] = existing;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empties a table completely.
|
||||
*
|
||||
* @param table Where to delete everything.
|
||||
*/
|
||||
delete(table: Table): boolean;
|
||||
/**
|
||||
* Delete data in a table, based on a filter.
|
||||
*
|
||||
* @param table Where to delete in.
|
||||
* @param filter An object to filter data.
|
||||
*/
|
||||
delete<T>(table: Table, filter: Partial<T>): boolean;
|
||||
/**
|
||||
* Empties a table completely.
|
||||
*
|
||||
* @param table Where to delete everything.
|
||||
*/
|
||||
delete(table: Table): boolean;
|
||||
/**
|
||||
* Delete data in a table, based on a filter.
|
||||
*
|
||||
* @param table Where to delete in.
|
||||
* @param filter An object to filter data.
|
||||
*/
|
||||
delete<T>(table: Table, filter: Partial<T>): boolean;
|
||||
|
||||
/**
|
||||
* Delete data in a table, based on a filter.
|
||||
*
|
||||
* @param table Where to delete in.
|
||||
* @param filter An object to filter data.
|
||||
*/
|
||||
delete<T>(table: Table, filter?: Partial<T>): boolean {
|
||||
const values = this.data[table];
|
||||
if (!values) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Delete data in a table, based on a filter.
|
||||
*
|
||||
* @param table Where to delete in.
|
||||
* @param filter An object to filter data.
|
||||
*/
|
||||
delete<T>(table: Table, filter?: Partial<T>): boolean {
|
||||
const values = this.data[table];
|
||||
if (!values) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!filter) {
|
||||
this.data[table] = [];
|
||||
if (!filter) {
|
||||
this.data[table] = [];
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
const predicate = this.makeFilter(filter);
|
||||
const negativeFilter = (value: any): boolean => !predicate(value);
|
||||
const predicate = this.makeFilter(filter);
|
||||
const negativeFilter = (value: any): boolean => !predicate(value);
|
||||
|
||||
// @ts-expect-error imported
|
||||
this.data[table] = values.filter(negativeFilter);
|
||||
// @ts-expect-error imported
|
||||
this.data[table] = values.filter(negativeFilter);
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user