mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-15 04:10:40 -07:00
fix: enumerate Promises (e.g. in for & tablerow) (#237)
* fix: enumerate Promise<array> (e.g. in {% for ... %})
Previously, a Promise of an array was not being enumerated in
{% for %}, for example. This is misleading since the library
handles promises elsewhere (e.g. if you {% assign x = promiseArray %}
and then {% for v in x %}, it worked just fine.
This PR makes Promises of arrays handled by changing toEnumerable
to handle then-ables. This affects other iterators, too, e.g. tablerow,
so I put in a test for that as well.
This commit is contained in:
@@ -20,15 +20,23 @@ describe('tags/for', function () {
|
||||
nullProtoObj: Object.create(null),
|
||||
obj: { foo: 'bar', coo: 'haa' },
|
||||
alpha: ['a', 'b', 'c'],
|
||||
promiseArray: Promise.resolve(['a', 'b', 'c']),
|
||||
emptyArray: []
|
||||
}
|
||||
})
|
||||
|
||||
it('should support array', async function () {
|
||||
const src = '{%for c in alpha%}{{c}}{%endfor%}'
|
||||
const html = await liquid.parseAndRender(src, scope)
|
||||
return expect(html).to.equal('abc')
|
||||
})
|
||||
|
||||
it('should support promise of array', async function () {
|
||||
const src = '{%for c in promiseArray%}{{c}}{%endfor%}'
|
||||
const html = await liquid.parseAndRender(src, scope)
|
||||
return expect(html).to.equal('abc')
|
||||
})
|
||||
|
||||
it('should support object', async function () {
|
||||
const src = '{%for item in obj%}{{item[0]}},{{item[1]}}-{%else%}b{%endfor%}'
|
||||
const html = await liquid.parseAndRender(src, scope)
|
||||
|
||||
@@ -14,6 +14,16 @@ describe('tags/tablerow', function () {
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
|
||||
it('should support promises', async function () {
|
||||
const src = '{% tablerow i in promiseNumbers %}{{ i }}{% endtablerow %}'
|
||||
const ctx = {
|
||||
promiseNumbers: Promise.resolve([1,2,3])
|
||||
}
|
||||
const dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
|
||||
const html = await liquid.parseAndRender(src, ctx)
|
||||
return expect(html).to.equal(dst)
|
||||
})
|
||||
|
||||
it('should support cols', async function () {
|
||||
const src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}'
|
||||
const ctx = {
|
||||
|
||||
Reference in New Issue
Block a user