mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix: expression support Drop.valueOf, fixes #522
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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')
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user