Skip to content

Commit

Permalink
Merge pull request #210 from Goncalerta/master
Browse files Browse the repository at this point in the history
Add filters: "at_least" and "at_most".
  • Loading branch information
epage authored Oct 15, 2018
2 parents 4c5563a + be3e55c commit 620e92a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
84 changes: 84 additions & 0 deletions src/filters/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,52 @@ pub fn abs(input: &Value, args: &[Value]) -> FilterResult {
}
}

/// Limits a number to a minimum value.
pub fn at_least(input: &Value, args: &[Value]) -> FilterResult {
check_args_len(args, 1, 0)?;
let input = input
.as_scalar()
.ok_or_else(|| invalid_input("Number expected"))?;

let max_value = args[0]
.as_scalar()
.ok_or_else(|| invalid_argument(0, "Number expected"))?;

let result = input
.to_integer()
.and_then(|i| max_value.to_integer().map(|max| Value::scalar(i.max(max))))
.or_else(|| {
input
.to_float()
.and_then(|i| max_value.to_float().map(|max| Value::scalar(i.max(max))))
}).ok_or_else(|| invalid_argument(0, "Number expected"))?;

Ok(result)
}

/// Limits a number to a maximum value.
pub fn at_most(input: &Value, args: &[Value]) -> FilterResult {
check_args_len(args, 1, 0)?;
let input = input
.as_scalar()
.ok_or_else(|| invalid_input("Number expected"))?;

let max_value = args[0]
.as_scalar()
.ok_or_else(|| invalid_argument(0, "Number expected"))?;

let result = input
.to_integer()
.and_then(|i| max_value.to_integer().map(|max| Value::scalar(i.min(max))))
.or_else(|| {
input
.to_float()
.and_then(|i| max_value.to_float().map(|max| Value::scalar(i.min(max))))
}).ok_or_else(|| invalid_argument(0, "Number expected"))?;

Ok(result)
}

pub fn plus(input: &Value, args: &[Value]) -> FilterResult {
check_args_len(args, 1, 0)?;

Expand Down Expand Up @@ -199,6 +245,44 @@ mod tests {
assert_eq!(unit!(abs, tos!("-19.86"), &[]), Value::scalar(19.86f64));
}
#[test]
fn unit_at_least() {
assert_eq!(
unit!(at_least, Value::scalar(4f64), &[Value::scalar(5f64)]),
Value::scalar(5f64)
);
assert_eq!(
unit!(at_least, Value::scalar(4f64), &[Value::scalar(3f64)]),
Value::scalar(4f64)
);
assert_eq!(
unit!(at_least, Value::scalar(21.5), &[Value::scalar(2.25)]),
Value::scalar(21.5)
);
assert_eq!(
unit!(at_least, Value::scalar(21.5), &[Value::scalar(42.25)]),
Value::scalar(42.25)
);
}
#[test]
fn unit_at_most() {
assert_eq!(
unit!(at_most, Value::scalar(4f64), &[Value::scalar(5f64)]),
Value::scalar(4f64)
);
assert_eq!(
unit!(at_most, Value::scalar(4f64), &[Value::scalar(3f64)]),
Value::scalar(3f64)
);
assert_eq!(
unit!(at_most, Value::scalar(21.5), &[Value::scalar(2.25)]),
Value::scalar(2.25)
);
assert_eq!(
unit!(at_most, Value::scalar(21.5), &[Value::scalar(42.25)]),
Value::scalar(21.5)
);
}
#[test]
fn unit_plus() {
assert_eq!(
unit!(plus, Value::scalar(2f64), &[Value::scalar(1f64)]),
Expand Down
2 changes: 1 addition & 1 deletion src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use self::date::date;
#[cfg(feature = "extra-filters")]
pub use self::date::date_in_tz;
pub use self::html::{escape, escape_once, newline_to_br, strip_html};
pub use self::math::{abs, divided_by, minus, modulo, plus, times};
pub use self::math::{abs, at_least, at_most, divided_by, minus, modulo, plus, times};
pub use self::url::{url_decode, url_encode};

use std::borrow::Cow;
Expand Down
2 changes: 2 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl ParserBuilder {
pub fn liquid_filters(self) -> Self {
self.filter("abs", filters::abs as interpreter::FnFilterValue)
.filter("append", filters::append as interpreter::FnFilterValue)
.filter("at_least", filters::at_least as interpreter::FnFilterValue)
.filter("at_most", filters::at_most as interpreter::FnFilterValue)
.filter(
"capitalize",
filters::capitalize as interpreter::FnFilterValue,
Expand Down

0 comments on commit 620e92a

Please sign in to comment.