frontend: get rid of evil eval function in table sorting

This commit is contained in:
wea_ondara
2023-11-25 20:48:05 +01:00
parent 8b58758751
commit c21809152d
2 changed files with 28 additions and 2 deletions

View File

@@ -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 {TableField, TableFieldObject} from 'bootstrap-vue-next/dist/src/types';
import type {ViewEntry, ViewList} from '@/components/manga/MangaList.vue'; import type {ViewEntry, ViewList} from '@/components/manga/MangaList.vue';
import MangaEntryDetailsModal from '@/components/manga/MangaEntryDetailsModal.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> = { type HeadData<I> = {
label: string, label: string,
@@ -89,7 +89,7 @@ export default class MangaListTable extends Vue {
if (!this.sortKey) { if (!this.sortKey) {
return this.tableEntries; 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 comparer = (l: ViewEntry, r: ViewEntry) => {
const lkey = keyExtractor(l); const lkey = keyExtractor(l);
const rkey = keyExtractor(r); const rkey = keyExtractor(r);

View File

@@ -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); const max = entry.media?.chapters || entry.chapters.reduce((l, r) => Math.max(l, r.chapter), 0);
return Math.max(0, max - entry.entry.progress); 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
}