mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-06-12 00:54:53 -07:00
Add ESLint & Run it against the system (#4470)
This commit is contained in:
parent
43eee6b32e
commit
f789e02096
106 changed files with 1235 additions and 20242 deletions
|
|
@ -1,22 +1,22 @@
|
|||
import { SortBy, SortDirection, User } from "types";
|
||||
import { SortBy, SortDirection, User } from 'types';
|
||||
|
||||
export default class SortUtil {
|
||||
static sortByField(arr: any[], sortBy: SortBy): void {
|
||||
static sortByField(arr: any[], sortBy: SortBy): void {
|
||||
if (arr.length) {
|
||||
const field = SortUtil.resolveFieldChain(arr[0], sortBy.field);
|
||||
const fieldType = typeof field;
|
||||
|
||||
if (fieldType === "string") {
|
||||
if (fieldType === 'string') {
|
||||
SortUtil.sortByString(arr, sortBy);
|
||||
return;
|
||||
}
|
||||
|
||||
if (fieldType === "number") {
|
||||
if (fieldType === 'number') {
|
||||
SortUtil.sortByNumber(arr, sortBy);
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error("SortField must resolve to either a string or number");
|
||||
throw new Error('SortField must resolve to either a string or number');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -26,10 +26,10 @@ export default class SortUtil {
|
|||
for (let i = 0; i < sorts.length; i++) {
|
||||
const sortBy = sorts[i];
|
||||
const field = SortUtil.resolveFieldChain(arr[0], sortBy.field);
|
||||
|
||||
|
||||
const fieldType = typeof field;
|
||||
|
||||
if (fieldType === "string") {
|
||||
if (fieldType === 'string') {
|
||||
const result = SortUtil.stringComparator(a, b, sortBy);
|
||||
|
||||
if (result) {
|
||||
|
|
@ -37,7 +37,7 @@ export default class SortUtil {
|
|||
}
|
||||
}
|
||||
|
||||
if (fieldType === "number") {
|
||||
if (fieldType === 'number') {
|
||||
const result = SortUtil.numberComparator(a, b, sortBy);
|
||||
|
||||
if (result) {
|
||||
|
|
@ -45,7 +45,7 @@ export default class SortUtil {
|
|||
}
|
||||
}
|
||||
|
||||
throw new Error("SortField must resolve to either a string or number");
|
||||
throw new Error('SortField must resolve to either a string or number');
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
@ -80,7 +80,7 @@ export default class SortUtil {
|
|||
private static userComparator(a, b, sortBy, sortByUserLevel = true) {
|
||||
if (sortByUserLevel) {
|
||||
const adminSortBy = {
|
||||
field: "userLevel",
|
||||
field: 'userLevel',
|
||||
order: SortDirection.DESC
|
||||
};
|
||||
|
||||
|
|
@ -116,9 +116,15 @@ export default class SortUtil {
|
|||
const bResolved = SortUtil.resolveFieldChain(b, field);
|
||||
|
||||
// Force empty strings to sort to bottom
|
||||
if (!aResolved && !bResolved) { return 0; }
|
||||
if (!aResolved) { return 1; }
|
||||
if (!bResolved) { return -1; }
|
||||
if (!aResolved && !bResolved) {
|
||||
return 0;
|
||||
}
|
||||
if (!aResolved) {
|
||||
return 1;
|
||||
}
|
||||
if (!bResolved) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (order === SortDirection.ASC) {
|
||||
return aResolved.localeCompare(bResolved);
|
||||
|
|
@ -128,13 +134,13 @@ export default class SortUtil {
|
|||
}
|
||||
|
||||
private static resolveFieldChain(obj: object, field: string) {
|
||||
const links = field.split(".");
|
||||
const links = field.split('.');
|
||||
|
||||
if (links.length > 1) {
|
||||
return links.reduce((obj, link) => {
|
||||
const parsed = parseInt(link, 10);
|
||||
|
||||
if (parsed.toLocaleString() === "NaN") {
|
||||
if (parsed.toLocaleString() === 'NaN') {
|
||||
return obj[link];
|
||||
} else {
|
||||
return obj[parsed];
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
export { default as SortUtil} from "./SortUtil";
|
||||
export { default as SortUtil } from './SortUtil';
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue