gastonrobledo / handlebars-async-helpers

Adding async functions support to handlebars
MIT License
14 stars 15 forks source link

"each" helper throws "TypeError: ret.join is not a function" when the context is an empty list #9

Open toverux opened 1 year ago

toverux commented 1 year ago

This error appears to happen when empty sources are passed to the each helper that is overriden by this library.

It's this line that crashes : https://github.com/gastonrobledo/handlebars-async-helpers/blob/cc6ad44ab165cd2273be266df51115af8c9a1df2/helpers/each.js#L108

The problem is that when the items count is 0, options.inverse() is called on the context and replaces the return value. But by default, when each has no else condition, it's a noop that does not return an array but an empty string, making ret.join() crash.

Handlebars does not have this problem because they don't have to join : https://github.com/handlebars-lang/handlebars.js/blob/785a63e0a84bc5d6617fd1619baa975f1380ddbd/lib/handlebars/helpers/each.js#L80

This solution works, for example:

image

ericalli commented 1 year ago

I submitted PR #15 with the aforementioned fix and tests.