Skip to content

Commit

Permalink
fix each helper and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gastonrobledo committed Mar 10, 2022
1 parent 9cb544c commit 407c651
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
33 changes: 16 additions & 17 deletions helpers/each.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const {
appendContextPath, createFrame, blockParams, isPromise
} = require('../utils'),
appendContextPath, createFrame, blockParams, isPromise
} = require('../utils'),
{ Readable } = require('stream')
const { resolve } = require('eslint-plugin-promise/rules/lib/promise-statics')

module.exports = (handlebars) => {
handlebars.registerHelper('each', async function(context, options) {
Expand All @@ -11,11 +10,11 @@ module.exports = (handlebars) => {
}

let { fn } = options,
{ inverse } = options,
i = 0,
ret = [],
data,
contextPath
{ inverse } = options,
i = 0,
ret = [],
data,
contextPath

if (options.data && options.ids) {
contextPath = `${appendContextPath(options.data.contextPath, options.ids[0])}.`
Expand All @@ -41,7 +40,7 @@ module.exports = (handlebars) => {
}
}

ret.push(fn(context[field], {
ret.push(await fn(context[field], {
data,
blockParams: blockParams(
[context[field], field],
Expand All @@ -57,28 +56,28 @@ module.exports = (handlebars) => {
if (Array.isArray(context)) {
for (let j = context.length; i < j; i++) {
if (i in context) {
execIteration(i, i, i === context.length - 1)
await execIteration(i, i, i === context.length - 1)
}
}
} else if (global.Symbol && context[global.Symbol.iterator]) {
const newContext = [],
iterator = context[global.Symbol.iterator]()
iterator = context[global.Symbol.iterator]()
for (let it = iterator.next(); !it.done; it = iterator.next()) {
newContext.push(it.value)
}
context = newContext
for (let j = context.length; i < j; i++) {
execIteration(i, i, i === context.length - 1)
await execIteration(i, i, i === context.length - 1)
}
} else if (context instanceof Readable) {
const newContext = []
await new Promise((resolve, reject) => {
context.on('data', (item) => {
newContext.push(item)
}).on('end', () => {
}).on('end', async() => {
context = newContext
for (let j = context.length; i < j; i++) {
execIteration(i, i, i === context.length - 1)
await execIteration(i, i, i === context.length - 1)
}
resolve()
}).once('error', e => reject(e))
Expand All @@ -91,13 +90,13 @@ module.exports = (handlebars) => {
// the last iteration without have to scan the object twice and create
// an itermediate keys array.
if (priorKey !== undefined) {
execIteration(priorKey, i - 1)
await execIteration(priorKey, i - 1)
}
priorKey = key
i++
}
if (priorKey !== undefined) {
execIteration(priorKey, i - 1, true)
await execIteration(priorKey, i - 1, true)
}
}
}
Expand All @@ -106,6 +105,6 @@ module.exports = (handlebars) => {
ret = inverse(this)
}

return (await Promise.all(ret)).join('')
return ret.join('')
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "handlebars-async-helpers",
"version": "1.0.0",
"version": "1.0.1",
"description": "Adding async function support to handlebars helpers",
"main": "index.js",
"scripts": {
Expand Down
20 changes: 10 additions & 10 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('Test async helpers', () => {
const hbs = asyncHelpers(Handlebars)

hbs.registerHelper('sleep', async () => new Promise((resolve) => {
setTimeout(() => resolve('Done!'), 1000)
setTimeout(() => resolve('Done!'), 50)
}))

const result = await hbs.compile('Mark when is completed: {{#sleep}}{{/sleep}}')()
Expand All @@ -22,7 +22,7 @@ describe('Test async helpers', () => {
const hbs = asyncHelpers(Handlebars)
hbs.registerPartial('myPartial', 'Some text: {{#sleep}}{{/sleep}}')
hbs.registerHelper('sleep', async () => new Promise((resolve) => {
setTimeout(() => resolve('Done!'), 1000)
setTimeout(() => resolve('Done!'), 50)
}))

const result = await hbs.compile('Mark when is completed: {{> myPartial}}')()
Expand All @@ -33,7 +33,7 @@ describe('Test async helpers', () => {
it('Test each helper with async helpers inside', async () => {
const hbs = asyncHelpers(Handlebars)
hbs.registerHelper('sleep', async () => new Promise((resolve) => {
setTimeout(() => resolve('Done!'), 1000)
setTimeout(() => resolve('Done!'), 50)
}))

const items = [
Expand Down Expand Up @@ -62,7 +62,7 @@ describe('Test async helpers', () => {
'Drew'
])
hbs.registerHelper('getArray', () => new Promise((resolve) => {
setTimeout(() => resolve(items), 1000)
setTimeout(() => resolve(items), 50)
}))

const result = await hbs.compile('Devs \n{{#each (getArray items)}}Dev: {{this}}\n{{/each}}')({items})
Expand Down Expand Up @@ -98,7 +98,7 @@ describe('Test async helpers', () => {
</div>`
hbs.registerHelper('ipInfo', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 500)
setTimeout(resolve, 50)
})
return {country: 'Canada'}
})
Expand All @@ -120,7 +120,7 @@ describe('Test async helpers', () => {
expected = `<p>Show</p>`
hbs.registerHelper('shouldShow', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 500)
setTimeout(resolve, 50)
})
return true
})
Expand All @@ -137,7 +137,7 @@ describe('Test async helpers', () => {
expected = `<p>Show</p>`
hbs.registerHelper('shouldShow', async () => {
await new Promise((resolve) => {
setTimeout(resolve, 500)
setTimeout(resolve, 50)
})
return null
})
Expand All @@ -151,7 +151,7 @@ describe('Test async helpers', () => {
it('Test with custom helpers and complex replacements', async() => {
const timeout = ms => new Promise(res => setTimeout(res, ms)),
delay = async function delayFn() {
await timeout(100)
await timeout(50)
return 1000
},
hbs = asyncHelpers(Handlebars)
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('Test async helpers', () => {

hbs.registerHelper('delay', delay)
hbs.registerHelper('cursor', async(options) => {
await timeout(1000)
await timeout(50)
return [{ name: 'test' }, { name: 'test2' }]
})

Expand Down Expand Up @@ -280,7 +280,7 @@ describe('Test async helpers', () => {
const hbs = asyncHelpers(Handlebars),
template = '<div>Parent {{> child}}</div>',
child = '<div>Child: {{> grandChild}}</div>',
grandChild = '<p>Grand Child: {{#delayed 500}}{{/delayed}}</p>',
grandChild = '<p>Grand Child: {{#delayed 50}}{{/delayed}}</p>',
expected = '<div>Parent <div>Child: <p>Grand Child: Hello!</p></div></div>'
hbs.registerHelper('delayed', (time) => {
return new Promise((resolve) => {
Expand Down

0 comments on commit 407c651

Please sign in to comment.