diff --git a/src/builtin/tags/for.ts b/src/builtin/tags/for.ts index 8dba084524..60f5af1e91 100644 --- a/src/builtin/tags/for.ts +++ b/src/builtin/tags/for.ts @@ -1,4 +1,3 @@ -import { mapSeries } from '../../util/promise' import { isString, isObject, isArray } from '../../util/underscore' import { evalExp } from '../../render/syntax' import assert from '../../util/assert' @@ -10,6 +9,7 @@ import Hash from '../../template/tag/hash' import ITemplate from '../../template/itemplate' import ITagImplOptions from '../../template/tag/itag-impl-options' import ParseStream from '../../parser/parse-stream' +import { ForloopDrop } from '../../drop/forloop-drop' const re = new RegExp(`^(${identifier.source})\\s+in\\s+` + `(${value.source})` + @@ -61,39 +61,22 @@ export default { collection = collection.slice(offset, offset + limit) if (this.reversed) collection.reverse() - const contexts = collection.map((item: string, i: number) => { - const ctx = {} - ctx[this.variable] = item - ctx['forloop'] = { - first: i === 0, - index: i + 1, - index0: i, - last: i === collection.length - 1, - length: collection.length, - rindex: collection.length - i, - rindex0: collection.length - i - 1 - } - return ctx - }) - + const context = { forloop: new ForloopDrop(collection.length) } + scope.push(context) let html = '' - let finished = false - await mapSeries(contexts, async context => { - if (finished) return - - scope.push(context) + for (const item of collection) { + context[this.variable] = item try { html += await this.liquid.renderer.renderTemplates(this.templates, scope) } catch (e) { if (e.name === 'RenderBreakError') { html += e.resolvedHTML - if (e.message === 'break') { - finished = true - } + if (e.message === 'break') break } else throw e } - scope.pop(context) - }) + context.forloop.next() + } + scope.pop() return html } } diff --git a/src/builtin/tags/tablerow.ts b/src/builtin/tags/tablerow.ts index 00e01d891c..2562b83748 100644 --- a/src/builtin/tags/tablerow.ts +++ b/src/builtin/tags/tablerow.ts @@ -1,4 +1,3 @@ -import { mapSeries } from '../../util/promise' import assert from '../../util/assert' import { evalExp } from '../../render/syntax' import { identifier, value, hash } from '../../parser/lexical' @@ -9,6 +8,7 @@ import Scope from '../../scope/scope' import Hash from '../../template/tag/hash' import ITagImplOptions from '../../template/tag/itag-impl-options' import ParseStream from '../../parser/parse-stream' +import { TablerowloopDrop } from '../../drop/tablerowloop-drop' const re = new RegExp(`^(${identifier.source})\\s+in\\s+` + `(${value.source})` + @@ -42,34 +42,24 @@ export default { collection = collection.slice(offset, offset + limit) const cols = hash.cols || collection.length - const contexts = collection.map((item: any) => { - const ctx = {} - ctx[this.variable] = item - return ctx - }) - let row: number = 0 + const tablerowloop = new TablerowloopDrop(collection.length, cols) + const ctx = { tablerowloop } + scope.push(ctx) + let html = '' - await mapSeries(contexts, async (context, idx) => { - row = Math.floor(idx / cols) + 1 - const col = (idx % cols) + 1 - if (col === 1) { - if (row !== 1) { - html += '' - } - html += `` + for (let idx = 0; idx < collection.length; idx++, tablerowloop.next()) { + ctx[this.variable] = collection[idx] + if (tablerowloop.col0() === 0) { + if (tablerowloop.row() !== 1) html += '' + html += `` } - - html += `` - scope.push(context) + html += `` html += await this.liquid.renderer.renderTemplates(this.templates, scope) html += '' - scope.pop(context) - return html - }) - if (row > 0) { - html += '' } + if (collection.length) html += '' + scope.pop(ctx) return html } } as ITagImplOptions diff --git a/src/drop/forloop-drop.ts b/src/drop/forloop-drop.ts new file mode 100644 index 0000000000..699d5c4298 --- /dev/null +++ b/src/drop/forloop-drop.ts @@ -0,0 +1,34 @@ +import { Drop } from './drop' + +export class ForloopDrop extends Drop { + protected i: number = 0 + length: number + constructor (length: number) { + super() + this.length = length + } + next () { + this.i++ + } + index0 () { + return this.i + } + index () { + return this.i + 1 + } + first () { + return this.i === 0 + } + last () { + return this.i === this.length - 1 + } + rindex () { + return this.length - this.i + } + rindex0 () { + return this.length - this.i - 1 + } + valueOf () { + return JSON.stringify(this) + } +} diff --git a/src/drop/tablerowloop-drop.ts b/src/drop/tablerowloop-drop.ts new file mode 100644 index 0000000000..835bf3737d --- /dev/null +++ b/src/drop/tablerowloop-drop.ts @@ -0,0 +1,25 @@ +import { ForloopDrop } from './forloop-drop' + +export class TablerowloopDrop extends ForloopDrop { + private cols: number + constructor (length: number, cols: number) { + super(length) + this.length = length + this.cols = cols + } + row () { + return Math.floor(this.i / this.cols) + 1 + } + col0 () { + return (this.i % this.cols) + } + col () { + return this.col0() + 1 + } + col_first () { // eslint-disable-line + return this.col0() === 0 + } + col_last () { // eslint-disable-line + return this.col() === this.cols + } +} diff --git a/src/util/promise.ts b/src/util/promise.ts deleted file mode 100644 index 8f225ee629..0000000000 --- a/src/util/promise.ts +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Call functions in serial until someone rejected. - * @param {Array} iterable the array to iterate with. - * @param {Array} iteratee returns a new promise. - * The iteratee is invoked with three arguments: (value, index, iterable). - */ -export function mapSeries ( - iterable: T1[], - iteratee: (item: T1, idx: number, iterable: T1[]) => Promise | T2 -): Promise { - let ret = Promise.resolve(0) - const result: T2[] = [] - iterable.forEach(function (item, idx) { - ret = ret - .then(() => iteratee(item, idx, iterable)) - .then(x => result.push(x)) - }) - return ret.then(() => result) -} diff --git a/test/integration/builtin/tags/for.ts b/test/integration/builtin/tags/for.ts index aa31b923a0..a5b3921cd3 100644 --- a/test/integration/builtin/tags/for.ts +++ b/test/integration/builtin/tags/for.ts @@ -34,7 +34,11 @@ describe('tags/for', function () { const html = await liquid.parseAndRender(src, ctx) return expect(html).to.equal('foo,bar-coo,haa-') }) - + it('should output forloop', async function () { + const src = '{%for i in (1..1)%}{{forloop}}{%endfor%}' + const html = await liquid.parseAndRender(src, ctx) + return expect(html).to.equal('{"i":0,"length":1}') + }) describe('scope', function () { it('should read super scope', async function () { const src = '{%for a in (1..2)%}{{num}}{%endfor%}' diff --git a/test/integration/builtin/tags/tablerow.ts b/test/integration/builtin/tags/tablerow.ts index 7fd1adb72c..1088066842 100644 --- a/test/integration/builtin/tags/tablerow.ts +++ b/test/integration/builtin/tags/tablerow.ts @@ -69,10 +69,42 @@ describe('tags/tablerow', function () { return expect(html).to.equal(dst) }) - it('should support tablerow with offset', async function () { - const src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}' - const dst = '45' + it('should support index0, index, rindex0, rindex', async function () { + const src = '{% tablerow i in (1..3)%}{{tablerowloop.index0}}{{tablerowloop.index}}{{tablerowloop.rindex0}}{{tablerowloop.rindex}}{% endtablerow %}' + const dst = '012312122301' const html = await liquid.parseAndRender(src) return expect(html).to.equal(dst) }) + it('should support first, last, length', async function () { + const src = '{% tablerow i in (1..3)%}{{tablerowloop.first}} {{tablerowloop.last}} {{tablerowloop.length}}{% endtablerow %}' + const dst = 'true false 3false false 3false true 3' + const html = await liquid.parseAndRender(src) + return expect(html).to.equal(dst) + }) + it('should support col, row, col0, col_first, col_last', async function () { + const src = '{% tablerow i in (1..3)%}{{tablerowloop.col}} {{tablerowloop.col0}} {{tablerowloop.col_first}} {{tablerowloop.col_last}}{% endtablerow %}' + const dst = '1 0 true false2 1 false false3 2 false true' + const html = await liquid.parseAndRender(src) + return expect(html).to.equal(dst) + }) + describe('offset', function () { + it('should support tablerow with offset', async function () { + const src = '{% tablerow i in (1..5) cols:2 offset:3 %}{{ i }}{% endtablerow %}' + const dst = '45' + const html = await liquid.parseAndRender(src) + return expect(html).to.equal(dst) + }) + it('index should also start at 1', async function () { + const src = '{% tablerow i in (1..4) cols:2 offset:2 %}{{tablerowloop.index}}{% endtablerow %}' + const dst = '12' + const html = await liquid.parseAndRender(src) + return expect(html).to.equal(dst) + }) + it('col should also start at 1', async function () { + const src = '{% tablerow i in (1..4) cols:2 offset:3 %}{{tablerowloop.col}}{% endtablerow %}' + const dst = '1' + const html = await liquid.parseAndRender(src) + return expect(html).to.equal(dst) + }) + }) }) diff --git a/test/unit/util/promise.ts b/test/unit/util/promise.ts deleted file mode 100644 index 9a1d2cc61b..0000000000 --- a/test/unit/util/promise.ts +++ /dev/null @@ -1,57 +0,0 @@ -import * as chai from 'chai' -import * as sinon from 'sinon' -import * as sinonChai from 'sinon-chai' -import * as chaiAsPromised from 'chai-as-promised' - -const expect = chai.expect -chai.use(sinonChai) -chai.use(chaiAsPromised) - -const P = require('../../../src/util/promise') - -describe('util/promise', function () { - describe('.mapSeries()', async function () { - it('should resolve when all resolved', async function () { - const result = await P.mapSeries( - ['first', 'second', 'third'], - (item: string) => Promise.resolve(item) - ) - return expect(result).to.deep.equal(['first', 'second', 'third']) - }) - it('should reject with the error that first callback rejected', () => { - const p = P.mapSeries(['first', 'second'], - (item: string) => Promise.reject(item)) - return expect(p).to.rejectedWith('first') - }) - it('should resolve in series', function () { - const spy1 = sinon.spy() - const spy2 = sinon.spy() - return P - .mapSeries( - ['first', 'second'], - (item: string, idx: number) => new Promise(function (resolve) { - if (idx === 0) { - setTimeout(function () { - spy1() - resolve('first cb') - }, 10) - } else { - spy2() - resolve('foo') - } - })) - .then(() => expect(spy2).to.have.been.calledAfter(spy1)) - }) - it('should not call rest of callbacks once rejected', () => { - const spy = sinon.spy() - return P - .mapSeries(['first', 'second'], (item: string, idx: number) => { - if (idx > 0) { - spy() - } - return Promise.reject(new Error(item)) - }) - .catch(() => expect(spy).to.not.have.been.called) - }) - }) -})