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]>
This commit is contained in:
Guillermo Casal Caro
2024-05-24 10:35:49 +08:00
committed by GitHub
co-authored by Guillermo Casal Caro
parent 6ca5376362
commit 55e144a029
2 changed files with 28 additions and 1 deletions
+8 -1
View File
@@ -9,5 +9,12 @@ export interface Comparable {
}
export function isComparable (arg: any): arg is Comparable {
return arg && isFunction(arg.equals)
return (
arg &&
isFunction(arg.equals) &&
isFunction(arg.gt) &&
isFunction(arg.geq) &&
isFunction(arg.lt) &&
isFunction(arg.leq)
)
}
+20
View File
@@ -83,6 +83,26 @@ describe('drop/drop', function () {
const html = await liquid.parseAndRender(tpl, { address, customer })
expect(html).toBe('test')
})
it('should correctly evaluate custom Drop objects with equals function without full Comparable implementation', async () => {
class TestDrop extends Drop {
value: string;
constructor () {
super()
this.value = 'test'
}
equals (rhs: string): boolean {
return this.valueOf() === rhs
}
valueOf (): string {
return this.value
}
}
const address = new TestDrop()
const customer = { default_address: new TestDrop() }
const tpl = `{{ address >= customer.default_address }}`
const html = await liquid.parseAndRender(tpl, { address, customer })
expect(html).toBe('true')
})
it('should support returning supported value types from liquidMethodMissing', async function () {
class DynamicTypeDrop extends Drop {
liquidMethodMissing (key: string) {