Last active
May 11, 2024 13:16
-
-
Save Kerollmops/f32380ea64003f9f48ffac25e7ca9d2a to your computer and use it in GitHub Desktop.
A Rhai little function to title case strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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