From 91fd52170990801b241f5906fc1a74cfc445bf87 Mon Sep 17 00:00:00 2001 From: Guillermo Casal Caro Date: Thu, 23 May 2024 16:43:38 +0200 Subject: [PATCH 1/2] Improve isComparable function to ensure full Comparable interface implementation --- 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 9e7cd69a9c..50cd380b86 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 bf299cefe8..8ca0fb1c62 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) { From ce60333cf79a48bad89fb4654ec078000e73f69a Mon Sep 17 00:00:00 2001 From: Guillermo Casal Caro Date: Thu, 23 May 2024 17:02:14 +0200 Subject: [PATCH 2/2] Deleted trailing commas --- src/drop/comparable.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/drop/comparable.ts b/src/drop/comparable.ts index 50cd380b86..b950dc310f 100644 --- a/src/drop/comparable.ts +++ b/src/drop/comparable.ts @@ -10,11 +10,11 @@ export interface Comparable { export function isComparable (arg: any): arg is Comparable { return ( - arg && - isFunction(arg.equals) && - isFunction(arg.gt) && - isFunction(arg.geq) && - isFunction(arg.lt) && + arg && + isFunction(arg.equals) && + isFunction(arg.gt) && + isFunction(arg.geq) && + isFunction(arg.lt) && isFunction(arg.leq) - ); + ) }