Skip to content

Commit

Permalink
Merge pull request #13 from gastonrobledo/master
Browse files Browse the repository at this point in the history
Update from forked master
  • Loading branch information
gastonrobledo authored Sep 24, 2019
2 parents 34f8fbb + 8c26d3e commit fd5926a
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 10 deletions.
16 changes: 13 additions & 3 deletions lib/handlebars/helpers/each.js
Original file line number Diff line number Diff line change
@@ -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')

Expand Down Expand Up @@ -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 {
Expand Down
49 changes: 48 additions & 1 deletion lib/handlebars/utils.js
Original file line number Diff line number Diff line change
@@ -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 = {
'&': '&',
'<': '&lt;',
Expand Down Expand Up @@ -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,
Expand All @@ -113,5 +159,6 @@ module.exports = {
isArray,
isFunction,
extend,
isPromise
isPromise,
Counter
}
13 changes: 8 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion tests/test_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe('Test stream results', () => {
return [{ name: 'test' }, { name: 'test2' }]
})

hbs.compile('{{#extend "layout"}}{{#prepend "body_prepend"}}{{#each (cursor)}}<div><h2>{{name}}</h2><p>{{#delay}}{{/delay}}</p></div>{{/each}}{{/prepend}}{{#append "body_append"}}{{#each (cursor)}}<a>{{name}}</a><p>{{#delay}}{{/delay}}</p></div>{{/each}}{{/append}}{{#replace "body_replace"}}<ul>{{#each (cursor)}}<li>{{name}} - {{#delay}}{{/delay}}</li>{{/each}}</ul>{{/replace}}{{/extend}}', {})
hbs.compile('{{#extend "layout"}}{{#prepend "body_prepend"}}{{#each (cursor)}}{{#if @first}}<span>test</span>{{/if}}<div><h2>{{name}}</h2><p>{{#delay}}{{/delay}}</p></div>{{/each}}{{/prepend}}{{#append "body_append"}}{{#each (cursor)}}<a>{{name}}</a><p>{{#delay}}{{/delay}}</p></div>{{/each}}{{/append}}{{#replace "body_replace"}}<ul>{{#each (cursor)}}<li>{{name}} - {{#delay}}{{/delay}}</li>{{/each}}</ul>{{/replace}}{{/extend}}', {})

const expected = '<html><body><h1>Layout</h1><div><ul><li>test - 1000</li><li>test2 - 1000</li></ul></div><div><i>Text before appended content</i><a>test</a><p>1000</p></div><a>test2</a><p>1000</p></div></div><div><div><h2>test</h2><p>1000</p></div><div><h2>test2</h2><p>1000</p></div><i>Text before appended content</i></div></body></html>'
let result = ''
Expand Down Expand Up @@ -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}}<div>{{/if}}<i>{{name}}</i>{{#if @last}}</div>{{/if}}{{/each}}'
}]),
expected = '<div><i>gaston</i><i>pedro</i></div>',
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()
})

})

0 comments on commit fd5926a

Please sign in to comment.