|
| 1 | +// Converts string to lowercase, removes symbols, replaces spaces with dashes |
| 2 | +function toSlug(text) { |
| 3 | + let str = text.toLowerCase(); |
| 4 | + str = str.replace(/[^a-z0-9_\s-]/g, ""); |
| 5 | + str = str.replace(/[\s-]+/g, " "); |
| 6 | + str = str.replace(/[\s_]/g, "-"); |
| 7 | + return str; |
| 8 | +}; |
| 9 | + |
| 10 | +// Converts dashed/underscored text back to words with spaces |
| 11 | +function fromSlugToWords(text) { |
| 12 | + return text.replace(/[-_]/g, " "); |
| 13 | +}; |
| 14 | + |
| 15 | +// Converts dashed/underscored text to continuous word |
| 16 | +function fromSlugToCompact(text) { |
| 17 | + return text.replace(/[-_]/g, ""); |
| 18 | +}; |
| 19 | + |
| 20 | +// Replaces spaces with underscores |
| 21 | +function toUnderscoreCase(text) { |
| 22 | + return text.replace(/\s/g, "_"); |
| 23 | +}; |
| 24 | + |
| 25 | +// Capitalizes the first letter of each word |
| 26 | +function capitalizeWords(text) { |
| 27 | + return text.toLowerCase().replace(/(^\w{1})|(\s+\w{1})/g, (letter) => letter.toUpperCase()); |
| 28 | +}; |
| 29 | + |
| 30 | +// Converts entire string to uppercase |
| 31 | +function toUpperCase(text) { |
| 32 | + return text ? text.toUpperCase() : text; |
| 33 | +}; |
| 34 | + |
| 35 | +// Converts entire string to lowercase |
| 36 | +function toLowerCase(text) { |
| 37 | + return text ? text.toLowerCase() : text; |
| 38 | +}; |
| 39 | + |
| 40 | +// Removes spaces from start and end |
| 41 | +function trimText(text) { |
| 42 | + return text.trim(); |
| 43 | +}; |
0 commit comments