-
Notifications
You must be signed in to change notification settings - Fork 479
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
"falsey" conditional {?.x} is not treating 0 as false #180
Comments
https://github.com/linkedin/dustjs/blob/master/dist/dust-core-1.1.1.js#L448 That's the code which does it ^ Edit: no it's not - sorry Inclined to think that it's incorrect. Any cons to it treating 0 as false? |
Chunk.prototype.exists = function(elem, context, bodies) { if (!dust.isEmpty(elem)) { it is the isEmpty method. dust.isEmpty = function(value) { |
it is the .notation that trips it, else it works fine for me ( so please update your the problem the same code example as before. |
I'm getting exactly the same result with and without the dot before "key", so stripped it out to simplify as much as possible. Will re-add anyway. |
if (value === 0) return false; this is the code ( hence it returns false for isEmpty ) Change this line of code, lot of unit tests fail ! |
Therefore, it is not a "falsy" conditional as stated in the docs. |
Good point. that if you try {#key} , it treats it falsey I bet? |
Doesn't seem to. Seems to be the same.
again returns
Same with dots. |
ok, that makes me a little happy ! , atleast it is consistent. Should we just update the doc? I did spend some time with unit tests for these, let me dig around a bit later today. |
Hmm... the problem here is that this makes it impossible to use conditionals based on zero values. The only way to make it evaluate as false is to remove the value altogether. In languages where you are directly converting a static type to JSON (like C#), this is rather difficult to do. For example: { id: 1, rating: 2 }
{ id: 2, rating: 0 } Say your app uses 0 to represent a lack of rating. The only way to make dust understand this is: { id: 1, rating: 2 }
{ id: 2 } But when your model is this: class Entity
{
int id {get;set;}
int rating {get;set;}
} You can't just remove one of the properties. It's not feasible. Therefore, I think it's important to allow for checking for zero values. |
That said, arguably, a nullable int would be more appropriate. Though I think there are still cases where conditionals around 0 values would be useful. |
here are two other special section notations that provide conditional testing: {?name/} |
wiki has the details already, hence closing :https://github.com/linkedin/dustjs/wiki/Dust-Tutorial#wiki-Logic_in_Templates |
"numeric 0 evaluates to true" <-- I do not think this is intuitive. What makes this hurt even more is that there are no keywords for true/false that we could pass as parameters - meaning that the only universal falsy value (other than non-existence/undefined) is "" (empty string). |
As per #127 (comment), heavily simplified
Using the tool with this for the template:
And this for the JSON:
Output I'm getting:
Output I'm expecting to get:
Occurring the same in both of the following tools:
http://akdubya.github.com/dustjs/
http://linkedin.github.com/dustjs/test/test.html
For reference, I have tested in Safari on OS X and Chrome on Windows.
The following "falsey" values give the correct response - b:
The integer value 0 (in Javascript) is also considered as "falsey", as per the following:
Whether intentional or not, I feel this is a bug because zero is a "falsey" value.
The text was updated successfully, but these errors were encountered: