feat: add shared util helpers and config
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
export const DEFAULT_BASE_URL = "http://talos.milky.way:9292";
|
||||
|
||||
export interface CfgSettings {
|
||||
baseUrl?: string;
|
||||
apiKey?: string;
|
||||
}
|
||||
|
||||
export interface LlamaSwapConfig {
|
||||
baseUrl: string;
|
||||
apiKey?: string;
|
||||
}
|
||||
|
||||
export function cfgFromSettings(settings: CfgSettings): LlamaSwapConfig {
|
||||
return {
|
||||
baseUrl: normalizeBaseUrl(settings.baseUrl ?? DEFAULT_BASE_URL),
|
||||
apiKey: settings.apiKey || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeBaseUrl(baseUrl: string): string {
|
||||
return baseUrl.trim().replace(/\/+$/, "");
|
||||
}
|
||||
|
||||
export function shorten(name: string, max = 14): string {
|
||||
if (name.length <= max) return name;
|
||||
const head = Math.floor((max - 1) * 0.7);
|
||||
return `${name.slice(0, head)}…${name.slice(name.length - (max - head - 1))}`;
|
||||
}
|
||||
|
||||
export function escapeXml(text: string): string {
|
||||
return text
|
||||
.replace(/&/g, "&")
|
||||
.replace(/</g, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/"/g, """)
|
||||
.replace(/'/g, "'");
|
||||
}
|
||||
Reference in New Issue
Block a user