fix: allow liquidMethodMissing to return any supported value type (#698)

* fix: allow liquidMethodMissing to return any supported value type

* Update src/drop/drop.ts

Co-authored-by: Jun Yang <[email protected]>

---------

Co-authored-by: Jun Yang <[email protected]>
This commit is contained in:
Adam Tanner
2024-05-22 12:35:46 +08:00
committed by GitHub
co-authored by Jun Yang
parent 12001b6161
commit 0983f2c420
2 changed files with 17 additions and 1 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
export abstract class Drop { export abstract class Drop {
public liquidMethodMissing (key: string | number): Promise<string | undefined> | string | undefined { public liquidMethodMissing (key: string | number): Promise<any> | any {
return undefined return undefined
} }
} }
+16
View File
@@ -83,4 +83,20 @@ describe('drop/drop', function () {
const html = await liquid.parseAndRender(tpl, { address, customer }) const html = await liquid.parseAndRender(tpl, { address, customer })
expect(html).toBe('test') expect(html).toBe('test')
}) })
it('should support returning supported value types from liquidMethodMissing', async function () {
class DynamicTypeDrop extends Drop {
liquidMethodMissing (key: string) {
switch (key) {
case 'number': return 42
case 'string': return 'foo'
case 'boolean': return true
case 'array': return [1, 2, 3]
case 'object': return { foo: 'bar' }
case 'drop': return new CustomDrop()
}
}
}
const html = await liquid.parseAndRender(`{{obj.number}} {{obj.string}} {{obj.boolean}} {{obj.array | first}} {{obj.object.foo}} {{obj.drop.getName}}`, { obj: new DynamicTypeDrop() })
expect(html).toBe('42 foo true 1 bar GET NAME')
})
}) })