From 716a110d2e34a261d1266e11f6eafddcae163872 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 14 Jun 2020 14:13:33 -0400 Subject: [PATCH] Added some notes about observed behavior and fixed return types for missing/absent keys. --- js/test_cases/helpers_size/README.md | 9 +++++++++ js/test_cases/helpers_size/inputobject.json | 4 ++-- js/test_cases/helpers_size/main.dust | 2 +- src/renderer/renderer.rs | 7 ++++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/js/test_cases/helpers_size/README.md b/js/test_cases/helpers_size/README.md index d39036d..76b797a 100644 --- a/js/test_cases/helpers_size/README.md +++ b/js/test_cases/helpers_size/README.md @@ -7,3 +7,12 @@ Excerpt from [the DustJS tutorial](https://github.com/linkedin/dustjs/wiki/Dust- Undefined, 0, empty string - zero Any other value - length after conversion to string +This appears to be inaccurate (probably out of date because that tutorial was deprecated in favor of [dustjs.com](https://www.dustjs.com/), but the latter is incomplete and lacking any reference to the `@size` helper. + +Corrections +----------- +- Booleans are 0, not converted to strings + +Oddities +-------- +Reference parameters (like `foo="{bar}"`) are usually treated as strings but it seems if it contains ONLY a reference to a value and not anything else, then it is still treated as a number. diff --git a/js/test_cases/helpers_size/inputobject.json b/js/test_cases/helpers_size/inputobject.json index 4ead7ce..e511117 100644 --- a/js/test_cases/helpers_size/inputobject.json +++ b/js/test_cases/helpers_size/inputobject.json @@ -1,6 +1,6 @@ { "val": { - "pet": "cat", - "name": "fluffy" + "name": "fluffy", + "pet": "cat" } } diff --git a/js/test_cases/helpers_size/main.dust b/js/test_cases/helpers_size/main.dust index fab1f99..13720ff 100644 --- a/js/test_cases/helpers_size/main.dust +++ b/js/test_cases/helpers_size/main.dust @@ -1,3 +1,3 @@ The size of val ({val|js|s}) is {@size key=val /}{~n} -The size of "{val}" is {@size key="{val}" /}{~n} +The size of "{~lb}val{~rb}" is {@size key="{val}" /}{~n} The size with no key is {@size /}{~n} diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index b9b8c8e..6611a78 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -733,7 +733,12 @@ impl<'a> DustRenderer<'a> { .map(|ce| ce.get_size()) }); match value_ce { - None | Some(Err(_)) | Some(Ok(None)) => return Ok("".to_owned()), + // If we found the key but could not get the size from it, render nothing. + Some(Ok(None)) => return Ok("".to_owned()), + // If "key" is not on the @size tag at all, render 0. + None => return Ok("0".to_owned()), + // If the key value could not be found in the context, render 0. + Some(Err(_)) => return Ok("0".to_owned()), Some(Ok(Some(ce_size))) => { return ce_size.get_context_element_reference().render(&Vec::new()) }