mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
All values in Liquid are truthy except nil and false. (#26)
* All values in Liquid are truthy except nil and false. * The falsy values are null, undefined and false.
This commit is contained in:
+2
-3
@@ -43,12 +43,11 @@ function evalValue(str, scope) {
|
||||
}
|
||||
|
||||
function isTruthy(val) {
|
||||
if (val instanceof Array) return !!val.length;
|
||||
return !!val;
|
||||
return !isFalsy(val);
|
||||
}
|
||||
|
||||
function isFalsy(val) {
|
||||
return !isTruthy(val);
|
||||
return false === val || undefined === val || null === val;;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
+3
-1
@@ -36,7 +36,9 @@ module.exports = function(liquid) {
|
||||
|
||||
render: function(scope, hash) {
|
||||
var collection = Liquid.evalExp(this.collection, scope);
|
||||
if (Liquid.isFalsy(collection)) {
|
||||
|
||||
if (!Array.isArray(collection) ||
|
||||
(Array.isArray(collection) && collection.length === 0)) {
|
||||
return liquid.renderer.renderTemplates(this.elseTemplates, scope);
|
||||
}
|
||||
|
||||
|
||||
+9
-2
@@ -10,6 +10,7 @@ describe('tags/for', function() {
|
||||
ctx = {
|
||||
one: 1,
|
||||
alpha: ['a', 'b', 'c'],
|
||||
emptyArray: []
|
||||
};
|
||||
});
|
||||
it('should support for', function() {
|
||||
@@ -24,6 +25,12 @@ describe('tags/for', function() {
|
||||
.to.be.rejectedWith(/tag .* not closed/);
|
||||
});
|
||||
|
||||
it('should return else when for in empty array', function() {
|
||||
var src = '{%for c in emptyArray%}a{%else%}b{%endfor%}';
|
||||
return expect(liquid.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('b');
|
||||
});
|
||||
|
||||
it('should support for else', function() {
|
||||
var src = '{%for c in ""%}a{%else%}b{%endfor%}';
|
||||
return expect(liquid.parseAndRender(src, ctx))
|
||||
@@ -80,13 +87,13 @@ describe('tags/for', function() {
|
||||
.to.eventually.equal('21');
|
||||
});
|
||||
|
||||
it('should support for reversed in the middle position', function() {
|
||||
it('should support for reversed in the first position', function() {
|
||||
var src = '{% for i in (1..5) reversed limit:2 %}{{ i }}{% endfor %}';
|
||||
return expect(liquid.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('21');
|
||||
});
|
||||
|
||||
it('should support for reversed in the first position', function() {
|
||||
it('should support for reversed in the middle position', function() {
|
||||
var src = '{% for i in (1..5) offset:2 reversed limit:4 %}{{ i }}{% endfor %}';
|
||||
return expect(liquid.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('543');
|
||||
|
||||
+7
-1
@@ -8,6 +8,7 @@ describe('tags/if', function() {
|
||||
var ctx = {
|
||||
one: 1,
|
||||
two: 2,
|
||||
emptyString: '',
|
||||
emptyArray: []
|
||||
};
|
||||
|
||||
@@ -19,7 +20,7 @@ describe('tags/if', function() {
|
||||
it('should support if 2', function() {
|
||||
var src = '{%if emptyArray%}a{%endif%}';
|
||||
return expect(liquid.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('');
|
||||
.to.eventually.equal('a');
|
||||
});
|
||||
it('should support if 3', function() {
|
||||
var src = '{% if 2==3 %}yes{%else%}no{%endif%}';
|
||||
@@ -46,4 +47,9 @@ describe('tags/if', function() {
|
||||
return expect(liquid.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('');
|
||||
});
|
||||
it('should return true if empty string', function() {
|
||||
var src = "{%if emptyString%}a{%endif%}";
|
||||
return expect(liquid.parseAndRender(src, ctx))
|
||||
.to.eventually.equal('a');
|
||||
});
|
||||
});
|
||||
|
||||
+3
-2
@@ -7,12 +7,13 @@ describe('tags/unless', function() {
|
||||
var liquid = Liquid();
|
||||
|
||||
it('should render else when predicate yields true', function() {
|
||||
var src = '{% unless 1 %}yes{%else%}no{%endunless%}';
|
||||
// 0 is truthy
|
||||
var src = '{% unless 0 %}yes{%else%}no{%endunless%}';
|
||||
return expect(liquid.parseAndRender(src))
|
||||
.to.eventually.equal('no');
|
||||
});
|
||||
it('should render unless when predicate yields false', function() {
|
||||
var src = '{% unless 0 %}yes{%else%}no{%endunless%}';
|
||||
var src = '{% unless false %}yes{%else%}no{%endunless%}';
|
||||
return expect(liquid.parseAndRender(src))
|
||||
.to.eventually.equal('yes');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user