mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-26 13:45:15 -07:00
use isTruthy for and/or operators, fixes #47
This commit is contained in:
+16
-16
@@ -1,17 +1,17 @@
|
|||||||
var operators = {
|
module.exports = function (isTruthy) {
|
||||||
'==': (l, r) => l === r,
|
return {
|
||||||
'!=': (l, r) => l !== r,
|
'==': (l, r) => l === r,
|
||||||
'>': (l, r) => l !== null && r !== null && l > r,
|
'!=': (l, r) => l !== r,
|
||||||
'<': (l, r) => l !== null && r !== null && l < r,
|
'>': (l, r) => l !== null && r !== null && l > r,
|
||||||
'>=': (l, r) => l !== null && r !== null && l >= r,
|
'<': (l, r) => l !== null && r !== null && l < r,
|
||||||
'<=': (l, r) => l !== null && r !== null && l <= r,
|
'>=': (l, r) => l !== null && r !== null && l >= r,
|
||||||
'contains': (l, r) => {
|
'<=': (l, r) => l !== null && r !== null && l <= r,
|
||||||
if (!l) return false
|
'contains': (l, r) => {
|
||||||
if (typeof l.indexOf !== 'function') return false
|
if (!l) return false
|
||||||
return l.indexOf(r) > -1
|
if (typeof l.indexOf !== 'function') return false
|
||||||
},
|
return l.indexOf(r) > -1
|
||||||
'and': (l, r) => l && r,
|
},
|
||||||
'or': (l, r) => l || r
|
'and': (l, r) => isTruthy(l) && isTruthy(r),
|
||||||
|
'or': (l, r) => isTruthy(l) || isTruthy(r)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = operators
|
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
const operators = require('./operators.js')
|
const operators = require('./operators.js')(isTruthy)
|
||||||
const lexical = require('./lexical.js')
|
const lexical = require('./lexical.js')
|
||||||
const assert = require('../src/util/assert.js')
|
const assert = require('../src/util/assert.js')
|
||||||
|
|
||||||
|
|||||||
+3
-7
@@ -70,13 +70,9 @@ describe('liquid', function () {
|
|||||||
it('should render template multiple times', function () {
|
it('should render template multiple times', function () {
|
||||||
var template = engine.parse('{{obj}}')
|
var template = engine.parse('{{obj}}')
|
||||||
return engine.render(template, ctx)
|
return engine.render(template, ctx)
|
||||||
.then((result) => {
|
.then(result => expect(result).to.equal('{"foo":"bar"}'))
|
||||||
expect(result).to.equal('{"foo":"bar"}')
|
.then(() => engine.render(template, ctx))
|
||||||
return engine.render(template, ctx)
|
.then((result) => expect(result).to.equal('{"foo":"bar"}'))
|
||||||
})
|
|
||||||
.then((result) => {
|
|
||||||
return expect(result).to.equal('{"foo":"bar"}')
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
it('should render filters', function () {
|
it('should render filters', function () {
|
||||||
var template = engine.parse('<p>{{arr | join: "_"}}</p>')
|
var template = engine.parse('<p>{{arr | join: "_"}}</p>')
|
||||||
|
|||||||
+14
-4
@@ -14,6 +14,7 @@ describe('expression', function () {
|
|||||||
scope = Scope.factory({
|
scope = Scope.factory({
|
||||||
one: 1,
|
one: 1,
|
||||||
two: 2,
|
two: 2,
|
||||||
|
empty: '',
|
||||||
x: 'XXX',
|
x: 'XXX',
|
||||||
y: undefined,
|
y: undefined,
|
||||||
z: null
|
z: null
|
||||||
@@ -73,10 +74,19 @@ describe('expression', function () {
|
|||||||
expect(evalExp('"<=" == "<="', scope)).to.equal(true)
|
expect(evalExp('"<=" == "<="', scope)).to.equal(true)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should eval complex expression', function () {
|
describe('complex expression', function () {
|
||||||
expect(evalExp('1<2 and x contains "x"', scope)).to.equal(false)
|
it('should support value or value', function () {
|
||||||
expect(evalExp('1<2 or x contains "x"', scope)).to.equal(true)
|
expect(evalExp('false or true', scope)).to.equal(true)
|
||||||
expect(evalExp('false or true', scope)).to.equal(true)
|
})
|
||||||
|
it('should support < and contains', function () {
|
||||||
|
expect(evalExp('1<2 and x contains "x"', scope)).to.equal(false)
|
||||||
|
})
|
||||||
|
it('should support < or contains', function () {
|
||||||
|
expect(evalExp('1<2 or x contains "x"', scope)).to.equal(true)
|
||||||
|
})
|
||||||
|
it('should support value and !=', function () {
|
||||||
|
expect(evalExp('empty and empty != ""', scope)).to.equal(false)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should eval range expression', function () {
|
it('should eval range expression', function () {
|
||||||
|
|||||||
+80
-68
@@ -17,87 +17,99 @@ describe('tags/if', function () {
|
|||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.be.rejectedWith(/tag {% if false%} not closed/)
|
.to.be.rejectedWith(/tag {% if false%} not closed/)
|
||||||
})
|
})
|
||||||
it('should treat Array truthy', function () {
|
|
||||||
var src = '{%if emptyArray%}a{%endif%}'
|
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
|
||||||
.to.eventually.equal('a')
|
|
||||||
})
|
|
||||||
it('should support ==', function () {
|
|
||||||
var src = '{% if 2==3 %}yes{%else%}no{%endif%}'
|
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
|
||||||
.to.eventually.equal('no')
|
|
||||||
})
|
|
||||||
it('should support >=', function () {
|
|
||||||
var src = '{% if 1>=2 and one<two %}a{%endif%}'
|
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
|
||||||
.to.eventually.equal('')
|
|
||||||
})
|
|
||||||
it('should support !=', function () {
|
|
||||||
var src = '{% if one!=two %}yes{%else%}no{%endif%}'
|
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
|
||||||
.to.eventually.equal('yes')
|
|
||||||
})
|
|
||||||
it('should support boolean', function () {
|
|
||||||
var src = '{% if false %}1{%elsif true%}2{%else%}3{%endif%}'
|
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
|
||||||
.to.eventually.equal('2')
|
|
||||||
})
|
|
||||||
it('should support nested', function () {
|
it('should support nested', function () {
|
||||||
var src = '{%if false%}{%if true%}{%else%}a{%endif%}{%endif%}'
|
var src = '{%if false%}{%if true%}{%else%}a{%endif%}{%endif%}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('')
|
.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')
|
|
||||||
})
|
|
||||||
|
|
||||||
it('should return else when comparison on null 1', function () {
|
describe('single value as condition', function () {
|
||||||
var src = '{% if null < 10 %}yes{% else %}no{% endif %}'
|
it('should support boolean', function () {
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
var src = '{% if false %}1{%elsif true%}2{%else%}3{%endif%}'
|
||||||
.to.eventually.equal('no')
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('2')
|
||||||
|
})
|
||||||
|
it('should treat Array truthy', function () {
|
||||||
|
var src = '{%if emptyArray%}a{%endif%}'
|
||||||
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('a')
|
||||||
|
})
|
||||||
|
it('should return true if empty string', function () {
|
||||||
|
var src = '{%if emptyString%}a{%endif%}'
|
||||||
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('a')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
describe('expression as condition', function () {
|
||||||
it('should return else when comparison on null 2', function () {
|
it('should support ==', function () {
|
||||||
var src = '{% if null <= 10 %}yes{% else %}no{% endif %}'
|
var src = '{% if 2==3 %}yes{%else%}no{%endif%}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('no')
|
.to.eventually.equal('no')
|
||||||
|
})
|
||||||
|
it('should support >=', function () {
|
||||||
|
var src = '{% if 1>=2 and one<two %}a{%endif%}'
|
||||||
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('')
|
||||||
|
})
|
||||||
|
it('should support !=', function () {
|
||||||
|
var src = '{% if one!=two %}yes{%else%}no{%endif%}'
|
||||||
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('yes')
|
||||||
|
})
|
||||||
|
it('should support value and expression', function () {
|
||||||
|
var src = `X{%if version and version != '' %}x{{version}}y{%endif%}Y`
|
||||||
|
var ctx = { 'version': '' }
|
||||||
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('XY')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
describe('comparasion to null', function () {
|
||||||
|
it('should evaluate false for null < 10', function () {
|
||||||
|
var src = '{% if null < 10 %}yes{% else %}no{% endif %}'
|
||||||
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('no')
|
||||||
|
})
|
||||||
|
|
||||||
it('should return else when comparison on null 3', function () {
|
it('should evaluate false for null > 10', function () {
|
||||||
var src = '{% if null >= 10 %}yes{% else %}no{% endif %}'
|
var src = '{% if null > 10 %}yes{% else %}no{% endif %}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('no')
|
.to.eventually.equal('no')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return else when comparison on null 4', function () {
|
it('should evaluate false for null <= 10', function () {
|
||||||
var src = '{% if null > 10 %}yes{% else %}no{% endif %}'
|
var src = '{% if null <= 10 %}yes{% else %}no{% endif %}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('no')
|
.to.eventually.equal('no')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return else when comparison on null 5', function () {
|
it('should evaluate false for null >= 10', function () {
|
||||||
var src = '{% if 10 < null %}yes{% else %}no{% endif %}'
|
var src = '{% if null >= 10 %}yes{% else %}no{% endif %}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('no')
|
.to.eventually.equal('no')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return else when comparison on null 6', function () {
|
it('should evaluate false for 10 < null', function () {
|
||||||
var src = '{% if 10 <= null %}yes{% else %}no{% endif %}'
|
var src = '{% if 10 < null %}yes{% else %}no{% endif %}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('no')
|
.to.eventually.equal('no')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return else when comparison on null 7', function () {
|
it('should evaluate false for 10 > null', function () {
|
||||||
var src = '{% if 10 >= null %}yes{% else %}no{% endif %}'
|
var src = '{% if 10 > null %}yes{% else %}no{% endif %}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('no')
|
.to.eventually.equal('no')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should return else when comparison on null 8', function () {
|
it('should evaluate false for 10 <= null', function () {
|
||||||
var src = '{% if 10 > null %}yes{% else %}no{% endif %}'
|
var src = '{% if 10 <= null %}yes{% else %}no{% endif %}'
|
||||||
return expect(liquid.parseAndRender(src, ctx))
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
.to.eventually.equal('no')
|
.to.eventually.equal('no')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should evaluate false for 10 >= null', function () {
|
||||||
|
var src = '{% if 10 >= null %}yes{% else %}no{% endif %}'
|
||||||
|
return expect(liquid.parseAndRender(src, ctx))
|
||||||
|
.to.eventually.equal('no')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user