Skip to content

Commit

Permalink
fix #83
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Nov 19, 2024
1 parent 69dbc54 commit 59cc9ad
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Fixed
- `auto_trim` plugin: Trim comments.
- `set` evaluates the expression twice.
- `for`: Make sure key values of an array are always numbers [#83].

## [1.12.11] - 2024-11-03
### Fixed
Expand Down Expand Up @@ -264,6 +265,7 @@ First version
[#72]: https://github.com/oscarotero/vento/issues/72
[#73]: https://github.com/oscarotero/vento/issues/73
[#74]: https://github.com/oscarotero/vento/issues/74
[#83]: https://github.com/oscarotero/vento/issues/83

[1.12.12]: https://github.com/oscarotero/vento/compare/v1.12.11...HEAD
[1.12.11]: https://github.com/oscarotero/vento/compare/v1.12.10...v1.12.11
Expand Down
4 changes: 3 additions & 1 deletion plugins/for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ function toIterator(
}

if (Array.isArray(item)) {
return withKeys ? Object.entries(item) : item;
return withKeys
? Object.entries(item).map(([key, value]) => [parseInt(key, 10), value])
: item;
}

if (typeof item === "function") {
Expand Down
28 changes: 28 additions & 0 deletions test/for.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ Deno.test("For tag (array)", async () => {
`,
expected: "1(0)-2(1)-3(2)-4(3)-",
});
// Fix for issue https://github.com/ventojs/vento/issues/83
await test({
template: `
{{ for key, name of [0, 1, 2, 3] }}{{key == name}}{{key === name}}{{ /for }}
`,
expected: "truetruetruetruetruetruetruetrue",
});
});

Deno.test("For tag (object)", async () => {
Expand All @@ -72,6 +79,27 @@ Deno.test("For tag (object)", async () => {
`,
expected: "1(one)-2(two)-",
});

await test({
template: `
{{ for name of names }}{{people[name].surname}}{{ /for }}
`,
expected: "OteroRubio",
data: {
names: [
"Óscar",
"Laura",
],
people: {
"Óscar": {
surname: "Otero",
},
"Laura": {
surname: "Rubio",
},
},
},
});
});

Deno.test("For tag (function)", async () => {
Expand Down

0 comments on commit 59cc9ad

Please sign in to comment.