Skip to content

Commit 4ad383d

Browse files
committedJul 20, 2022
fix: expression support Drop.valueOf, fixes #522
1 parent 66eb3b8 commit 4ad383d

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed
 

‎src/render/operator.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,38 @@ export const defaultOperators: Operators = {
1111
'==': (l: any, r: any) => {
1212
if (isComparable(l)) return l.equals(r)
1313
if (isComparable(r)) return r.equals(l)
14-
return l === r
14+
return toValue(l) === toValue(r)
1515
},
1616
'!=': (l: any, r: any) => {
1717
if (isComparable(l)) return !l.equals(r)
1818
if (isComparable(r)) return !r.equals(l)
19-
return l !== r
19+
return toValue(l) !== toValue(r)
2020
},
2121
'>': (l: any, r: any) => {
2222
if (isComparable(l)) return l.gt(r)
2323
if (isComparable(r)) return r.lt(l)
24-
return l > r
24+
return toValue(l) > toValue(r)
2525
},
2626
'<': (l: any, r: any) => {
2727
if (isComparable(l)) return l.lt(r)
2828
if (isComparable(r)) return r.gt(l)
29-
return l < r
29+
return toValue(l) < toValue(r)
3030
},
3131
'>=': (l: any, r: any) => {
3232
if (isComparable(l)) return l.geq(r)
3333
if (isComparable(r)) return r.leq(l)
34-
return l >= r
34+
return toValue(l) >= toValue(r)
3535
},
3636
'<=': (l: any, r: any) => {
3737
if (isComparable(l)) return l.leq(r)
3838
if (isComparable(r)) return r.geq(l)
39-
return l <= r
39+
return toValue(l) <= toValue(r)
4040
},
4141
'contains': (l: any, r: any) => {
4242
l = toValue(l)
4343
r = toValue(r)
4444
return l && isFunction(l.indexOf) ? l.indexOf(r) > -1 : false
4545
},
46-
'and': (l: any, r: any, ctx: Context) => isTruthy(l, ctx) && isTruthy(r, ctx),
47-
'or': (l: any, r: any, ctx: Context) => isTruthy(l, ctx) || isTruthy(r, ctx)
46+
'and': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) && isTruthy(toValue(r), ctx),
47+
'or': (l: any, r: any, ctx: Context) => isTruthy(toValue(l), ctx) || isTruthy(toValue(r), ctx)
4848
}

‎test/integration/drop/drop.ts

+12
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,16 @@ describe('drop/drop', function () {
7272
const html = await liquid.parseAndRender(tpl, { drop: new CustomDrop() })
7373
expect(html).to.equal('foobar: foo;bar;')
7474
})
75+
it('should support valueOf in == expression', async () => {
76+
class AddressDrop extends Drop {
77+
valueOf () {
78+
return 'test'
79+
}
80+
}
81+
const address = new AddressDrop()
82+
const customer = { default_address: new AddressDrop() }
83+
const tpl = `{% if address == customer.default_address %}{{address}}{% endif %}`
84+
const html = await liquid.parseAndRender(tpl, { address, customer })
85+
expect(html).to.equal('test')
86+
})
7587
})

0 commit comments

Comments
 (0)
Please sign in to comment.