Permalink
Browse files

Merge pull request #44 from iamleppert/select-helper-t

Select helper and unit tests, fixes #16
  • Loading branch information...
2 parents cefac70 + 89bdd6c commit c90b9c8729f02d91060c694e881dc58fec228c54 @jleppert jleppert committed May 30, 2012
Showing with 339 additions and 1 deletion.
  1. +76 −1 lib/dust-helpers.js
  2. +138 −0 test/examples.js
  3. +125 −0 test/jasmine-test/spec/examples.js
View
@@ -1,5 +1,52 @@
(function(dust){
+
+function isSelect(context) {
+ var value = context.current();
+ return typeof value === "object" && value.isSelect === true;
+};
+
+function filter(chunk, context, bodies, params, filter) {
+ var params = params || {},
+ actual, expected;
+ if (params.key) {
+ actual = context.get(params.key);
+ } else if (isSelect(context)) {
+ actual = context.current().value;
+ if (context.current().isResolved) {
+ filter = function() { return false; };
+ }
+ } else {
+ throw "No key specified for filter and no key found in context from select statement";
+ }
+
+ expected = params.value;
+ if (filter(expected, coerce(actual, params.type, context))) {
+ if (isSelect(context)) {
+ context.current().isResolved = true;
+ }
+ return chunk.render(bodies.block, context);
+ } else if (bodies['else']) {
+ return chunk.render(bodies['else'], context);
+ }
+
+ return chunk.write('');
+};
+
+function coerce (value, type, context) {
+ if (value) {
+ switch (type || typeof(value)) {
+ case 'number': return +value;
+ case 'string': return String(value);
+ case 'boolean': return Boolean(value);
+ case 'date': return new Date(value);
+ case 'context': return context.get(value);
+ }
+ }
+
+ return value;
+};
+
var helpers = {
sep: function(chunk, context, bodies) {
@@ -43,9 +90,37 @@ var helpers = {
}
}
return chunk;
+ },
+
+ select: function(chunk, context, bodies, params) {
+ return chunk.render(bodies.block, context.push({ isSelect: true, isResolved: false, value: context.get(params.key) }));
+ },
+
+ eq: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual === expected; });
+ },
+
+ lt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual < expected; });
+ },
+
+ lte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual <= expected; });
+ },
+
+ gt: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual > expected; });
+ },
+
+ gte: function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return actual >= expected; });
+ },
+
+ "else": function(chunk, context, bodies, params) {
+ return filter(chunk, context, bodies, params, function(expected, actual) { return true; });
}
};
dust.helpers = helpers;
-})(typeof exports !== 'undefined' ? exports : getGlobal());
+})(typeof exports !== 'undefined' ? exports : getGlobal());
View
@@ -344,7 +344,145 @@ exports.dustExamples = [
}
},
expected: "3.14159"
+ },
+ {
+ name: "select helper: one condition - eq",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=10}foobar{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - lt",
+ source: ["{@select key=\"foo\"}",
+ "{@lt value=20}foobar{/lt}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - lte",
+ source: ["{@select key=\"foo\"}",
+ "{@lte value=10}foobar{/lte}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - lte",
+ source: ["{@select key=\"foo\"}",
+ "{@lte value=11}foobar{/lte}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - gt",
+ source: ["{@select key=\"foo\"}",
+ "{@gt value=5}foobar{/gt}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - gte",
+ source: ["{@select key=\"foo\"}",
+ "{@gte value=10}foobar{/gte}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - gte",
+ source: ["{@select key=\"foo\"}",
+ "{@gte value=5}foobar{/gte}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - number",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=10}foobar{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: 10
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: one condition - string",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: "bar"
+ },
+ expected: "foobar"
+ },
+ {
+ name: "select helper: two conditions",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{@eq value=\"baz\"}foobaz{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: "baz"
+ },
+ expected: "foobaz"
+ },
+ {
+ name: "select helper: three conditions with else",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{@eq value=\"baz\"}foobaz{/eq}",
+ "{@eq value=\"foobar\"}foofoobar{/eq}",
+ "{@else value=\"foo\"}foofoo{/else}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: "foo"
+ },
+ expected: "foofoo"
+ },
+ {
+ name: "select helper: no matching conditions",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{@eq value=\"baz\"}foobaz{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: {
+ foo: "foo"
+ },
+ expected: ""
}
];
+
})(typeof exports !== "undefined" ? exports : window);
@@ -520,6 +520,131 @@ var dustExamples = [
context: { obj: JSON.stringify({ id: 1, name: "bob", occupation: "construction" }) },
expected: JSON.parse(JSON.stringify({ id: 1, name: "bob", occupation: "construction" })).toString(),
message: "should objectify a JSON string when using the jp filter"
+ },
+ {
+ name: "select helper: one condition - eq",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=10}foobar{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition (eq)"
+ },
+ {
+ name: "select helper: one condition - lt",
+ source: ["{@select key=\"foo\"}",
+ "{@lt value=20}foobar{/lt}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition (lt)"
+ },
+ {
+ name: "select helper: one condition - lte",
+ source: ["{@select key=\"foo\"}",
+ "{@lte value=10}foobar{/lte}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition (lte)"
+ },
+ {
+ name: "select helper: one condition - lte",
+ source: ["{@select key=\"foo\"}",
+ "{@lte value=11}foobar{/lte}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition (lte)"
+ },
+ {
+ name: "select helper: one condition - gt",
+ source: ["{@select key=\"foo\"}",
+ "{@gt value=5}foobar{/gt}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition (gt)"
+ },
+ {
+ name: "select helper: one condition - gte",
+ source: ["{@select key=\"foo\"}",
+ "{@gte value=10}foobar{/gte}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition (gte)"
+ },
+ {
+ name: "select helper: one condition - gte",
+ source: ["{@select key=\"foo\"}",
+ "{@gte value=5}foobar{/gte}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition (gte)"
+ },
+ {
+ name: "select helper: one condition - number",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=10}foobar{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: 10 },
+ expected: "foobar",
+ message: "Select helper works with one condition of type number (eq)"
+ },
+ {
+ name: "select helper: one condition - string",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: "bar" },
+ expected: "foobar",
+ message: "Select helper works with one condition of type string (eq)"
+ },
+ {
+ name: "select helper: two conditions",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{@eq value=\"baz\"}foobaz{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: "baz" },
+ expected: "foobaz",
+ message: "Select helper works with two conditions"
+ },
+ {
+ name: "select helper: three conditions with else",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{@eq value=\"baz\"}foobaz{/eq}",
+ "{@eq value=\"foobar\"}foofoobar{/eq}",
+ "{@else value=\"foo\"}foofoo{/else}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: "foo" },
+ expected: "foofoo",
+ message: "Select helper works with three conditions with else"
+ },
+ {
+ name: "select helper: no matching conditions",
+ source: ["{@select key=\"foo\"}",
+ "{@eq value=\"bar\"}foobar{/eq}",
+ "{@eq value=\"baz\"}foobaz{/eq}",
+ "{/select}"
+ ].join("\n"),
+ context: { foo: "foo" },
+ expected: "",
+ message: "Select helper works correctly with no matching conditions"
}
];

0 comments on commit c90b9c8

Please sign in to comment.