...
This commit is contained in:
43
rhai_engine/examples/loadscripts/scripts/string_utils.rhai
Normal file
43
rhai_engine/examples/loadscripts/scripts/string_utils.rhai
Normal file
@@ -0,0 +1,43 @@
|
||||
// String manipulation functions that will be exposed to Tera templates
|
||||
|
||||
// Capitalize the first letter of each word
|
||||
fn capitalize(text) {
|
||||
if text.len == 0 {
|
||||
return "";
|
||||
}
|
||||
|
||||
let words = text.split(" ");
|
||||
let result = [];
|
||||
|
||||
for word in words {
|
||||
if word.len > 0 {
|
||||
let first_char = word.substr(0, 1).to_upper();
|
||||
let rest = word.substr(1, word.len - 1);
|
||||
result.push(first_char + rest);
|
||||
} else {
|
||||
result.push("");
|
||||
}
|
||||
}
|
||||
|
||||
result.join(" ")
|
||||
}
|
||||
|
||||
// Truncate text with ellipsis
|
||||
fn truncate(text, max_length) {
|
||||
if text.len <= max_length {
|
||||
return text;
|
||||
}
|
||||
|
||||
text.substr(0, max_length - 3) + "..."
|
||||
}
|
||||
|
||||
// Convert text to slug format (lowercase, hyphens)
|
||||
fn slugify(text) {
|
||||
let slug = text.to_lower();
|
||||
slug = slug.replace(" ", "-");
|
||||
slug = slug.replace(".", "");
|
||||
slug = slug.replace(",", "");
|
||||
slug = slug.replace("!", "");
|
||||
slug = slug.replace("?", "");
|
||||
slug
|
||||
}
|
Reference in New Issue
Block a user