Skip to content

Commit

Permalink
fix: isComparable full interface check (#701)
Browse files Browse the repository at this point in the history
* Improve isComparable function to ensure full Comparable interface implementation

* Deleted trailing commas

---------

Co-authored-by: Guillermo Casal Caro <Guillermo.Casal@unit4.com>
  • Loading branch information
GuillermoCasalCaro and Guillermo Casal Caro authored May 24, 2024
1 parent 6ca5376 commit 55e144a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/drop/comparable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 20 additions & 0 deletions test/integration/drop/drop.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 55e144a

Please sign in to comment.