Skip to content

Instantly share code, notes, and snippets.

@Kerollmops
Last active May 11, 2024 13:16
Show Gist options
  • Save Kerollmops/f32380ea64003f9f48ffac25e7ca9d2a to your computer and use it in GitHub Desktop.
Save Kerollmops/f32380ea64003f9f48ffac25e7ca9d2a to your computer and use it in GitHub Desktop.
A Rhai little function to title case strings
// Meilisearch is setting the document for you, this way.
// Note we use the rhai.rs file extension to benefit from
// the Rust Syntax Highlighting only, it's Rhai nothing more.
let doc = #{ title: "star wars" };
print(doc);
// -- script starts here --
fn to_title_case() {
let title_cased = "";
let must_upper_case = true;
for c in this.to_chars() {
if c == " " {
must_upper_case = true;
title_cased += " ";
} else if must_upper_case {
title_cased += c.to_upper();
must_upper_case = false;
} else {
title_cased += c.to_lower();
}
}
this = title_cased;
}
doc.title.to_title_case();
// -- and ends here --
print(doc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment