2025-10-01 22:41:23 -05:00
|
|
|
export function literals(
|
|
|
|
|
string: string,
|
2026-03-19 17:17:23 -05:00
|
|
|
literals?: Record<string, string | number | undefined>,
|
2025-10-01 22:41:23 -05:00
|
|
|
) {
|
|
|
|
|
for (const key in literals) {
|
2026-03-19 17:17:23 -05:00
|
|
|
string = string.replaceAll(`{${key}}`, `${literals[key]}`);
|
2025-10-01 22:41:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return string;
|
2025-01-06 20:09:11 -06:00
|
|
|
}
|
2026-03-19 17:17:23 -05:00
|
|
|
|
|
|
|
|
export function literalsArray<Type, Literal = Type | string>(
|
|
|
|
|
string: string,
|
|
|
|
|
literals?: Record<string, Literal>,
|
|
|
|
|
) {
|
|
|
|
|
if (!literals) return [string];
|
|
|
|
|
|
|
|
|
|
let array: (string | Literal)[] = [string];
|
|
|
|
|
|
|
|
|
|
for (const key in literals) {
|
|
|
|
|
const value = literals[key];
|
|
|
|
|
const next: (string | Literal)[] = [];
|
|
|
|
|
|
|
|
|
|
for (const chunk of array) {
|
|
|
|
|
if (typeof chunk !== "string") {
|
|
|
|
|
next.push(chunk);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parts = chunk.split(`{${key}}`);
|
|
|
|
|
|
|
|
|
|
for (let index = 0; index < parts.length; index++) {
|
|
|
|
|
if (parts[index]) next.push(parts[index]);
|
|
|
|
|
|
|
|
|
|
if (index < parts.length - 1) {
|
|
|
|
|
next.push(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
array = next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
}
|