Skip to content

Commit

Permalink
fix: comparison for empty/nil, fixes #321
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Mar 13, 2021
1 parent aff9976 commit 99d14e7
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 11 deletions.
4 changes: 3 additions & 1 deletion src/drop/empty-drop.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Drop } from './drop'
import { Comparable } from './comparable'
import { isObject, isString, isArray } from '../util/underscore'
import { isObject, isString, isArray, toValue } from '../util/underscore'

export class EmptyDrop extends Drop implements Comparable {
public equals (value: any) {
if (value instanceof EmptyDrop) return false
value = toValue(value)
if (isString(value) || isArray(value)) return value.length === 0
if (isObject(value)) return Object.keys(value).length === 0
return false
Expand Down
3 changes: 1 addition & 2 deletions src/drop/null-drop.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Drop } from './drop'
import { Comparable } from './comparable'
import { isNil, toValue } from '../util/underscore'
import { BlankDrop } from '../drop/blank-drop'

export class NullDrop extends Drop implements Comparable {
public equals (value: any) {
return isNil(toValue(value)) || value instanceof BlankDrop
return isNil(toValue(value))
}
public gt () {
return false
Expand Down
5 changes: 3 additions & 2 deletions src/util/literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { NullDrop } from '../drop/null-drop'
import { EmptyDrop } from '../drop/empty-drop'
import { BlankDrop } from '../drop/blank-drop'

const nil = new NullDrop()
export const literalValues = {
'true': true,
'false': false,
'nil': new NullDrop(),
'null': new NullDrop(),
'nil': nil,
'null': nil,
'empty': new EmptyDrop(),
'blank': new BlankDrop()
}
8 changes: 8 additions & 0 deletions test/e2e/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ describe('Issues', function () {
const html = await engine.parseAndRender(`{{ name | default: "default name" }}`)
expect(html).to.equal('default name')
})
it('#321 comparison for empty/nil', async () => {
const engine = new Liquid()
const html = await engine.parseAndRender(
'{% if empty == nil %}true{%else%}false{%endif%}' +
'{% if nil == empty %}true{%else%}false{%endif%}'
)
expect(html).to.equal('falsefalse')
})
})
10 changes: 10 additions & 0 deletions test/integration/drop/empty-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,14 @@ describe('drop/empty-drop', function () {
const html = await liquid.parseAndRender(src)
expect(html).to.equal('true')
})
it('empty != empty', async function () {
const src = '{%if empty == empty %}true{%else%}false{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('false')
})
it('empty != nil', async function () {
const src = '{%if empty == nil %}true{%else%}false{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('false')
})
})
17 changes: 11 additions & 6 deletions test/integration/drop/null-drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,27 @@ describe('drop/null-drop', function () {
const html = await liquid.parseAndRender('{{null}}')
expect(html).to.equal('')
})
it('undefined variable should equal to null', async function () {
it('undefined == null', async function () {
const src = '{%if foo == nil %}foo == nil{%else%}foo != nil{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('foo == nil')
})
it('nil equals blank', async function () {
const src = '{%if nil == blank %}nil == blank{%else%}nil != blank{% endif %}'
it('nil != blank', async function () {
const src = '{%if nil == blank %}eq{%else%}neq{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('nil == blank')
expect(html).to.equal('neq')
})
it('0 should not equal to null', async function () {
it('nil != empty', async function () {
const src = '{%if nil == empty %}eq{%else%}neq{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('neq')
})
it('0 != null', async function () {
const src = '{%if 0 == null %}0 == null{%else%}0 != null{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('0 != null')
})
it('nil should equal to null', async function () {
it('nil == null', async function () {
const src = '{%if nil == null %}nil == null{%else%}nil != null{% endif %}'
const html = await liquid.parseAndRender(src)
expect(html).to.equal('nil == null')
Expand Down

0 comments on commit 99d14e7

Please sign in to comment.