From ce39645ee1e3ddd4c70f86a36023b2c1f10c7ef3 Mon Sep 17 00:00:00 2001 From: Gaston Robledo Date: Tue, 24 Sep 2019 14:45:18 -0300 Subject: [PATCH 1/2] adding changes to use @last, @first, @index, @key on each for streams --- lib/handlebars/helpers/each.js | 16 ++++++++--- lib/handlebars/utils.js | 49 +++++++++++++++++++++++++++++++++- tests/test_helpers.js | 30 ++++++++++++++++++++- 3 files changed, 90 insertions(+), 5 deletions(-) diff --git a/lib/handlebars/helpers/each.js b/lib/handlebars/helpers/each.js index ee6d9ad..0f1869e 100755 --- a/lib/handlebars/helpers/each.js +++ b/lib/handlebars/helpers/each.js @@ -1,7 +1,7 @@ const { Stream, Transform } = require('stream'), pump = require('pump'), { - createFrame, isArray, isFunction, isPromise + createFrame, isArray, isFunction, isPromise, Counter } = require('../utils'), Exception = require('../exception') @@ -47,10 +47,20 @@ module.exports = function(instance) { const transform = new Transform({ objectMode: true, transform(chunk, enc, cb) { - fn(chunk).then(() => cb()).catch(e => cb(e)) + const { first, last, counter } = chunk + if (data) { + data.key = counter + data.index = counter + data.first = first + data.last = last + } + fn(chunk.data, { + data, + blockParams: [chunk.data, counter] + }).then(() => cb()).catch(e => cb(e)) } }) - pump(context, transform, (err) => { + pump(context, Counter(), transform, (err) => { if (err) { reject() } else { diff --git a/lib/handlebars/utils.js b/lib/handlebars/utils.js index fedf937..814db41 100755 --- a/lib/handlebars/utils.js +++ b/lib/handlebars/utils.js @@ -1,6 +1,9 @@ /* eslint-disable prefer-rest-params */ /* eslint-disable no-param-reassign */ /* eslint-disable no-restricted-syntax */ +const { Transform } = require('stream') + + const escape = { '&': '&', '<': '<', @@ -104,6 +107,49 @@ function isPromise(obj) { return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function' } +const COUNTER_NULL_SYMBOL = Symbol('COUNTER_NULL_SYMBOL'), + Counter = () => { + let data = COUNTER_NULL_SYMBOL, + counter = 1, + first = true + + const counterStream = new Transform({ + + objectMode: true, + decodeStrings: false, + highWaterMark: 1, + + transform(chunk, encoding, callback) { + if (data === COUNTER_NULL_SYMBOL) { + data = chunk + return callback() + } + this.push({ + data, counter, last: false, first + }) + first = false + counter += 1 + data = chunk + + return callback() + + }, + }) + /* eslint-disable no-underscore-dangle */ + counterStream._flush = function(callback) { + if (data === COUNTER_NULL_SYMBOL) { + return callback() + } + this.push({ + data, counter, last: true, first + }) + return callback() + + } + + return counterStream + } + module.exports = { createFrame, @@ -113,5 +159,6 @@ module.exports = { isArray, isFunction, extend, - isPromise + isPromise, + Counter } diff --git a/tests/test_helpers.js b/tests/test_helpers.js index b12ffe4..d9368af 100644 --- a/tests/test_helpers.js +++ b/tests/test_helpers.js @@ -180,7 +180,7 @@ describe('Test stream results', () => { return [{ name: 'test' }, { name: 'test2' }] }) - hbs.compile('{{#extend "layout"}}{{#prepend "body_prepend"}}{{#each (cursor)}}

{{name}}

{{#delay}}{{/delay}}

{{/each}}{{/prepend}}{{#append "body_append"}}{{#each (cursor)}}{{name}}

{{#delay}}{{/delay}}

{{/each}}{{/append}}{{#replace "body_replace"}}{{/replace}}{{/extend}}', {}) + hbs.compile('{{#extend "layout"}}{{#prepend "body_prepend"}}{{#each (cursor)}}{{#if @first}}test{{/if}}

{{name}}

{{#delay}}{{/delay}}

{{/each}}{{/prepend}}{{#append "body_append"}}{{#each (cursor)}}{{name}}

{{#delay}}{{/delay}}

{{/each}}{{/append}}{{#replace "body_replace"}}{{/replace}}{{/extend}}', {}) const expected = '

Layout

Text before appended contenttest

1000

test2

1000

test

1000

test2

1000

Text before appended content
' let result = '' @@ -516,4 +516,32 @@ describe('Test stream results', () => { }).catch(e => done(e)) }) + + it('test @first @last on stream', (done) => { + const hbs = new HandlebarsStream([{ + name: 'test', + content: '{{#each names}}{{#if @first}}
{{/if}}{{name}}{{#if @last}}
{{/if}}{{/each}}' + }]), + expected = '
gastonpedro
', + stream = new Transform({ + objectMode: true, + transform(chunk, enc, cb) { + this.push(chunk) + cb() + } + }) + hbs.compile('{{#extend "test"}}{{#replace "name"}}Nombre:{{/replace}}{{/extend}}', { names: stream }) + let result = '' + hbs.on('data', (block) => { + result += block.toString() + }).on('end', () => { + assert(result.trim() === expected.trim()) + done() + }) + + stream.write({ name: 'gaston' }) + stream.write({ name: 'pedro' }) + stream.end() + }) + }) From 8c26d3e0be4b9d83e71314ee33079d5520d6e024 Mon Sep 17 00:00:00 2001 From: Gaston Robledo Date: Tue, 24 Sep 2019 14:46:14 -0300 Subject: [PATCH 2/2] update libs --- package-lock.json | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/package-lock.json b/package-lock.json index cb00abd..14ea02b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "handlebars-stream-render", - "version": "1.1.5", + "version": "1.1.6", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -584,10 +584,13 @@ } }, "eslint-utils": { - "version": "1.3.1", - "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.3.1.tgz", - "integrity": "sha512-Z7YjnIldX+2XMcjr7ZkgEsOj/bREONV60qYeB/bjMAqqqZ4zxKyWX+BOUkdmRmA9riiIPVvo5x86m5elviOk0Q==", - "dev": true + "version": "1.4.2", + "resolved": "https://registry.npmjs.org/eslint-utils/-/eslint-utils-1.4.2.tgz", + "integrity": "sha512-eAZS2sEUMlIeCjBeubdj45dmBHQwPHWyBcT1VSYB7o9x9WRRqKxyUoiXlRjyAwzN7YEzHJlYg0NmzDRWx6GP4Q==", + "dev": true, + "requires": { + "eslint-visitor-keys": "^1.0.0" + } }, "eslint-visitor-keys": { "version": "1.0.0",