frontend: get rid of evil eval function in table sorting
This commit is contained in:
@@ -6,7 +6,7 @@ import {BTable, type TableItem} from 'bootstrap-vue-next';
|
||||
import type {TableField, TableFieldObject} from 'bootstrap-vue-next/dist/src/types';
|
||||
import type {ViewEntry, ViewList} from '@/components/manga/MangaList.vue';
|
||||
import MangaEntryDetailsModal from '@/components/manga/MangaEntryDetailsModal.vue';
|
||||
import {latestChaptersSorted, latestChapterString, newChapterCount} from '@/components/manga/util.manga';
|
||||
import {get, latestChaptersSorted, latestChapterString, newChapterCount} from '@/components/manga/util.manga';
|
||||
|
||||
type HeadData<I> = {
|
||||
label: string,
|
||||
@@ -89,7 +89,7 @@ export default class MangaListTable extends Vue {
|
||||
if (!this.sortKey) {
|
||||
return this.tableEntries;
|
||||
}
|
||||
const keyExtractor = (e: ViewEntry) => eval('e.' + this.sortKey);//TODO eval is evil
|
||||
const keyExtractor = (e: ViewEntry) => get(e, this.sortKey);//TODO eval is evil
|
||||
const comparer = (l: ViewEntry, r: ViewEntry) => {
|
||||
const lkey = keyExtractor(l);
|
||||
const rkey = keyExtractor(r);
|
||||
|
||||
@@ -18,3 +18,29 @@ export function newChapterCount(entry: ViewEntry): number {
|
||||
const max = entry.media?.chapters || entry.chapters.reduce((l, r) => Math.max(l, r.chapter), 0);
|
||||
return Math.max(0, max - entry.entry.progress);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dynamically get a nested value from an array or
|
||||
* object with a string.
|
||||
*
|
||||
* @example get(person, 'friends[0].name')
|
||||
* @link https://github.com/rayepps/radash/blob/master/src/object.ts#L214
|
||||
*/
|
||||
export const get = <TDefault = unknown>(
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
value: any,
|
||||
path: string,
|
||||
defaultValue?: TDefault
|
||||
): TDefault => {
|
||||
const segments = path.split(/[.[\]]/g)
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let current: any = value
|
||||
for (const key of segments) {
|
||||
if (current === null) return defaultValue as TDefault
|
||||
if (current === undefined) return defaultValue as TDefault
|
||||
if (key.trim() === '') continue
|
||||
current = current[key]
|
||||
}
|
||||
if (current === undefined) return defaultValue as TDefault
|
||||
return current
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user