This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/rhai_engine/examples/loadscripts/scripts/string_utils.rhai
2025-04-03 12:31:01 +02:00

43 lines
1005 B
Plaintext

// 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
}