@watchtowerobservability/activity-render 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/LICENSE +202 -0
- package/README.md +71 -2
- package/dist/format.d.cts +16 -0
- package/dist/format.d.ts +16 -0
- package/dist/index.cjs +336 -0
- package/dist/index.d.cts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +313 -0
- package/dist/vectors.d.cts +33 -0
- package/dist/vectors.d.ts +33 -0
- package/package.json +51 -4
- package/vectors.json +3127 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to `@watchtowerobservability/activity-render` are documented here, in the [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format (`## [x.y.z] - YYYY-MM-DD`; Added, Changed, Fixed, Deprecated, Removed).
|
|
4
|
+
|
|
5
|
+
## Versioning policy
|
|
6
|
+
|
|
7
|
+
This package follows [Semantic Versioning](https://semver.org/). A patch release (`0.y.Z`) fixes bugs and changes nothing else. A minor release adds API surface and may deprecate; while the version is `0.y.z` it may also change behaviour, and every such change is listed under **Changed** with what to do about it. From `1.0.0` a minor release is additive only, and a removal needs a major release, preceded by at least one minor release that lists the API under **Deprecated**. How long a deprecated field or endpoint of the HTTP API keeps working, and the `Deprecation` and `Sunset` response headers: [versioning and deprecation](https://watchtowerops.net/docs/activity-versioning).
|
|
8
|
+
|
|
9
|
+
`CHANGELOG.md` and `vectors.json` are inside every published tarball (a test packs the package and checks).
|
|
10
|
+
|
|
11
|
+
## [0.2.0] - 2026-11-25
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- **`timeZone` is honoured.** `render(event, theme, { timeZone: 'America/New_York' })` prints `{x|date}`, `{x|time}` and `{x|datetime}` as the wall clock of that IANA zone, the same text the server's `display` has for a viewer session minted with `time_zone` (or a read with `time_zone=`). New formatter `time` (`10:15`). Applies to RFC 3339 instants of 1970 to 2037; anything else, no zone, or a zone this runtime does not know prints as written. `Intl` is used only to read the zone's UTC offset; the text is built by the package. New `supportsTimeZone(zone)`.
|
|
16
|
+
- **Conformance vectors ship in the package:** `@watchtowerobservability/activity-render/vectors.json` (`cases[]: { name, event, theme, options, display, verified }`, the same file the server tests run) and `runVectors(render, vectors)`, which returns the cases a renderer gets wrong. Every case is verified against the server's renderer (its test over the shared file passes in CI).
|
|
17
|
+
- `CHANGELOG.md` and `vectors.json` are in the tarball.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- **One published rounding rule, used by the server and this package:** `money` and `count` round the exact binary value of the double half to even (`0.125` is `0.12`, `2.5|count` is `2`), computed with BigInt arithmetic instead of `toFixed`, so every finite double matches. A number placeholder without a formatter prints as the server's JSON prints it (`1e-7`, `1e21`, `-0.0`, whole numbers to 64 bits in full) instead of JavaScript's `String(n)`. The rule and the unavoidable differences (`5.0`, float-literal `1e16`, integers above 2^53) are in the README. Exponent text matches the server (`e+NN`).
|
|
22
|
+
- The vectors file no longer carries one global "unverified" flag: each case says whether the server is known to produce it (`verified`), and `expected` is now `display`.
|
|
23
|
+
- `CHANGELOG.md` follows Keep a Changelog, with the versioning policy at the top.
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- `npm pack` and `npm publish` work after a plain `npm ci` under `NODE_ENV=production` (or `--omit=dev`): `prepack` installs the dev dependencies the build needs from the lockfile first.
|
|
28
|
+
|
|
29
|
+
## [0.1.0] - 2026-11-25
|
|
30
|
+
|
|
31
|
+
### Added
|
|
32
|
+
|
|
33
|
+
- First release: `render(event, theme, { locale, timeZone })`, the renderer of the Activity API's `display=1` in TypeScript with no dependencies. Placeholders and formatters (`money`, `count`, `date`, `datetime`), outcome variants, locales (`pt-BR`, then `pt`), prefix templates, `fallbackTemplate` (`{action.noun}`, `{action.verb}`) and `failure_reason` from the theme's `failureReasons`. Checked against `testdata/activity-render-vectors.json`, which the server's tests read too. The server-side counterpart is `crates/wt-domain-activity` (`render.rs`, `failure.rs`, `templates.rs`); the vectors also cover the open-event fields (`progress`, `abandoned`, `imported`, `detail_ref`), the advice for an abandoned event and the fallback template for an action the theme does not know yet.
|
|
34
|
+
|
|
35
|
+
### Changed
|
|
36
|
+
|
|
37
|
+
- `failure_reason` follows the server: an event that is abandoned (`abandoned: true`, or `ext.abandoned` in a stored document) gets the theme's `abandoned` reason, with no `failureReasonPath` needed; otherwise the path decides as before. Four shared vectors in `testdata/activity-render-vectors.json` cover it.
|
|
38
|
+
|
|
39
|
+
### Fixed
|
|
40
|
+
|
|
41
|
+
- The `.d.cts` declarations are real CommonJS declarations (relative imports end in `.cjs`); `scripts/check-pack.mjs` installs the packed tarball outside the repository and compiles an import under `nodenext`, `node16` and `bundler` with `skipLibCheck: false`.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,72 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @watchtowerobservability/activity-render
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The renderer behind `display=1` of the Watchtower Activity API, client side and dependency free. Give it an event and a theme as the API returns them and it returns the same `display` object the server would, so an app can show an event the moment it creates it, before any round trip.
|
|
4
|
+
|
|
5
|
+
```ts
|
|
6
|
+
import { render } from '@watchtowerobservability/activity-render';
|
|
7
|
+
|
|
8
|
+
const display = render(event, theme, { locale: 'de', timeZone: 'Europe/Berlin' });
|
|
9
|
+
// { title, subtitle?, fields?, failure_reason?: { code, text, action?: { label, url } } }
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
`event` is an Event as `GET .../events` returns it (`action`, `outcome`, `actor`, `target`, `metadata`, ...). `theme` is the effective theme as the feed returns it: only `actions`, `fallbackTemplate`, `failureReasonPath` and `failureReasons` are read (the snake_case spellings are accepted too). Remove what the viewer must not see (metadata, IP) from the event before you call `render`: the server renders after its own hiding.
|
|
13
|
+
|
|
14
|
+
What it does, in this order: the action's template (exact key, else the longest `stem.*` key, else `fallbackTemplate`), the outcome variant (`label_failure`, `label_denied`), the locale copy (`pt-BR`, then `pt`, then the template's own), placeholders and formatters, and for a `failure` outcome the `failureReasons` entry named by the value at `failureReasonPath`.
|
|
15
|
+
|
|
16
|
+
The event's own fields are all available as placeholders, the open-event ones too: `{progress.done}` of `{progress.total}`, `{abandoned}`, `{imported}`, `{label}`, `{detail_ref.bytes|count}`, `{metadata.error_code}`. The `failure_reason` is the advice of the theme's `failureReasons` for the code at `failureReasonPath` (for example `metadata.error_code`), rendered with the same placeholders and locales, `fallbackTemplate` reads a new action as a sentence (`{action.noun}`, `{action.verb}`) until the theme has a template for it.
|
|
17
|
+
|
|
18
|
+
## Time zones
|
|
19
|
+
|
|
20
|
+
`timeZone` is an IANA zone name written as the tz database spells it (`America/New_York`, `Asia/Kolkata`, `UTC`). It is what a viewer session minted with `time_zone` (and a server-key read with `time_zone=`) gives the server, and both sides then print the same text.
|
|
21
|
+
|
|
22
|
+
- `{x|date}` is `2026-11-24`, `{x|time}` is `10:15` and `{x|datetime}` is `2026-11-24 10:15`: the wall clock of the zone, to the minute.
|
|
23
|
+
- The value must be an RFC 3339 timestamp with an offset (`2026-11-24T10:15:30Z`, `...T10:15:30.5+02:00`). An offset in the text is honoured: it names an instant, which is then read in the zone.
|
|
24
|
+
- Text that is not such a timestamp (a date alone, a timestamp without an offset, a date that is not on the calendar, `Tuesday`), a UTC year before 1970 or after 2037 (the tz database has no rule past 2037, so a later instant is not guessed), no `timeZone` at all, or a zone this runtime does not know, prints as written: the first 10 or 16 characters of an ISO date, anything else unchanged. `supportsTimeZone(zone)` says whether the zone is known here.
|
|
25
|
+
- The zone's UTC offset at the instant is read with `Intl.DateTimeFormat(...).formatToParts` and nothing else; the text is built by this package, so output does not depend on the runtime's locale data. Without `Intl` (or without a zone's data) the fallback above applies: the text prints as written, which for the usual `...Z` timestamps is UTC.
|
|
26
|
+
- Both sides use the tz database of their runtime. A zone whose rules changed after one side's database was built (rare) can differ for the instants that moved; the vectors cover New York across both 2026 transitions, Asia/Kolkata (+05:30), Australia/Lord_Howe (30 minute DST) and UTC.
|
|
27
|
+
|
|
28
|
+
## Rounding and number text
|
|
29
|
+
|
|
30
|
+
The server is the reference; this package computes the same text for every finite double, with BigInt arithmetic on the exact value of the double (no `toFixed`).
|
|
31
|
+
|
|
32
|
+
- **`money` and `count`** read the placeholder's text as a number the way Rust's `f64` parser does (an optional sign, digits with an optional `.` and exponent; `inf`, `NaN`, hex, spaces and `1_0` are not numbers and print unchanged; a value that overflows to infinity prints unchanged). They then print the exact decimal expansion of the double rounded to 2 places (`money`; `JPY` has 0) or 0 places (`count`). Rounding is **half to even on the exact binary value**: `0.125` is `0.12`, `0.375` is `0.38`, `2.5|count` is `2`, `3.5|count` is `4`, `0.5|count` is `0`. A value that only looks like a tie in decimal is not one: `1.005` is `1.00`, `2.675` is `2.67`, `8.345` is `8.35`, because those doubles sit below or above the tie.
|
|
33
|
+
- **Sign.** `count` keeps the sign of the double: `-0.4|count` and `-0|count` are `-0`. `money` puts the minus before the symbol only when the value is below zero: `-0.001|money:USD` is `-$0.00` and `-0.0|money:USD` is `$0.00`.
|
|
34
|
+
- **Grouping.** Thousands are separated by `,` on the integer part; `USD`, `EUR`, `GBP` and `JPY` take `$`, `€`, `£` and `¥` before the digits, any other code follows them (`1,234.50 CHF`), no code prints the bare number. Amounts are in major units. A double of any size prints all its digits (`1e21|count` is `1,000,000,000,000,000,000,000`).
|
|
35
|
+
- **A number placeholder without a formatter** prints the way the server's JSON prints it: a whole number up to 64 bits prints its digits (`5`, `10000000000000000`), `-0` is `-0.0`, any other double prints its shortest round-trip digits as the server's JSON library does, with a sign on positive exponents: `0.1`, `1234.5`, `0.0001`, `1e-7`, `1.5e-7`, `1e+21`, `2.5e+20`. The exponent form is used from 1e16 up (for floats) and from 1e-6 down.
|
|
36
|
+
|
|
37
|
+
JSON numbers that cannot match, because JavaScript has lost the text: `5.0` (the server prints `5.0` for a float literal, this package prints `5`), `1e16` as a float literal (server `1e+16`, here `10000000000000000`), integers above 2^53 (`9007199254740993` arrives as `9007199254740992`), and integer literals beyond 64 bits (the server reads them as a double with its own parser and can differ in the last digit). They agree under `money` and `count`, which read the value, not the text. Send a number as a string when its exact text matters.
|
|
38
|
+
|
|
39
|
+
Values are cut at 200 code points after formatting, so the first 200 characters of a very long number are what both print.
|
|
40
|
+
|
|
41
|
+
## Conformance vectors
|
|
42
|
+
|
|
43
|
+
`vectors.json` ships in this package (`@watchtowerobservability/activity-render/vectors.json`) and is the file the server's test suite runs: one source of truth, copied from `testdata/activity-render-vectors.json` by the build, with a test that fails when the two differ.
|
|
44
|
+
|
|
45
|
+
```json
|
|
46
|
+
{ "cases": [ { "name": "...", "event": {}, "theme": {}, "options": { "locale": "de", "timeZone": "America/New_York" }, "display": {}, "verified": true } ] }
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Render `event` with `theme` and `options`; the result, serialized with `JSON.stringify`, must equal `display` (key order and omitted keys count). `verified` is `true` when the server's renderer is known to produce `display` (its unit test passed on it), `false` when the expectation was traced by hand and is awaiting its first server run.
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { runVectors, render } from '@watchtowerobservability/activity-render';
|
|
53
|
+
import vectors from '@watchtowerobservability/activity-render/vectors.json' with { type: 'json' };
|
|
54
|
+
|
|
55
|
+
expect(runVectors(render, vectors)).toEqual([]); // [{ name, expected, actual }] for each case that differs
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`runVectors(yourRender, vectors)` accepts any function with the signature of `render`, so an app that wraps or reimplements the renderer can prove it still conforms.
|
|
59
|
+
|
|
60
|
+
## Parity
|
|
61
|
+
|
|
62
|
+
`testdata/activity-render-vectors.json` in the repository holds the same cases. The Rust renderer (`crates/wt-domain-activity`) and this package both run it; the test here compares `JSON.stringify`, so key order and omitted keys must match too.
|
|
63
|
+
|
|
64
|
+
### Not guaranteed
|
|
65
|
+
|
|
66
|
+
Everything outside the vectors is the same algorithm but is not proven byte for byte. Known differences:
|
|
67
|
+
|
|
68
|
+
- **JSON number text and the 64-bit boundary.** See "Rounding and number text": `5.0`, a float-literal `1e16` and integers above 2^53 reach JavaScript already changed.
|
|
69
|
+
- **Digits.** Rust's `\d` in the ISO date check matches Unicode digits (a date written in Arabic-Indic digits is cut by bytes there and left alone here); this package matches ASCII digits only.
|
|
70
|
+
- **Characters.** Values are cut at 200 Unicode code points in both (a surrogate pair counts once here); control characters are the Unicode `Cc` set in both.
|
|
71
|
+
- **Theme merge.** The server merges the org default theme under the project's. `render` takes the theme you give it: pass the effective theme.
|
|
72
|
+
- **Invalid themes.** The server rejects unknown fields and over-long texts when the theme is saved; `render` does not validate, so a theme that could not be saved renders as best it can.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `format!("{:.dec$}", n)` of Rust for a finite `n >= 0`: the exact decimal expansion of the double, rounded to `dec`
|
|
3
|
+
* places, an exact tie to the even digit. Pure BigInt arithmetic, no `toFixed`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function fixed(n: number, dec: number): string;
|
|
6
|
+
/**
|
|
7
|
+
* The text of a JSON number, as the server prints it. Whole numbers in the range of a 64-bit integer print their digits
|
|
8
|
+
* (`5`, `10000000000000000`); negative zero is `-0.0`; every other double prints as serde_json does (`0.1`, `1e-7`, `1e+21`).
|
|
9
|
+
* A JavaScript number has lost whether the JSON text said `5` or `5.0` (the server prints `5.0` for the second): see README.
|
|
10
|
+
*/
|
|
11
|
+
export declare function numberText(n: number): string;
|
|
12
|
+
export type TimeKind = 'date' | 'datetime' | 'time';
|
|
13
|
+
/** Whether this runtime can convert times to `zone` (an IANA name written as the database spells it, such as `America/New_York`). */
|
|
14
|
+
export declare function supportsTimeZone(zone: string): boolean;
|
|
15
|
+
/** `v` as `kind`: in `zone` when it names a zone this runtime knows and `v` is an RFC 3339 timestamp, else as written. */
|
|
16
|
+
export declare function formatTime(v: string, kind: TimeKind, zone: string | null | undefined): string;
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `format!("{:.dec$}", n)` of Rust for a finite `n >= 0`: the exact decimal expansion of the double, rounded to `dec`
|
|
3
|
+
* places, an exact tie to the even digit. Pure BigInt arithmetic, no `toFixed`.
|
|
4
|
+
*/
|
|
5
|
+
export declare function fixed(n: number, dec: number): string;
|
|
6
|
+
/**
|
|
7
|
+
* The text of a JSON number, as the server prints it. Whole numbers in the range of a 64-bit integer print their digits
|
|
8
|
+
* (`5`, `10000000000000000`); negative zero is `-0.0`; every other double prints as serde_json does (`0.1`, `1e-7`, `1e+21`).
|
|
9
|
+
* A JavaScript number has lost whether the JSON text said `5` or `5.0` (the server prints `5.0` for the second): see README.
|
|
10
|
+
*/
|
|
11
|
+
export declare function numberText(n: number): string;
|
|
12
|
+
export type TimeKind = 'date' | 'datetime' | 'time';
|
|
13
|
+
/** Whether this runtime can convert times to `zone` (an IANA name written as the database spells it, such as `America/New_York`). */
|
|
14
|
+
export declare function supportsTimeZone(zone: string): boolean;
|
|
15
|
+
/** `v` as `kind`: in `zone` when it names a zone this runtime knows and `v` is an RFC 3339 timestamp, else as written. */
|
|
16
|
+
export declare function formatTime(v: string, kind: TimeKind, zone: string | null | undefined): string;
|