Files
Firefly_Srtools/src/helper/replaceByParam.ts
T
Kain344 ca61ddfd5f
Gitea Auto Deploy / Deploy-Container (push) Successful in 50s
feat: enhance locale handling and improve cookie management in Header component
fix: update CopyImport component to handle profile selection and data validation

feat: refactor EnkaImport component for better data parsing and error handling

fix: improve FreeSRImport component with enhanced data validation and error management

fix: update calcData helper functions to handle edge cases and improve robustness

refactor: enhance converter functions for better data handling and validation

feat: add validation to persisted stores using Zod schemas

fix: update Zustand stores to use validated JSON storage for improved data integrity

chore: add new Zod schemas for locale and connection persistence

fix: ensure proper handling of optional fields in various data structures
2026-05-15 20:34:15 +07:00

40 lines
1.4 KiB
TypeScript

const formatValue = (value: number, format: string, floatDigits?: string, hasPercent?: boolean): string => {
if (format.startsWith('f')) {
const digits = parseInt(floatDigits || "1", 10);
const num = hasPercent ? value * 100 : value;
return `${num.toFixed(digits)}${hasPercent ? "%" : ""}`;
}
if (format === 'i') {
const num = hasPercent ? value * 100 : value;
return `${Math.round(num)}${hasPercent ? "%" : ""}`;
}
return String(value);
};
export function replaceByParam(desc?: string, params: number[] = []): string {
if (!desc) return "";
const PARAM_REGEX = /#(\d+)\[(f(\d+)|i)\](%)?/g;
const processor = (_match: string, index: string, format: string, digits?: string, percent?: string): string => {
const i = parseInt(index, 10) - 1;
const val = params[i];
return val !== undefined ? formatValue(val, format, digits, !!percent) : "";
};
let result = desc.replace(/<color=(#[0-9a-fA-F]{8})>(.*?)<\/color>/g, (_, color, inner) => {
const processedInner = inner.replace(PARAM_REGEX, processor);
return `<span style="color: ${color}">${processedInner}</span>`;
});
result = result.replace(/<unbreak>(.*?)<\/unbreak>/g, (_, inner) => {
return inner.replace(PARAM_REGEX, processor);
});
result = result.replace(PARAM_REGEX, processor);
return result.split("\\n").join("<br/>");
}