feat(premium-analytics): port formatters package from next-woocommerce-analytics#49226
Conversation
…package convention
|
Are you an Automattician? Please test your changes on all WordPress.com environments to help mitigate accidental explosions.
Interested in more tips and information?
|
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 Follow this PR Review Process:
If you have questions about anything, reach out in #jetpack-developers for guidance! |
…and-typescript-for-internal-packages' into wooa7s-1313-integrate-formatters-package-into-analytics
dognose24
left a comment
There was a problem hiding this comment.
Walked through local verification (Node 24.14.0, branch tip):
pnpm typecheck— exit 0pnpm build— formatters transpiled, full build cleanpnpm exec eslint --flag v10_config_lookup_from_file --max-warnings=0 packages/formatters/— exit 0- Smoke import (
formatMetricValue+formatDatefromroutes/dashboard/stage.tsx) — typecheck passes, the@jetpack-premium-analytics/*alias resolves end-to-end
One coordination note: this PR and #49221 both add projects/packages/premium-analytics/eslint.config.mjs as a new file with mutually exclusive files globs (packages/datetime/** vs packages/formatters/**). Whichever lands second will need to merge both rule sets into one config. Worth coordinating with @chihsuan on merge order.
Two small doc nits inline. Otherwise LGTM. Disclosure: downstream consumer of these formatters via WOOA7S-1329 (widget metric display in dashboard route needs exactly this).
Co-authored-by: Dognose <dognose24@users.noreply.github.com>
Co-authored-by: Dognose <dognose24@users.noreply.github.com>
Proposed changes
Second leaf in M2 — Shared Packages Integration (WOOA7S-1311): port
@wc-analytics/formatters(formerly@next-woo-analytics/formatters) into@automattic/jetpack-premium-analyticsas an internal package. Provides locale-aware number / currency / percentage formatting (formatMetricValue) on top of@automattic/number-formatters, plus thin date helpers (formatDate,formatDateRange) on top ofdate-fns.Chosen as another leaf because it has no internal cross-package imports — only
@automattic/number-formatters(already a Jetpack workspace package) anddate-fns. Stacked on #49189 (which sets up the@jetpack-premium-analytics/*tsconfig paths alias this work relies on), parallel to #49221 (datetime port — same patterns).What's in the package
src/date/format-date.tsformatDate( date, format? )with 11 named presets (short,medium,long,full,iso, etc.)src/date/format-date-range.tsformatDateRange( { from, to } )— collapses ranges by same-day / same-month / same-yearsrc/metric/format-metric-value.tsformatMetricValue( value, type, options )— type-routed orchestrator (number/currency/percentage/average) over@automattic/number-formattersMonorepo adaptations
name: @wc-analytics/formattersname: @automattic/jetpack-premium-analytics-formattersREADME.md— the import specifier (@jetpack-premium-analytics/formatters) comes fromwpPlugin.packageNamespace; thename:field is separate and must use the@automattic/...formversion: "1.0.0"version: "0.1.0"init,datetime); these aren't published so version is informational"@automattic/number-formatters": "*""workspace:*""date-fns": "^4.1.0""4.1.0"minimumReleaseAge: 1440which conflicts with caret rangestsconfig.jsonincludes: [packages/**/*]; siblingpackages/init/has no leaf tsconfig eitherformat-date.tsJSDocMirrors DateRange from @next-woo-analytics/datetimecomment informat-date-range.ts@jetpack-premium-analytics/datetimecode: 'USD'unused destructured param in metric test mock@typescript-eslint/no-unused-vars; the mock never consumed it, thecode?: stringfield in the param type stays so call sites still type-checkimportgroups in testsimport/orderrule disallows blank lines inside import groupspnpm exec prettier --writeTwo structural files added at the parent level:
package.json: adds@automattic/number-formatters(workspace) anddate-fnsso the leaf's imports resolve.packages/formatters/is not a pnpm workspace member (the rootpnpm-workspace.yamlglobprojects/*/*doesn't reach in two levels), so the leaf's own dep declarations are decorative — only the parent's matter to pnpm. This mirrors howpackages/init/and the datetime port in feat(premium-analytics): port datetime package from next-woocommerce-analytics #49221 declare deps. Also adds@types/jest(devDep) so the ported test files typecheck.eslint.config.mjs(new — temporary): softensjsdoc/require-*forpackages/formatters/**so the initial port can land with the upstream JSDoc style (function-body descriptions, no per-param tags). The plan is to backfill proper JSDoc on the helpers in a follow-up and delete this config file entirely. Same pattern as feat(premium-analytics): port datetime package from next-woocommerce-analytics #49221.What's intentionally not here
link:dep on the parent yet. Same reason as feat(premium-analytics): port datetime package from next-woocommerce-analytics #49221 —eslint-plugin-package-json'svalid-dependenciesrule rejectslink:URLs by default. Since no route or sibling package importsformattersyet, the wiring isn't load-bearing —wp-build'spackages/*autodiscovery is enough for the package to exist. The first consumer PR will need to resolve the lint conflict (probably by relaxing the rule for thispackage.json).tsgo --noEmittypecheck (@types/jestis wired), but the parent package has nojest.config.cjsortestscript yet. They'll execute once test infrastructure is wired in a follow-up.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
Requires Node 24 (repo
engineStrict).Optional smoke import, to confirm the path alias works through the typechecker:
routes/dashboard/stage.tsx:pnpm typecheck— resolves and passes.Optional — build-time resolution via
link:(confirms wp-build tracks the package as a module dependency once a consumer imports it):projects/packages/premium-analytics/package.jsondependencies:"@jetpack-premium-analytics/formatters": "link:packages/formatters"(thevalid-dependencieslint rejectslink:— relax it for the temp edit).formattersexport inroutes/dashboard/stage.tsx.pnpm install && pnpm build→build/routes/dashboard/content.min.asset.phplists the dependency undermodule_dependencies.pnpm install.