reduce-precision 1.2.0 → 1.3.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/README.md +86 -4
- package/package.json +6 -2
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@
|
|
|
20
20
|
- Intelligent handling of very small and very large numbers
|
|
21
21
|
- Automatic thousand separators and decimal points based on the selected language
|
|
22
22
|
- TypeScript type definitions included
|
|
23
|
+
- Optional inline Toman SVG and structured parts for custom UI rendering (JavaScript/TypeScript)
|
|
23
24
|
|
|
24
25
|
## Installation
|
|
25
26
|
|
|
@@ -70,7 +71,7 @@ echo $formatter->toString(12345.678); // Default format
|
|
|
70
71
|
|
|
71
72
|
## Options
|
|
72
73
|
|
|
73
|
-
The `
|
|
74
|
+
The JavaScript/TypeScript `NumberFormatter` constructor accepts an optional `options` object with the following properties:
|
|
74
75
|
|
|
75
76
|
| Option | Type | Default | Description |
|
|
76
77
|
| --------------- | ---------------------------------------------------------- | ---------- | ----------------------------------------------------- |
|
|
@@ -82,6 +83,7 @@ The `format` function accepts an optional `options` object with the following pr
|
|
|
82
83
|
| `postfixMarker` | `string` | `'i'` | Postfix marker for HTML and Markdown output |
|
|
83
84
|
| `prefix` | `string` | `''` | Prefix string to be added before the formatted number |
|
|
84
85
|
| `postfix` | `string` | `''` | Postfix string to be added after the formatted number |
|
|
86
|
+
| `currencySymbol` | `'text'` \| `'svg'` | `'text'` | Toman symbol style for HTML output (JavaScript/TypeScript) |
|
|
85
87
|
|
|
86
88
|
## Examples
|
|
87
89
|
|
|
@@ -189,6 +191,88 @@ Formats the input number as an HTML string.
|
|
|
189
191
|
|
|
190
192
|
Formats the input number as a Markdown string.
|
|
191
193
|
|
|
194
|
+
## Toman SVG (TypeScript / JavaScript)
|
|
195
|
+
|
|
196
|
+
Opt in to the bundled icon for HTML output:
|
|
197
|
+
|
|
198
|
+
```ts
|
|
199
|
+
import { NumberFormatter, tomanSymbolSvg } from 'reduce-precision';
|
|
200
|
+
|
|
201
|
+
const formatter = new NumberFormatter({
|
|
202
|
+
template: 'irt',
|
|
203
|
+
currencySymbol: 'svg', // default: 'text'
|
|
204
|
+
}).setLanguage('fa');
|
|
205
|
+
|
|
206
|
+
formatter.toHtmlString(12500); // localized amount with inline Toman SVG
|
|
207
|
+
formatter.toPlainString(12500); // existing text representation
|
|
208
|
+
const parts = formatter.formatToParts(12500);
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
Plain text, Markdown, other templates, and the existing JSON contract retain their
|
|
212
|
+
behavior. `toString()` follows the selected output format, as before. The icon uses
|
|
213
|
+
`currentColor`, a `1em` size, an accessible Toman label, and `.rp-currency-symbol` for styling.
|
|
214
|
+
SVG output escapes custom affix text. HTML markers support `i`, `b`, `em`,
|
|
215
|
+
`strong`, `span`, `small`, `sup`, and `sub`; other markers fall back to `span`
|
|
216
|
+
when SVG output is enabled.
|
|
217
|
+
|
|
218
|
+
`formatToParts()` returns `{ type, value }` objects. IRT parts separate `sign`,
|
|
219
|
+
`prefix`, `number`, `compact`, `currency`, `postfix`, and spacing (`literal`).
|
|
220
|
+
Render text parts as text nodes and replace the currency part with the exported
|
|
221
|
+
`tomanSymbolSvg` or your framework component. Compact parts explicitly separate
|
|
222
|
+
scale and currency (for example, `هزار میلیارد` and `ت` instead of `همت`), so joining
|
|
223
|
+
parts may differ from legacy plain output. Other templates currently return a
|
|
224
|
+
single `literal` part. Invalid/empty input returns an empty array.
|
|
225
|
+
|
|
226
|
+
This feature is currently available in the JavaScript/TypeScript implementation;
|
|
227
|
+
the PHP implementation is unchanged.
|
|
228
|
+
|
|
229
|
+
### Render parts in a browser
|
|
230
|
+
|
|
231
|
+
```ts
|
|
232
|
+
const output = document.querySelector('#price')!;
|
|
233
|
+
output.replaceChildren();
|
|
234
|
+
|
|
235
|
+
for (const part of formatter.formatToParts(12500)) {
|
|
236
|
+
if (part.type === 'currency') {
|
|
237
|
+
// Parse only the bundled SVG, never user-provided text.
|
|
238
|
+
const icon = new DOMParser()
|
|
239
|
+
.parseFromString(tomanSymbolSvg, 'image/svg+xml').documentElement;
|
|
240
|
+
output.appendChild(document.importNode(icon, true));
|
|
241
|
+
} else {
|
|
242
|
+
output.appendChild(document.createTextNode(part.value));
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
The exported `FormatPart` TypeScript type describes each part. Calling
|
|
248
|
+
`formatToParts()` does not change the formatter's selected output mode.
|
|
249
|
+
|
|
250
|
+
## Local demo
|
|
251
|
+
|
|
252
|
+
From a checkout of this repository:
|
|
253
|
+
|
|
254
|
+
```bash
|
|
255
|
+
npm ci
|
|
256
|
+
npm run dev
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
Open the local URL printed by Vite. Select **Toman**, choose **Text** or **SVG icon**,
|
|
260
|
+
and switch between **HTML**, **Plain**, **Markdown**, and **Parts preview**.
|
|
261
|
+
The demo displays the rendered result, raw output, and formatted object or parts.
|
|
262
|
+
Plain and Markdown remain textual even when SVG is selected.
|
|
263
|
+
|
|
264
|
+
Build the package and demo:
|
|
265
|
+
|
|
266
|
+
```bash
|
|
267
|
+
npm run typecheck
|
|
268
|
+
npm test -- --runInBand
|
|
269
|
+
npm run build
|
|
270
|
+
npm run demo:build
|
|
271
|
+
npm run demo:preview
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
The demo build is written to `demo-dist/` and is excluded from Git.
|
|
275
|
+
|
|
192
276
|
## Testing
|
|
193
277
|
|
|
194
278
|
### Node.js / TypeScript
|
|
@@ -200,7 +284,7 @@ You can run tests using Jest or any other preferred testing framework for TypeSc
|
|
|
200
284
|
You can run tests using PHPUnit:
|
|
201
285
|
|
|
202
286
|
```bash
|
|
203
|
-
./vendor/bin/phpunit tests
|
|
287
|
+
./vendor/bin/phpunit php/tests/NumberFormatterTest.php
|
|
204
288
|
```
|
|
205
289
|
|
|
206
290
|
## Contributing
|
|
@@ -210,5 +294,3 @@ Contributions are welcome! If you find a bug or have a feature request, please o
|
|
|
210
294
|
## License
|
|
211
295
|
|
|
212
296
|
This project is licensed under the [MIT License](LICENSE).
|
|
213
|
-
|
|
214
|
-
---
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reduce-precision",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "./ts/lib/index.js",
|
|
5
|
+
"main": "./ts/lib/src/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"ts/lib/**/*"
|
|
8
8
|
],
|
|
9
9
|
"scripts": {
|
|
10
|
+
"dev": "vite --config demo/vite.config.mjs",
|
|
11
|
+
"demo:build": "vite build --config demo/vite.config.mjs",
|
|
12
|
+
"demo:preview": "vite preview --config demo/vite.config.mjs",
|
|
10
13
|
"build": "npm run build:tsc && npm run build:webpack",
|
|
11
14
|
"build:tsc": "tsc --project tsconfig.json",
|
|
12
15
|
"build:webpack": "webpack --config webpack.config.js",
|
|
@@ -49,6 +52,7 @@
|
|
|
49
52
|
"ts-loader": "^9.5.2",
|
|
50
53
|
"ts-node": "^10.9.2",
|
|
51
54
|
"typescript": "^5.9.2",
|
|
55
|
+
"vite": "^8.3.0",
|
|
52
56
|
"webpack": "^5.101.0",
|
|
53
57
|
"webpack-cli": "^6.0.1"
|
|
54
58
|
},
|