diff --git a/src/render/operator.ts b/src/render/operator.ts index b97291bc0b..4f0f0dde98 100644 --- a/src/render/operator.ts +++ b/src/render/operator.ts @@ -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) } diff --git a/test/integration/drop/drop.ts b/test/integration/drop/drop.ts index b9a5bf78dc..f32ebfabe2 100644 --- a/test/integration/drop/drop.ts +++ b/test/integration/drop/drop.ts @@ -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') + }) })