Skip to content

Commit

Permalink
{@SiZe} resolves Dust bodies and outputs their size
Browse files Browse the repository at this point in the history
Closes #46
  • Loading branch information
Seth Kinast committed Apr 17, 2015
1 parent ccc2522 commit ccaa1af
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
43 changes: 22 additions & 21 deletions lib/dust-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,35 +393,36 @@ var helpers = {
},

/**
* size helper prints the size of the given key
* Note : size helper is self closing and does not support bodies
* @param key, the element whose size is returned
* {@size}
* Write the size of the target to the chunk
* Falsy values and true have size 0
* Numbers are returned as-is
* Arrays and Strings have size equal to their length
* Objects have size equal to the number of keys they contain
* Dust bodies are evaluated and the length of the string is returned
* Functions are evaluated and the length of their return value is evaluated
* @param key find the size of this value or reference
*/
"size": function( chunk, context, bodies, params ) {
var key, value=0, nr, k;
params = params || {};
key = params.key;
if (!key || key === true) { //undefined, null, "", 0
"size": function(chunk, context, bodies, params) {
var key = params.key,
value, k;

key = context.resolve(params.key);
if (!key || key === true) {
value = 0;
}
else if(dust.isArray(key)) { //array
} else if(dust.isArray(key)) {
value = key.length;
}
else if (!isNaN(parseFloat(key)) && isFinite(key)) { //numeric values
} else if (!isNaN(parseFloat(key)) && isFinite(key)) {
value = key;
}
else if (typeof key === "object") { //object test
//objects, null and array all have typeof ojbect...
//null and array are already tested so typeof is sufficient http://jsperf.com/isobject-tests
nr = 0;
} else if (typeof key === "object") {
value = 0;
for(k in key){
if(Object.hasOwnProperty.call(key,k)){
nr++;
if(key.hasOwnProperty(k)){
value++;
}
}
value = nr;
} else {
value = (key + '').length; //any other value (strings etc.)
value = (key + '').length;
}
return chunk.write(value);
}
Expand Down
56 changes: 28 additions & 28 deletions test/jasmine-test/spec/helpersTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,105 +1209,105 @@
source: 'you have {@size key=list}{body}{/size} new messages',
context: { list: [ 'msg1', 'msg2', 'msg3' ], "body" : "body block" },
expected: "you have 3 new messages",
message: "should test size helper not supporting body"
message: "should test {@size} skips its body"
},
{
name: "size helper 3 items",
source: 'you have {@size key=list/} new messages',
context: { list: [ 'msg1', 'msg2', 'msg3' ] },
expected: "you have 3 new messages",
message: "should test if size helper is working properly with array"
message: "should test {@size} with an array"
},
{
name: "size helper string",
source: "'{mystring}' has {@size key=mystring/} letters",
context: { mystring: 'hello' },
expected: "'hello' has 5 letters",
message: "should test if size helper is working properly with strings"
message: "should test {@size} with a string"
},
{
name: "size helper string (empty)",
source: "'{mystring}' has {@size key=mystring/} letters",
context: { mystring: '' },
expected: "'' has 0 letters",
message: "should test if size helper is working properly with strings"
message: "should test {@size} with an empty string"
},
{
name: "size helper for newline",
source: "{@size key=mystring/} letters",
context: { mystring: '\n' },
expected: "1 letters",
message: "should test if size is working for newline"
},
{
name: "size helper string with newline",
source: "{@size key=mystring/} letters",
context: { mystring: 'test\n' },
expected: "5 letters",
message: "should test if size for string with newline"
},
{
name: "size helper string with newline, tab, carriage return and bakspace",
name: "size helper string with newline, tab, carriage return and backspace",
source: "{@size key=mystring/} letters",
context: { mystring: 'test\n\t\r\b' },
expected: "8 letters",
message: "should test if size helper is working for string with newline, tab, carriage return and bakspace"
message: "should test {@size} with character literals in a string"
},
{
name: "size helper number",
source: 'you have {@size key=mynumber/} new messages',
context: { mynumber: 0 },
expected: "you have 0 new messages",
message: "should test if size helper is working properly with numeric 0"
message: "should test {@size} with 0"
},
{
name: "size helper number",
source: 'you have {@size key=mynumber/} new messages',
context: { mynumber: 10 },
expected: "you have 10 new messages",
message: "should test if size helper is working properly with numeric 10"
message: "should test {@size} with an int"
},
{
name: "size helper floating numeric",
source: 'you have {@size key=mynumber/} new messages',
context: { mynumber: 0.4 },
expected: "you have 0.4 new messages",
message: "should test if size helper is working properly with floating numeric"
message: "should test {@size} with a float"
},
{
name: "size helper with boolean false",
source: 'you have {@size key=myboolean/} new messages',
context: { myboolean: false },
expected: "you have 0 new messages",
message: "should test if size helper is working properly with boolean false"
message: "should test {@size} with false"
},
{
name: "size helper with boolean true",
source: 'you have {@size key=myboolean/} new messages',
context: { myboolean: true },
expected: "you have 0 new messages",
message: "should test if size helper is working properly with boolean true"
message: "should test {@size} with true"
},
{
name: "size helper with object",
source: 'you have {@size key=myValue/} new messages',
context: { myValue: { foo:'bar', baz:'bax' } },
expected: "you have 2 new messages",
message: "should test if size helper is working properly when the value is an object "
message: "should test {@size} with an Object"
},
{
name: "size helper with object",
source: 'you have {@size key=myValue/} new messages',
context: { myValue: {} },
expected: "you have 0 new messages",
message: "should test if size helper is working properly when the value is an object that is zero"
message: "should test {@size} with an empty Object"
},
{
name: "size helper value not set",
source: 'you have {@size key=myNumber/} new messages',
context: {},
expected: "you have 0 new messages",
message: "should test if size helper is working properly when the value is not present in context"
message: "should test {@size} with an undefined value"
},
{
name: "size helper function",
source: 'you have {@size key=func/} new messages',
context: { func: function() { return 4; } },
expected: "you have 4 new messages",
message: "should test {@size} with a function"
},
{
name: "size body function",
source: '"hello" has {@size key="{func}"/} letters',
context: { func: function() { return 'hello'; } },
expected: '"hello" has 5 letters',
message: "should test {@size} with a Dust body"
}
]
},
Expand Down

0 comments on commit ccaa1af

Please sign in to comment.