Skip to content

Commit

Permalink
(feat) added include zero option for truthy check [#240]
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Oct 17, 2018
1 parent ed16425 commit cec5ec9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/helpers/helper_each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl HelperDef for EachHelper {
.map(|p| format!("{}/{}", rc.get_path(), p));

debug!("each value {:?}", value.value());
let rendered = match (value.value().is_truthy(), value.value()) {
let rendered = match (value.value().is_truthy(false), value.value()) {
(true, &Json::Array(ref list)) => {
let len = list.len();
for (i, _) in list.iter().enumerate().take(len) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helper_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl HelperDef for IfHelper {
.param(0)
.ok_or_else(|| RenderError::new("Param not found for helper \"if\""))?;

let mut value = param.value().is_truthy();
let mut value = param.value().is_truthy(false);

if !self.positive {
value = !value;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helper_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl HelperDef for WithHelper {
let result = {
let mut local_rc = rc.derive();

let not_empty = param.value().is_truthy();
let not_empty = param.value().is_truthy(false);
let template = if not_empty {
h.template()
} else {
Expand Down
41 changes: 38 additions & 3 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub trait JsonRender {
}

pub trait JsonTruthy {
fn is_truthy(&self) -> bool;
fn is_truthy(&self, include_zero: bool) -> bool;
}

impl JsonRender for Json {
Expand Down Expand Up @@ -110,10 +110,17 @@ pub fn as_string(src: &Json) -> Option<&str> {
}

impl JsonTruthy for Json {
fn is_truthy(&self) -> bool {
fn is_truthy(&self, include_zero: bool) -> bool {
match *self {
Json::Bool(ref i) => *i,
Json::Number(ref n) => n.as_f64().map(|f| f.is_normal()).unwrap_or(false),
Json::Number(ref n) => {
if include_zero {
n.as_f64().map(|f| !f.is_nan()).unwrap_or(false)
} else {
// there is no inifity in json/serde_json
n.as_f64().map(|f| f.is_normal()).unwrap_or(false)
}
}
Json::Null => false,
Json::String(ref i) => !i.is_empty(),
Json::Array(ref i) => !i.is_empty(),
Expand All @@ -129,3 +136,31 @@ fn test_json_render() {

assert_eq!(raw, thing.render());
}

#[test]
fn test_json_number_truthy() {
assert!(json!(16i16).is_truthy(false));
assert!(json!(16i16).is_truthy(true));

assert!(json!(0i16).is_truthy(true));
assert!(!json!(0i16).is_truthy(false));

assert!(json!(1.0f64).is_truthy(false));
assert!(json!(1.0f64).is_truthy(true));

assert!(json!(Some(16i16)).is_truthy(false));
assert!(json!(Some(16i16)).is_truthy(true));

assert!(!json!(None as Option<i16>).is_truthy(false));
assert!(!json!(None as Option<i16>).is_truthy(true));

assert!(!json!(std::f64::NAN).is_truthy(false));
assert!(!json!(std::f64::NAN).is_truthy(true));

// there is no infinity in json/serde_json
// assert!(json!(std::f64::INFINITY).is_truthy(false));
// assert!(json!(std::f64::INFINITY).is_truthy(true));

// assert!(json!(std::f64::NEG_INFINITY).is_truthy(false));
// assert!(json!(std::f64::NEG_INFINITY).is_truthy(true));
}

0 comments on commit cec5ec9

Please sign in to comment.