| 1 | export const capitalizeFirstLetter = (val: string) => { |
| 2 | if (val.length === 0) { |
| 3 | return ""; |
| 4 | } |
| 5 | return val[0].toUpperCase() + val.slice(1); |
| 6 | }; |
| 7 | |
| 8 | export function replaceEscapedCharacters(str: string): string { |
| 9 | return str.replaceAll(/\\(n|t|r|\\|"|')/g, (match, p1) => { |
| 10 | switch (p1) { |
| 11 | case "n": |
| 12 | return "\n"; |
| 13 | case "t": |
| 14 | return "\t"; |
| 15 | case "r": |
| 16 | return "\r"; |
| 17 | case "\\": |
| 18 | return "\\"; |
| 19 | case '"': |
| 20 | return '"'; |
| 21 | case "'": |
| 22 | return "'"; |
| 23 | default: |
| 24 | return match; // NOTE: Handle unexpected escapes better than this. |
| 25 | } |
| 26 | }); |
| 27 | } |
| 28 | |
| 29 | export function escapeForSVG(text: string): string { |
| 30 | return text |
| 31 | .replace(/&/g, "&") // must be first |
| 32 | .replace(/</g, "<") |
| 33 | .replace(/>/g, ">") |
| 34 | .replace(/"/g, """) |
| 35 | .replace(/'/g, "'") |
| 36 | .replace(/\n/g, "\\n") // newlines |
| 37 | .replace(/\t/g, "\\t") // tabs |
| 38 | .replace(/\r/g, "\\r"); // carriage returns |
| 39 | } |
| 40 | |
| 41 | export function kebabOfStr(str: string): string { |
| 42 | return str |
| 43 | .replace(/([a-z0-9])([A-Z])/g, "$1-$2") // handle camelCase, PascalCase, and numbers followed by uppercase |
| 44 | .replace(/[\s_]+/g, "-") // replace spaces and underscores with hyphens |
| 45 | .toLowerCase(); |
| 46 | } |
| 47 | |
| 48 | export function kebabOfThemeStr(str: string): string { |
| 49 | return str |
| 50 | .toLowerCase() |
| 51 | .replace(/[\s_]+/g, "-") // replace spaces and underscores with hyphens |
| 52 | .replace(/\(|\)/g, ""); // remove parentheses |
| 53 | } |