Skip to content

Commit c3e51ca

Browse files
committedJul 9, 2022
fix: for tag not respecting Drop#valueOf(), fixes #515
1 parent a19feea commit c3e51ca

File tree

6 files changed

+15
-18
lines changed

6 files changed

+15
-18
lines changed
 

‎src/drop/drop.ts

-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
export abstract class Drop {
2-
public valueOf (): any {
3-
return undefined
4-
}
5-
62
public liquidMethodMissing (key: string | number): Promise<string | undefined> | string | undefined {
73
return undefined
84
}

‎src/util/collection.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { isNil, isString, isObject, isArray, isIterable } from './underscore'
1+
import { isNil, isString, isObject, isArray, isIterable, toValue } from './underscore'
22

33
export function toEnumerable (val: any) {
4+
val = toValue(val)
45
if (isArray(val)) return val
56
if (isString(val) && val.length > 0) return [val]
67
if (isIterable(val)) return Array.from(val)

‎src/util/underscore.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export function stringify (value: any): string {
3939
}
4040

4141
export function toValue (value: any): any {
42-
return value instanceof Drop ? value.valueOf() : value
42+
return (value instanceof Drop && isFunction(value.valueOf)) ? value.valueOf() : value
4343
}
4444

4545
export function isNumber (value: any): value is number {

‎test/integration/builtin/tags/for.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,7 @@ describe('tags/for', function () {
324324
yield 'b'
325325
yield 'c'
326326
}
327-
328-
public valueOf (): string {
327+
toString () {
329328
return 'MockIterableDrop'
330329
}
331330
}

‎test/integration/drop/drop.ts

+11
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,15 @@ describe('drop/drop', function () {
6161
const html = await liquid.parseAndRender(`{{obj.foo}}`, { obj: new PromiseDrop() })
6262
expect(html).to.equal('FOO')
6363
})
64+
it('should respect valueOf', async () => {
65+
class CustomDrop extends Drop {
66+
prop = 'not enumerable'
67+
valueOf () {
68+
return ['foo', 'bar']
69+
}
70+
}
71+
const tpl = '{{drop}}: {% for field in drop %}{{ field }};{% endfor %}'
72+
const html = await liquid.parseAndRender(tpl, { drop: new CustomDrop() })
73+
expect(html).to.equal('foobar: foo;bar;')
74+
})
6475
})

‎test/unit/drop/drop.ts

-10
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.