fix: tablerow cols defaults to 0, #74

This commit is contained in:
harttle
2018-07-07 01:18:11 +08:00
parent 44b1aac096
commit 88fc05732f
3 changed files with 26 additions and 31 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
language: node_js
node_js:
- "node"
- "lts/*"
- "8"
- "6"
- "4"
before_script:
- npm install -g mocha
+2 -3
View File
@@ -32,17 +32,17 @@ module.exports = function (liquid) {
render: function (scope, hash) {
var collection = Liquid.evalExp(this.collection, scope) || []
var html = '<table>'
var html = ''
var offset = hash.offset || 0
var limit = (hash.limit === undefined) ? collection.length : hash.limit
var cols = hash.cols
var row
var col
if (!cols) throw new Error(`illegal cols: ${cols}`)
// build array of arguments to pass to sequential promises...
collection = collection.slice(offset, offset + limit)
if (!cols) cols = collection.length
var contexts = []
collection.some((item, i) => {
var ctx = {}
@@ -76,7 +76,6 @@ module.exports = function (liquid) {
if (row > 0) {
html += '</tr>'
}
html += '</table>'
return html
})
}
+22 -26
View File
@@ -5,28 +5,39 @@ chai.use(require('chai-as-promised'))
describe('tags/tablerow', function () {
var liquid = Liquid()
it('should support tablerow', function () {
var src = '{% tablerow i in (1..3)%}{{ i }}{% endtablerow %}'
var dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support cols', function () {
var src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}'
var ctx = {
alpha: ['a', 'b', 'c']
}
var dst = '<table>' +
var dst =
'<tr class="row1"><td class="col1">a</td><td class="col2">b</td></tr>' +
'<tr class="row2"><td class="col1">c</td></tr>' +
'</table>'
'<tr class="row2"><td class="col1">c</td></tr>'
return expect(liquid.parseAndRender(src, ctx)).to.eventually.equal(dst)
})
it('should support cols set to 0', function () {
var src = '{% tablerow i in (1..3) cols:0 %}{{ i }}{% endtablerow %}'
var dst = '<tr class="row1"><td class="col1">1</td><td class="col2">2</td><td class="col3">3</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support empty tablerow', function () {
var src = '{% tablerow i in (1..0) cols:2 %}{{ i }}{% endtablerow %}'
var dst = '<table></table>'
var dst = ''
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support empty array', function () {
var src = '{% tablerow i in alpha.z cols:2 %}{{ i }}{% endtablerow %}'
var dst = '<table></table>'
var dst = ''
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
@@ -38,39 +49,24 @@ describe('tags/tablerow', function () {
it('should support tablerow with range', function () {
var src = '{% tablerow i in (1..5) cols:2 %}{{ i }}{% endtablerow %}'
var dst = '<table>' +
var dst =
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td><td class="col2">4</td></tr>' +
'<tr class="row3"><td class="col1">5</td></tr>' +
'</table>'
'<tr class="row3"><td class="col1">5</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('tablerow should throw on illegal cols 1', function () {
var src = '{% tablerow i in (1..5) cols:0 %}{{ i }}{% endtablerow %}'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/illegal cols: 0/)
})
it('tablerow should throw on illegal cols 2', function () {
var src = '{% tablerow i in (1..5) %}{{ i }}{% endtablerow %}'
return expect(liquid.parseAndRender(src))
.to.be.rejectedWith(/illegal cols: undefined/)
})
it('should support tablerow with limit', function () {
var src = '{% tablerow i in (1..5) cols:2 limit:3 %}{{ i }}{% endtablerow %}'
var dst = '<table>' +
var dst =
'<tr class="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
'<tr class="row2"><td class="col1">3</td></tr>' +
'</table>'
'<tr class="row2"><td class="col1">3</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
it('should support tablerow with offset', function () {
var src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
var dst = '<table>' +
'<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>' +
'</table>'
var dst = '<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>'
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
})
})