Skip to content

Commit 55e144a

Browse files
GuillermoCasalCaroGuillermo Casal Caro
and
Guillermo Casal Caro
authored
fix: isComparable full interface check (#701)
* Improve isComparable function to ensure full Comparable interface implementation * Deleted trailing commas --------- Co-authored-by: Guillermo Casal Caro <[email protected]>
1 parent 6ca5376 commit 55e144a

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/drop/comparable.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,12 @@ export interface Comparable {
99
}
1010

1111
export function isComparable (arg: any): arg is Comparable {
12-
return arg && isFunction(arg.equals)
12+
return (
13+
arg &&
14+
isFunction(arg.equals) &&
15+
isFunction(arg.gt) &&
16+
isFunction(arg.geq) &&
17+
isFunction(arg.lt) &&
18+
isFunction(arg.leq)
19+
)
1320
}

test/integration/drop/drop.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,26 @@ describe('drop/drop', function () {
8383
const html = await liquid.parseAndRender(tpl, { address, customer })
8484
expect(html).toBe('test')
8585
})
86+
it('should correctly evaluate custom Drop objects with equals function without full Comparable implementation', async () => {
87+
class TestDrop extends Drop {
88+
value: string;
89+
constructor () {
90+
super()
91+
this.value = 'test'
92+
}
93+
equals (rhs: string): boolean {
94+
return this.valueOf() === rhs
95+
}
96+
valueOf (): string {
97+
return this.value
98+
}
99+
}
100+
const address = new TestDrop()
101+
const customer = { default_address: new TestDrop() }
102+
const tpl = `{{ address >= customer.default_address }}`
103+
const html = await liquid.parseAndRender(tpl, { address, customer })
104+
expect(html).toBe('true')
105+
})
86106
it('should support returning supported value types from liquidMethodMissing', async function () {
87107
class DynamicTypeDrop extends Drop {
88108
liquidMethodMissing (key: string) {

0 commit comments

Comments
 (0)