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

implement split filter #44

Merged
merged 1 commit into from
Jun 15, 2016
Merged
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 @@ -244,6 +244,31 @@ pub fn last(input: &Value, _args: &[Value]) -> FilterResult {
}
}

pub fn split(input: &Value, args: &[Value]) -> FilterResult {
// Make sure there is only 1 argument to split
if args.len() != 1 {
return Err(InvalidArgumentCount(format!("expected 1, {} given", args.len())));
}


match *input {
Str(ref string_to_split) => {
// the input String is in fact a String
match args.first() { // Check the first (and only) argument
Some(&Str(ref split_string)) => {
// The split string argument is also in fact a String
// Split and construct resulting Array
Ok(Array(string_to_split.split(split_string)
.map(|x| Str(String::from(x)))
.collect()))
}
_ => Err(InvalidArgument(0, "expected String argument to split".to_owned())),
}
}
_ => Err(InvalidType("String expected".to_owned())),
}
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -402,4 +427,12 @@ mod tests {
tos!("last"));
assert_eq!(unit!(last, Array(vec![])), tos!(""));
}

#[test]
fn unit_split() {
assert_eq!(unit!(split, tos!("a, b, c"), &[tos!(", ")]),
Array(vec![tos!("a"), tos!("b"), tos!("c")]));
assert_eq!(unit!(split, tos!("a~b"), &[tos!("~")]),
Array(vec![tos!("a"), tos!("b")]));
}
}
2 changes: 2 additions & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use Renderable;
use context::Context;
use filters::{size, upcase, downcase, capitalize, minus, plus, times, divided_by, ceil, floor,
round, prepend, append, first, last, pluralize, replace};
use filters::split;
use error::Result;

pub struct Template {
Expand All @@ -28,6 +29,7 @@ impl Renderable for Template {
context.add_filter("append", Box::new(append));
context.add_filter("replace", Box::new(replace));
context.add_filter("pluralize", Box::new(pluralize));
context.add_filter("split", Box::new(split));

let mut buf = String::new();
for el in &self.elements {
Expand Down
28 changes: 28 additions & 0 deletions tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,31 @@ pub fn append() {
let output = template.render(&mut data);
assert_eq!(output.unwrap(), Some("roobarblifo".to_string()));
}

#[test]
// Got this test from example at https://shopify.github.io/liquid/filters/split/
// This is an additional test to verify the comma/space parsing is also working
// from https://github.com/cobalt-org/liquid-rust/issues/41
pub fn split_with_comma() {
let text = "{% assign beatles = \"John, Paul, George, Ringo\" | split: \", \" %}{% for member in beatles %}{{ member }}\n{% endfor %}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could also use single quotes to avoid having to escape them

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it as single quotes originally, but I changed when #41 was still a thing.
Strictly speaking, these don't need to be double quotes.

Do you want them changed? Doesn't matter to me... Seems like there are already plenty of tests that do single quotes but none that use double quotes. Sometimes helps to see examples...

Up to you.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine if you leave them!

let options : LiquidOptions = Default::default();
let template = parse(&text, options).unwrap();

let mut data = Context::new();

let output = template.render(&mut data);
assert_eq!(output.unwrap(), Some("John\nPaul\nGeorge\nRingo\n".to_string()));
}

#[test]
// This test verifies that issue https://github.com/cobalt-org/liquid-rust/issues/40 is fixed (that split works)
pub fn split_no_comma() {
let text = "{% assign letters = \"a~b~c\" | split:\"~\" %}{% for letter in letters %}LETTER: {{ letter }}\n{% endfor %}";
let options : LiquidOptions = Default::default();
let template = parse(&text, options).unwrap();

let mut data = Context::new();

let output = template.render(&mut data);
assert_eq!(output.unwrap(), Some("LETTER: a\nLETTER: b\nLETTER: c\n".to_string()));
}