Skip to content

Commit

Permalink
fix: expression support Drop.valueOf, fixes #522
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Jul 20, 2022
1 parent 66eb3b8 commit 4ad383d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/render/operator.ts
Expand Up @@ -11,38 +11,38 @@ export const defaultOperators: Operators = {
'==': (l: any, r: any) => {
if (isComparable(l)) return l.equals(r)
if (isComparable(r)) return r.equals(l)
return l === r
return toValue(l) === toValue(r)
},
'!=': (l: any, r: any) => {
if (isComparable(l)) return !l.equals(r)
if (isComparable(r)) return !r.equals(l)
return l !== r
return toValue(l) !== toValue(r)
},
'>': (l: any, r: any) => {
if (isComparable(l)) return l.gt(r)
if (isComparable(r)) return r.lt(l)
return l > r
return toValue(l) > toValue(r)
},
'<': (l: any, r: any) => {
if (isComparable(l)) return l.lt(r)
if (isComparable(r)) return r.gt(l)
return l < r
return toValue(l) < toValue(r)
},
'>=': (l: any, r: any) => {
if (isComparable(l)) return l.geq(r)
if (isComparable(r)) return r.leq(l)
return l >= r
return toValue(l) >= toValue(r)
},
'<=': (l: any, r: any) => {
if (isComparable(l)) return l.leq(r)
if (isComparable(r)) return r.geq(l)
return l <= r
return toValue(l) <= toValue(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),
'or': (l: any, r: any, ctx: Context) => isTruthy(l, ctx) || isTruthy(r, ctx)
'and': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) && isTruthy(toValue(r), ctx),
'or': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) || isTruthy(toValue(r), ctx)
}
12 changes: 12 additions & 0 deletions test/integration/drop/drop.ts
Expand Up @@ -72,4 +72,16 @@ describe('drop/drop', function () {
const html = await liquid.parseAndRender(tpl, { drop: new CustomDrop() })
expect(html).to.equal('foobar: foo;bar;')
})
it('should support valueOf in == expression', async () => {
class AddressDrop extends Drop {
valueOf () {
return 'test'
}
}
const address = new AddressDrop()
const customer = { default_address: new AddressDrop() }
const tpl = `{% if address == customer.default_address %}{{address}}{% endif %}`
const html = await liquid.parseAndRender(tpl, { address, customer })
expect(html).to.equal('test')
})
})

0 comments on commit 4ad383d

Please sign in to comment.