Repository navigation
feat(intl): add wrap to decide what a message renders - #311
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #311 +/- ##
==========================================
+ Coverage 83.62% 83.66% +0.03%
==========================================
Files 228 229 +1
Lines 4849 4860 +11
Branches 1074 1075 +1
==========================================
+ Hits 4055 4066 +11
Misses 553 553
Partials 241 241 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Some messages need code to decide what they render: a template that applies only when every part is present, values changed before they are filled in, a locale-aware change of the result. Today that logic lives in helpers at the call sites, like a
getGardenPlace(t, garden)that renders'{city}, {country}'only when both parts are present and the part alone otherwise. Every call site has to remember the helper, and the message type is declared again by hand next to it.wrapmoves that logic into the scheme, so the message renders the same everywhere:What
wrap(format, fn)works with any format. A value, like the result oftext(), reachesfnas is with the active locale:(message, locale). A message function, like one ofparams,pluralormatch, reaches it as is after the values of its call read from signals and accessors:(values, message, locale).fncalls the message with the values it chooses, changed ones included, or returns something else instead.fncalls it: a guard that skips it neither fills placeholders nor warns about a parameter without a value, and a value returned instead is used as is.WrapArgs<R>andWrapped<R, O>types instead of overloads: with overloads, TypeScript infersunknownfor the message of an inline call with arguments, likedatetime({ dateStyle: 'medium' }).format()message is wrapped from the inside,format(wrap(datetime(), fn)), as withcapitalize. Wrapped from the outside,messages()would take it for a translated message and warn that it is missing, since it tells the two apart by the arity of the format.nano-kit-intlskill and its regeneratedDOCS.md.Size
@nano_kit/intlAll publics (Gzip)@nano_kit/intlAll publics (Brotli)Minimal setandBasic setare unchanged. Importingwrapnext to{ intl, text, params }adds 78 B gzip.Verification
oxlint,tsc --noEmitandvitestare clean in@nano_kit/intl(144 tests, 11 of them forwrap) andsize-limitpasses. The pre-pushpnpm testpasses across the monorepo,pnpm skills:docs --checkpasses and the website builds. Every example of the new docs section was compiled and run against the package.