Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unicode aware Capitalize filter #17

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ pub fn upcase(input: &Value, _args: &[Value]) -> FilterResult {
}
}


pub fn capitalize(input: &Value, _args: &[Value]) -> FilterResult {
match *input {
Str(ref s) => Ok(Str(s.char_indices().fold(String::new(), |word, (_, chr)| {
let next_char = match word.chars().last() {
Some(last) => match last.is_whitespace() {
true => chr.to_uppercase().next().unwrap(),
false => chr,
},
_ => chr.to_uppercase().next().unwrap(),
}.to_string();
word + &next_char
}))),
_ => Err(InvalidType("String expected".to_owned())),
}
}

pub fn minus(input: &Value, args: &[Value]) -> FilterResult {

let num = match *input {
Expand Down Expand Up @@ -120,6 +137,22 @@ mod tests {
tos!("HELLO WORLD 21"));
}

#[test]
fn unit_capitalize() {
assert_eq!(unit!(capitalize, tos!("abc")), tos!("Abc"));
assert_eq!(unit!(capitalize, tos!("hello world 21")),
tos!("Hello World 21"));

// sure that Umlauts work
assert_eq!(unit!(capitalize, tos!("über ètat, y̆es?")),
tos!("Über Ètat, Y\u{306}es?"));

// Weird UTF-8 White space is kept – this is a no-break whitespace!
assert_eq!(unit!(capitalize, tos!("hello world​")),
tos!("Hello World​"));

}

#[test]
fn unit_minus() {
assert_eq!(unit!(minus, Num(2f32), &[Num(1f32)]), Num(1f32));
Expand Down
3 changes: 2 additions & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use Renderable;
use context::Context;
use filters::{size, upcase, minus, replace};
use filters::{size, upcase, minus, capitalize, replace};
use error::Result;

pub struct Template {
Expand All @@ -11,6 +11,7 @@ impl Renderable for Template {
fn render(&self, context: &mut Context) -> Result<Option<String>> {
context.filters.insert("size".to_owned(), Box::new(size));
context.filters.insert("upcase".to_owned(), Box::new(upcase));
context.filters.insert("capitalize".to_owned(), Box::new(capitalize));
context.filters.insert("minus".to_owned(), Box::new(minus));
context.filters.insert("replace".to_owned(), Box::new(replace));

Expand Down
14 changes: 14 additions & 0 deletions tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ pub fn upcase() {
assert_eq!(output.unwrap(), Some("HELLO".to_string()));
}


#[test]
pub fn capitalize() {
let text = "{{ text | capitalize}}";
let options : LiquidOptions = Default::default();
let template = parse(&text, options).unwrap();

let mut data = Context::new();
data.set_val("text", Value::Str("hello world".to_string()));

let output = template.render(&mut data);
assert_eq!(output.unwrap(), Some("Hello World".to_string()));
}

#[test]
pub fn minus() {
let text = "{{ num | minus : 2 }}";
Expand Down