Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Each helper's support for Maps using as | value key | #2030

Open
dylanpiera opened this issue Mar 4, 2024 · 0 comments
Open

Extend Each helper's support for Maps using as | value key | #2030

dylanpiera opened this issue Mar 4, 2024 · 0 comments
Labels

Comments

@dylanpiera
Copy link

Due to the way that the Map handler of the each function is currently done the value is returned as a multi dimensional array.

} else if (isMap(context)) {
const j = context.size;
for (const [key, value] of context) {
execIteration(key, value, i++, i === j);
}

During the execIteration function the Map value getter is now called as such:

blockParams: [context[field], field],

However a Map doesn't support index signatures, thus context[field] === undefined and instead needs to be called using Map#get.

In the case of the following handlebars code w/ appropriate map:

var aMapObject = new Map([["a",true],["b",false],["c",true]]);
{{#each aMapObject}}
   {{log this @key}}
{{/each}}

the result of the log would be ["a",true], 0 while I'd expect this to be true and @key to be "a" like it is when you call each on an Object.

The solution would be to on Line 36 of each.js call context.get(field) instead of context[field]. Of course it'd have to know for sure it was a Map at that point.

So it could be resolved either by adding a property to execIteration of isMap (or perhaps a more generic solution with calling a function on context) to determine whether it should use the index signature or call .get or some other function.

Or alternatively; if context is a Map, transform it into an Object Record instead.


I have already resolved this locally, but thought I'd share this here in case someone wants to pick this up and add it to Handlebars itself.
I resolved this by overwriting the each helper, checking if the context is a map, and if so using context.get(field) instead of context[field]. If it isn't a map, parse it using the original Handlebars each helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants