fix: expression support Drop.valueOf, fixes #522

This commit is contained in:
Harttle
2022-07-21 01:21:58 +08:00
committed by Harttle
parent 66eb3b842a
commit 4ad383d9be
2 changed files with 20 additions and 8 deletions
+8 -8
View File
@@ -11,38 +11,38 @@ export const defaultOperators: Operators = {
'==': (l: any, r: any) => { '==': (l: any, r: any) => {
if (isComparable(l)) return l.equals(r) if (isComparable(l)) return l.equals(r)
if (isComparable(r)) return r.equals(l) if (isComparable(r)) return r.equals(l)
return l === r return toValue(l) === toValue(r)
}, },
'!=': (l: any, r: any) => { '!=': (l: any, r: any) => {
if (isComparable(l)) return !l.equals(r) if (isComparable(l)) return !l.equals(r)
if (isComparable(r)) return !r.equals(l) if (isComparable(r)) return !r.equals(l)
return l !== r return toValue(l) !== toValue(r)
}, },
'>': (l: any, r: any) => { '>': (l: any, r: any) => {
if (isComparable(l)) return l.gt(r) if (isComparable(l)) return l.gt(r)
if (isComparable(r)) return r.lt(l) if (isComparable(r)) return r.lt(l)
return l > r return toValue(l) > toValue(r)
}, },
'<': (l: any, r: any) => { '<': (l: any, r: any) => {
if (isComparable(l)) return l.lt(r) if (isComparable(l)) return l.lt(r)
if (isComparable(r)) return r.gt(l) if (isComparable(r)) return r.gt(l)
return l < r return toValue(l) < toValue(r)
}, },
'>=': (l: any, r: any) => { '>=': (l: any, r: any) => {
if (isComparable(l)) return l.geq(r) if (isComparable(l)) return l.geq(r)
if (isComparable(r)) return r.leq(l) if (isComparable(r)) return r.leq(l)
return l >= r return toValue(l) >= toValue(r)
}, },
'<=': (l: any, r: any) => { '<=': (l: any, r: any) => {
if (isComparable(l)) return l.leq(r) if (isComparable(l)) return l.leq(r)
if (isComparable(r)) return r.geq(l) if (isComparable(r)) return r.geq(l)
return l <= r return toValue(l) <= toValue(r)
}, },
'contains': (l: any, r: any) => { 'contains': (l: any, r: any) => {
l = toValue(l) l = toValue(l)
r = toValue(r) r = toValue(r)
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false
}, },
'and': (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(l, ctx) || isTruthy(r, ctx) 'or': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) || isTruthy(toValue(r), ctx)
} }
+12
View File
@@ -72,4 +72,16 @@ describe('drop/drop', function () {
const html = await liquid.parseAndRender(tpl, { drop: new CustomDrop() }) const html = await liquid.parseAndRender(tpl, { drop: new CustomDrop() })
expect(html).to.equal('foobar: foo;bar;') 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')
})
}) })