From 55e144a0298047349d55d8483a46b2513303d940 Mon Sep 17 00:00:00 2001 From: Guillermo Casal Caro Date: Fri, 24 May 2024 04:35:49 +0200 Subject: [PATCH] fix: isComparable full interface check (#701) * Improve isComparable function to ensure full Comparable interface implementation * Deleted trailing commas --------- Co-authored-by: Guillermo Casal Caro --- src/drop/comparable.ts | 9 ++++++++- test/integration/drop/drop.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/drop/comparable.ts b/src/drop/comparable.ts index 9e7cd69a9..b950dc310 100644 --- a/src/drop/comparable.ts +++ b/src/drop/comparable.ts @@ -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) + ) } diff --git a/test/integration/drop/drop.spec.ts b/test/integration/drop/drop.spec.ts index bf299cefe..8ca0fb1c6 100644 --- a/test/integration/drop/drop.spec.ts +++ b/test/integration/drop/drop.spec.ts @@ -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) {