mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 09:05:14 -07:00
fix: tablerow cols defaults to 0, #74
This commit is contained in:
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
language: node_js
|
language: node_js
|
||||||
node_js:
|
node_js:
|
||||||
- "node"
|
- "8"
|
||||||
- "lts/*"
|
- "6"
|
||||||
- "4"
|
- "4"
|
||||||
before_script:
|
before_script:
|
||||||
- npm install -g mocha
|
- npm install -g mocha
|
||||||
|
|||||||
+2
-3
@@ -32,17 +32,17 @@ module.exports = function (liquid) {
|
|||||||
render: function (scope, hash) {
|
render: function (scope, hash) {
|
||||||
var collection = Liquid.evalExp(this.collection, scope) || []
|
var collection = Liquid.evalExp(this.collection, scope) || []
|
||||||
|
|
||||||
var html = '<table>'
|
var html = ''
|
||||||
var offset = hash.offset || 0
|
var offset = hash.offset || 0
|
||||||
var limit = (hash.limit === undefined) ? collection.length : hash.limit
|
var limit = (hash.limit === undefined) ? collection.length : hash.limit
|
||||||
|
|
||||||
var cols = hash.cols
|
var cols = hash.cols
|
||||||
var row
|
var row
|
||||||
var col
|
var col
|
||||||
if (!cols) throw new Error(`illegal cols: ${cols}`)
|
|
||||||
|
|
||||||
// build array of arguments to pass to sequential promises...
|
// build array of arguments to pass to sequential promises...
|
||||||
collection = collection.slice(offset, offset + limit)
|
collection = collection.slice(offset, offset + limit)
|
||||||
|
if (!cols) cols = collection.length
|
||||||
var contexts = []
|
var contexts = []
|
||||||
collection.some((item, i) => {
|
collection.some((item, i) => {
|
||||||
var ctx = {}
|
var ctx = {}
|
||||||
@@ -76,7 +76,6 @@ module.exports = function (liquid) {
|
|||||||
if (row > 0) {
|
if (row > 0) {
|
||||||
html += '</tr>'
|
html += '</tr>'
|
||||||
}
|
}
|
||||||
html += '</table>'
|
|
||||||
return html
|
return html
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
+21
-25
@@ -7,26 +7,37 @@ describe('tags/tablerow', function () {
|
|||||||
var liquid = Liquid()
|
var liquid = Liquid()
|
||||||
|
|
||||||
it('should support tablerow', function () {
|
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 src = '{% tablerow i in alpha cols:2 %}{{ i }}{% endtablerow %}'
|
||||||
var ctx = {
|
var ctx = {
|
||||||
alpha: ['a', 'b', 'c']
|
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="row1"><td class="col1">a</td><td class="col2">b</td></tr>' +
|
||||||
'<tr class="row2"><td class="col1">c</td></tr>' +
|
'<tr class="row2"><td class="col1">c</td></tr>'
|
||||||
'</table>'
|
|
||||||
return expect(liquid.parseAndRender(src, ctx)).to.eventually.equal(dst)
|
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 () {
|
it('should support empty tablerow', function () {
|
||||||
var src = '{% tablerow i in (1..0) cols:2 %}{{ i }}{% endtablerow %}'
|
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)
|
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support empty array', function () {
|
it('should support empty array', function () {
|
||||||
var src = '{% tablerow i in alpha.z cols:2 %}{{ i }}{% endtablerow %}'
|
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)
|
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -38,39 +49,24 @@ describe('tags/tablerow', function () {
|
|||||||
|
|
||||||
it('should support tablerow with range', function () {
|
it('should support tablerow with range', function () {
|
||||||
var src = '{% tablerow i in (1..5) cols:2 %}{{ i }}{% endtablerow %}'
|
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="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="row2"><td class="col1">3</td><td class="col2">4</td></tr>' +
|
||||||
'<tr class="row3"><td class="col1">5</td></tr>' +
|
'<tr class="row3"><td class="col1">5</td></tr>'
|
||||||
'</table>'
|
|
||||||
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
|
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 () {
|
it('should support tablerow with limit', function () {
|
||||||
var src = '{% tablerow i in (1..5) cols:2 limit:3 %}{{ i }}{% endtablerow %}'
|
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="row1"><td class="col1">1</td><td class="col2">2</td></tr>' +
|
||||||
'<tr class="row2"><td class="col1">3</td></tr>' +
|
'<tr class="row2"><td class="col1">3</td></tr>'
|
||||||
'</table>'
|
|
||||||
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
|
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should support tablerow with offset', function () {
|
it('should support tablerow with offset', function () {
|
||||||
var src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
|
var src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}'
|
||||||
var dst = '<table>' +
|
var dst = '<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>'
|
||||||
'<tr class="row1"><td class="col1">4</td><td class="col2">5</td></tr>' +
|
|
||||||
'</table>'
|
|
||||||
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
|
return expect(liquid.parseAndRender(src)).to.eventually.equal(dst)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user