Skip to content

Commit

Permalink
Add title template function
Browse files Browse the repository at this point in the history
  • Loading branch information
levkk committed Nov 23, 2024
1 parent 6ceb026 commit 994c420
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/docs/views/templates/functions/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,16 @@ This method accepts all data types, but it does convert them to their string rep
```
1 two three
```

### `title`

Converts the string to "Title Case" formatting.

=== "Template"
```erb
<%= "hello world".title %>
```
=== "Output"
```
Hello World
```
10 changes: 10 additions & 0 deletions rwf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ pub fn capitalize(string: &str) -> String {
uppercase
}

/// Convert string to title case.
pub fn title_case(string: &str) -> String {
string
.split(" ")
.into_iter()
.map(|s| capitalize(s))
.collect::<Vec<_>>()
.join(" ")
}

/// Convert string to PascalCase (often confused with camelCase).
pub fn pascal_case(string: &str) -> String {
string
Expand Down
9 changes: 9 additions & 0 deletions rwf/src/view/template/lexer/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ impl Value {
"capitalize" => Value::String(crate::capitalize(&value)),
"camelize" | "to_PascalCase" => Value::String(crate::pascal_case(&value)),
"underscore" | "to_snake_case" => Value::String(crate::snake_case(&value)),
"title" => Value::String(crate::title_case(&value)),
"urlencode" => Value::String(crate::http::urlencode(&value)),
"urldecode" => Value::String(crate::http::urldecode(&value)),
"len" => Value::Integer(value.len() as i64),
Expand Down Expand Up @@ -719,4 +720,12 @@ mod test {
.unwrap();
assert_eq!(v, Value::String("Hey Alice, this is Alice".into()));
}

#[test]
fn test_title_case() {
let v = Value::String("hello world, how are you?".into())
.call("title", &[], &Context::default())
.unwrap();
assert_eq!(v, Value::String("Hello World, How Are You?".into()))
}
}

0 comments on commit 994c420

Please sign in to comment.