Comments (5)
{@eq value="1"} - value is a constant
{@eq value={one}} - not valid grammar
{@eq value="{one}"} - added tests ( seems to work ) but may be not all cases
{@eq value=one} - this has unit tests too. ( but need to validate it works in all cases )
from dustjs-helpers.
Here are the test cases I was looking at
--- a/test/jasmine-test/spec/helpersTests.js
+++ b/test/jasmine-test/spec/helpersTests.js
@@ -189,6 +189,20 @@ var helpersTests = [
message: "eq helper matching string case"
},
{
+ name: "eq helper for boolean true value",
+ source: "{@eq key=\"{foo}\" type=\"boolean\" value=\"false\"}true is false {:else} true is not false{/eq}",
+ context: {foo: true},
+ expected: "true is not false",
+ message: "eq helper for boolean true value"
+ },
+ {
+ name: "eq helper for boolean false value",
+ source: "{@eq key=\"{foo}\" type=\"boolean\" value=\"false\"}false is false {:else} false is not false{/eq}",
+ context: {foo: false},
+ expected: "false is false",
+ message: "eq helper for boolean false value"
+ },
+ {
name: "eq helper non matching string case",
source: "{@eq key=\"foo\" value=\"bar\"}equal{:else}bar{/eq}",
context: {},
The coerce
function was modified to work better but seems specific to string inputs
function coerce (value, type, context) {
if (value) {
switch (type || typeof(value)) {
case 'number': return +value;
case 'string': return String(value);
case 'boolean': {
value = (value === 'false' ? false : value);
return Boolean(value);
}
case 'date': return new Date(value);
case 'context': return context.get(value);
}
}
return value;
}
Seems type coercing for things that are not strings are difficult because tap on references will return strings, whereas tap on numbers/boolean/scalars will just return it. We can't really tell if something will be a reference or not when we call tap.
from dustjs-helpers.
We have to fix this use case "We can't really tell if something will be a reference or not when we call tap."
from dustjs-helpers.
@jimmyhchan , here is the how the tap works ( same as chunk.render )
// Utility helping to resolve dust references in the given chunk
// uses the Chunk.render method to resolve value
/*
Reference resolution rules:
if value exists in JSON:
"" or '' will evaluate to false, boolean false, null, or undefined will evaluate to false,
numeric 0 evaluates to true, so does, string "0", string "null", string "undefined" and string "false".
Also note that empty array -> [] is evaluated to false and empty object -> {} and non-empty object are evaluated to true
if value does not exist in JSON and the input is a single reference: {x}
dust render emits empty string, and we then return false
if values does not exist in JSON and the input is a chain of references : {x} < {y}
dust render emits < and we return the partial output
*/
"tap": function( input, chunk, context ){
// return given input if there is no dust reference to resolve
var output = input;
// dust compiles a string/reference such as {foo} to function,
if( typeof input === "function"){
// just a plain function (a.k.a anonymous functions) in the context, not a dust body
function created by the dust compiler
if( input.isFunction === true ){
output = input();
} else {
output = '';
chunk.tap(function(data){
output += data;
return '';
}).render(input, context).untap();
if( output === '' ){
output = false;
}
}
}
return output;
},
from dustjs-helpers.
After talking to Jimmy, we identified the root cause is "tap" returns a string always ( thus modifying the type of the input passed in in many cases )
the reason it returns string : tap supports interpolated strings such as {A}_{B} and hence we need to coerce the final output to string.
from dustjs-helpers.
Related Issues (20)
- Testing sub boolean value HOT 2
- Drop Node 0.8 support
- breaking semver and my sites HOT 2
- Helpers 1.6.0 and dustjs-linkedin 2.6.0 can break some apps HOT 13
- @default and @none helpers not working properly on 1.6.0 version HOT 12
- Don't require a key for {@select} HOT 5
- {@select}s with multiple keys break when used with {@any} / {@none}
- Feature regression in 1.7.0, logic helpers ignored in select bodies HOT 9
- Cannot use 'else' as a property in IE8 HOT 1
- Sync test framework changes / saucelabs
- @select fails to render multiple @eq statements inside a @none block HOT 7
- Make use of context options to store @select state
- Add case-insensitive {@eq} comparator HOT 12
- Issue in running in production mode
- @eq helper doesn't work when accessing an object at a deeper level HOT 7
- Fix Travis tests by not supporting node 0.10 and node 0.12
- Support dustjs v3.0.0 HOT 4
- deprecate and remove if helper HOT 4
- Peer dependencies issue with dustjs-linkedin HOT 16
- Is there a reason why there is no loop helper? HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dustjs-helpers.