You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.3 KiB
36 lines
1.3 KiB
/** Level 配表字段解析(标量或按索引数组) */
|
|
export class LevelFieldResolver {
|
|
/** 取数值字段 */
|
|
static pickNumber(value: number | number[] | undefined | null, index: number, fallback: number): number {
|
|
if (Array.isArray(value)) {
|
|
if (index >= 0 && index < value.length) {
|
|
const n = Number(value[index]);
|
|
return Number.isFinite(n) ? n : fallback;
|
|
}
|
|
if (value.length > 0) {
|
|
const n = Number(value[0]);
|
|
return Number.isFinite(n) ? n : fallback;
|
|
}
|
|
return fallback;
|
|
}
|
|
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
|
return fallback;
|
|
}
|
|
|
|
/** 取泛型字段 */
|
|
static pickField<T>(value: T | T[] | undefined | null, index: number, fallback: T): T {
|
|
if (value == null) return fallback;
|
|
if (Array.isArray(value)) {
|
|
if (index >= 0 && index < value.length) return value[index];
|
|
return value.length > 0 ? value[0] : fallback;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
/** 取字符串;`#` 前为默认段 */
|
|
static pickString(value: string | undefined | null): string {
|
|
if (!value) return '';
|
|
if (!value.includes('#')) return value;
|
|
return value.split('#')[0] ?? '';
|
|
}
|
|
}
|
|
|