...
This commit is contained in:
32
rhai_engine/examples/loadscripts/scripts/math_utils.rhai
Normal file
32
rhai_engine/examples/loadscripts/scripts/math_utils.rhai
Normal file
@@ -0,0 +1,32 @@
|
||||
// Math utility functions for Tera templates
|
||||
|
||||
// Format a number with commas as thousands separators
|
||||
fn format_number(num) {
|
||||
let str = num.to_string();
|
||||
let result = "";
|
||||
let len = str.len;
|
||||
|
||||
for i in 0..len {
|
||||
if i > 0 && (len - i) % 3 == 0 {
|
||||
result += ",";
|
||||
}
|
||||
result += str.substr(i, 1);
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
// Calculate percentage
|
||||
fn percentage(value, total) {
|
||||
if total == 0 {
|
||||
return "0%";
|
||||
}
|
||||
|
||||
let pct = (value / total) * 100.0;
|
||||
pct.round().to_string() + "%"
|
||||
}
|
||||
|
||||
// Format currency
|
||||
fn currency(amount, symbol) {
|
||||
symbol + format_number(amount.round(2))
|
||||
}
|
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
|
||||
}
|
56
rhai_engine/examples/loadscripts/scripts/test_utils.rhai
Normal file
56
rhai_engine/examples/loadscripts/scripts/test_utils.rhai
Normal file
@@ -0,0 +1,56 @@
|
||||
// Test utility functions to verify dynamic function discovery
|
||||
// These functions were not in the original hardcoded lists
|
||||
|
||||
// Reverse a string
|
||||
fn reverse_string(text) {
|
||||
let result = "";
|
||||
let i = text.len - 1;
|
||||
|
||||
// Using a different approach to reverse the string
|
||||
// We'll iterate backwards with a while loop instead of using step
|
||||
while i >= 0 {
|
||||
result += text.substr(i, 1);
|
||||
i -= 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
// Count words in a string - rewritten to avoid split issues
|
||||
fn count_words(text) {
|
||||
if text.len == 0 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Manual word counting implementation
|
||||
let count = 1; // Start with 1 for the first word
|
||||
let in_word = true;
|
||||
|
||||
for i in 0..text.len {
|
||||
let char = text.substr(i, 1);
|
||||
|
||||
if char == " " {
|
||||
in_word = false;
|
||||
} else if !in_word {
|
||||
// Found a non-space after a space = new word
|
||||
count += 1;
|
||||
in_word = true;
|
||||
}
|
||||
}
|
||||
|
||||
if text.substr(0, 1) == " " {
|
||||
// If text starts with space, reduce count
|
||||
count -= 1;
|
||||
}
|
||||
|
||||
count
|
||||
}
|
||||
|
||||
// Calculate factorial
|
||||
fn factorial(n) {
|
||||
// Ensure consistent integer type using to_int()
|
||||
let num = n.to_int();
|
||||
if num <= 1 {
|
||||
return 1;
|
||||
}
|
||||
num * factorial(num-1)
|
||||
}
|
Reference in New Issue
Block a user