Files
ZYZ/shared/pubUtils/dictionary/DicAuthorsBook.ts
2023-07-13 16:23:29 +08:00

46 lines
1.4 KiB
TypeScript

// 列传内的条目
import {decodeArrayListStr, readFileAndParse} from '../util'
import { FILENAME } from '../../consts'
const _ = require('lodash');
export interface DicAuthorsBook {
// 列传id
readonly id: number;
// 限制条件
readonly limit: { type: number, bookId?: number, value: number }[];
}
type KeysEnum<T> = { [P in keyof Required<T>]: true };
const DicAuthorsBookKeys: KeysEnum<DicAuthorsBook> = {
id: true,
limit: true,
}
export const dicAuthorsBook = new Map<number, DicAuthorsBook>();
export function loadAuthorsBook() {
dicAuthorsBook.clear();
let arr = readFileAndParse(FILENAME.DIC_AUTHORS_BOOK);
arr.forEach(o => {
o.limit = parseLimit(o.limit);
dicAuthorsBook.set(o.id, _.pick(o, Object.keys(DicAuthorsBookKeys)));
});
arr = undefined;
}
function parseLimit(str: string) {
let result: { type: number, bookId?: number, value: number }[] = [];
if (!str) return result;
let decodeArr = decodeArrayListStr(str);
for (let [ str1, str2, str3 ] of decodeArr) {
if (isNaN(parseInt(str1)) || isNaN(parseInt(str2)) || (str3 && isNaN(parseInt(str3)))) {
throw new Error('data table format wrong');
}
if(str3) {
result.push({ type: parseInt(str1), bookId: parseInt(str2), value: parseInt(str3) });
} else {
result.push({ type: parseInt(str1), value: parseInt(str2) });
}
}
return result
}