Skip to content

Iterate strings by code point in for loops - #190

Merged
oscarotero merged 1 commit into
ventojs:mainfrom
Shaisolaris:for-string-code-points
Oct 13, 2026
Merged

oscarotero merged 1 commit into
ventojs:mainfrom
Shaisolaris:for-string-code-points

Conversation

@Shaisolaris

Copy link
Copy Markdown
Contributor

{{ for c of "..." }} turns a string into items with item.split(""), which splits by UTF-16 code unit. Characters outside the BMP (emoji, many CJK extension characters) come out as two lone surrogates:

{{ for c of "a馃榾b" }}{{ c }}-{{ /for }}

renders a-\ud83d-\ude00-b- (two broken characters) instead of a-馃榾-b-. Keys from for key, c of ... are shifted the same way. A String object already iterates by code point because it goes through the Symbol.iterator branch, so primitives and objects behave differently today.

This uses Array.from(item), which iterates the way JavaScript's own for...of does. ASCII and other BMP strings produce the same items and keys as before.

Tests: two cases added to "For tag (string)" in test/for.test.ts. They fail on main and pass with the change. deno test --allow-read test gives 105 passed, 0 failed (3 ignored), and deno fmt --check and deno lint are clean. I also added an Unreleased entry to CHANGELOG.md; happy to drop it if you prefer to write those yourself.

@oscarotero
oscarotero merged commit 5ceabb8 into ventojs:main Oct 13, 2026
1 check passed
@oscarotero

Copy link
Copy Markdown
Member

I knew this limitation but didn't know that Array.from() could fix it. That's great, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants