Skip to content

Commit

Permalink
fix: contains operator does not support Drop, fixes #492
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Apr 17, 2022
1 parent eec381e commit 9e024ff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/render/operator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isComparable } from '../drop/comparable'
import { Context } from '../context/context'
import { isFunction } from '../util/underscore'
import { isFunction, toValue } from '../util/underscore'
import { isTruthy } from '../render/boolean'

export interface Operators {
Expand Down Expand Up @@ -39,6 +39,8 @@ export const defaultOperators: Operators = {
return l <= r
},
'contains': (l: any, r: any) => {
l = toValue(l)
r = toValue(r)
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false
},
'and': (l: any, r: any, ctx: Context) => isTruthy(l, ctx) && isTruthy(r, ctx),
Expand Down
11 changes: 10 additions & 1 deletion test/e2e/issues.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Liquid } from '../..'
import { Liquid, Drop } from '../..'
import { expect, use } from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as sinon from 'sinon'
Expand Down Expand Up @@ -232,4 +232,13 @@ describe('Issues', function () {
const html = await engine.parseAndRender(`{% assign a = "x,y,z" | split: ',' -%}{{ a[-1] }} {{ a[-3] }} {{ a[-8] }}`)
expect(html).to.equal('z x ')
})
it('#492 contains operator does not support Drop', async () => {
class TemplateDrop extends Drop {
valueOf () { return 'product' }
}
const engine = new Liquid()
const ctx = { template: new TemplateDrop() }
const html = await engine.parseAndRender(`{% if template contains "product" %}contains{%endif%}`, ctx)
expect(html).to.equal('contains')
})
})
8 changes: 8 additions & 0 deletions test/unit/render/expression.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Tokenizer } from '../../../src/parser/tokenizer'
import { expect } from 'chai'
import { Drop } from '../../../src/drop/drop'
import { Context } from '../../../src/context/context'
import { toThenable } from '../../../src/util/async'
import { defaultOperators } from '../../../src/render/operator'
Expand Down Expand Up @@ -130,6 +131,13 @@ describe('Expression', function () {
it('should support < or contains', async function () {
expect(await toThenable(create('1 < 2 or x contains "x"').evaluate(ctx, false))).to.equal(true)
})
it('should support Drops for "x contains "x""', async () => {
class TemplateDrop extends Drop {
valueOf () { return 'X' }
}
const ctx = new Context({ x: 'XXX', X: new TemplateDrop() })
expect(await toThenable(create('x contains X').evaluate(ctx, false))).to.equal(true)
})
it('should support value and !=', async function () {
const ctx = new Context({ empty: '' })
expect(await toThenable(create('empty and empty != ""').evaluate(ctx, false))).to.equal(false)
Expand Down

0 comments on commit 9e024ff

Please sign in to comment.