You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 617b8d2
Browse filesBrowse the repository at this point in the historyBrowse files
Add formatters for booleans, bytes, enums and public keys (#1217)
This PR adds formatters for the remaining leaf values of decoded nodes, and makes every formatter share the same `(decoded, options)` signature.
```ts
formatBoolean(decoded); // "true"
formatBytes(decoded); // "0x0102", whatever encoding the bytes were decoded with
formatEnum(decoded); // the variant's label, otherwise its identifier in title case, e.g. "Move To"
formatPublicKey(decoded, { formatAddress: address => names.get(address) ?? address }); // "USDC"
```
- **Addresses:** the new `formatAddress` option formats addresses however the caller needs, e.g. to name them from a trusted token list or address book, or to truncate them. It defaults to the address itself.
- **Uniform signatures:** every formatter now accepts `FormatOptions`, even those that use none of them yet, so any formatter can stand in for another.
- **Invalid ticks:** `formatDateTime` and `formatDuration` now throw the new `CODAMA_ERROR__INVALID_TICKS_PER_SECOND` when `ticksPerSecond` is not a positive integer, instead of returning `null`. `formatInteger` is now the only formatter returning `null`, for amounts whose `decimals` cannot be resolved.
@@ -97,15 +101,19 @@ Numbers are formatted exactly, including 128-bit integers and binary fixed point
97
101
98
102
-**Units:** the unit of a display node wins over the unit of a type, which is the fallback whenever the former is absent or cannot be resolved. Units follow their value after a space, except `%`, `‰` and `°`.
99
103
-**Amounts:** when the `decimals` of an `amountNumberDisplayNode` cannot be resolved, `formatInteger` returns `null` rather than a wrongly scaled amount, so you can present the raw value instead.
100
-
-**Ticks:**`formatDateTime` and `formatDuration` return `null` when `ticksPerSecond` is not a positive integer.
101
-
102
-
`formatInteger`, `formatFloat` and `formatFixedPoint` accept options:
|`formatUnit`|`(value: string, unit: string) => string`| Place a unit next to a value, e.g. `` (value, unit) => `${unit} ${value}` `` for `"USD 40.5"`. Defaults to `formatUnit`, also exported. |
107
-
|`numberFormat`|`Intl.NumberFormat`| Format numbers for a locale, e.g. `"1,234,567.89"`. Its options decide the digits shown. Amounts and fixed points are given to it exactly, never through a JavaScript float. |
108
-
|`resolveInjectedValue`|`(path: NodePath<InjectedValueNode>) => unknown`| Resolve injected values of display nodes, e.g. `decimals` provided by an instruction, from their path through the decoded node. `undefined` means unresolved. |
104
+
-**Ticks:**`formatDateTime` and `formatDuration` throw `CODAMA_ERROR__INVALID_TICKS_PER_SECOND` when `ticksPerSecond` is not a positive integer.
105
+
-**Bytes:** bytes are written in hexadecimal whatever the encoding they were decoded with. Bytes decoded as `utf8` cannot be recovered exactly when they are not valid UTF-8, so decode them with another encoding.
106
+
-**Enums:** the label of the variant's display node, or the variant's identifier in title case otherwise. The data of the variant is not included.
107
+
-**Public keys:** formatted by the `formatAddress` option, e.g. to name or truncate them, or written as the address otherwise.
108
+
109
+
All formatters share the `(decoded, options)` signature and accept the following options, though each only uses those relevant to it, e.g. `formatAddress` for public keys and `numberFormat` for numbers:
|`formatUnit`|`(value: string, unit: string) => string`| Place a unit next to a value, e.g. `` (value, unit) => `${unit} ${value}` `` for `"USD 40.5"`. Defaults to `formatUnit`, also exported. |
114
+
|`formatAddress`|`(address: Address) => string`| Format an address, e.g. to name it from sources you trust such as a token list or an address book, or to truncate it: `address => names.get(address) ?? address`. Defaults to the address itself. |
115
+
|`numberFormat`|`Intl.NumberFormat`| Format numbers for a locale, e.g. `"1,234,567.89"`. Its options decide the digits shown. Amounts and fixed points are given to it exactly, never through a JavaScript float. |
116
+
|`resolveInjectedValue`|`(path: NodePath<InjectedValueNode>) => unknown`| Resolve injected values of display nodes, e.g. `decimals` provided by an instruction, from their path through the decoded node. `undefined` means unresolved. |
0 commit comments