@utiliread/primitives 1.0.0-8 → 1.0.0-80

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.
Files changed (162) hide show
  1. package/dist/cjs/aggregate-id.js +2 -2
  2. package/dist/cjs/consumption-type-id.js +2 -2
  3. package/dist/cjs/index.js +21 -9
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/cjs/measure-id.js +3 -0
  6. package/dist/cjs/measure-id.js.map +1 -0
  7. package/dist/cjs/number-formatter.js +79 -50
  8. package/dist/cjs/number-formatter.js.map +1 -1
  9. package/dist/cjs/number-formatter.spec.js +512 -0
  10. package/dist/cjs/number-formatter.spec.js.map +1 -0
  11. package/dist/cjs/optional.js +3 -0
  12. package/dist/cjs/optional.js.map +1 -0
  13. package/dist/cjs/precision-number.js +42 -0
  14. package/dist/cjs/precision-number.js.map +1 -0
  15. package/dist/cjs/precision-number.spec.js +27 -0
  16. package/dist/cjs/precision-number.spec.js.map +1 -0
  17. package/dist/cjs/register-convention.js +58 -0
  18. package/dist/cjs/register-convention.js.map +1 -0
  19. package/dist/cjs/register-convention.spec.js +39 -0
  20. package/dist/cjs/register-convention.spec.js.map +1 -0
  21. package/dist/cjs/register-definition.js +2 -2
  22. package/dist/cjs/register-id.js +2 -2
  23. package/dist/cjs/register-library.js +52 -41
  24. package/dist/cjs/register-library.js.map +1 -1
  25. package/dist/cjs/register-library.spec.js +36 -29
  26. package/dist/cjs/register-library.spec.js.map +1 -1
  27. package/dist/cjs/register-pack.js +2 -2
  28. package/dist/cjs/register-utils.js +8 -0
  29. package/dist/cjs/register-utils.js.map +1 -0
  30. package/dist/cjs/repository.js +26 -0
  31. package/dist/cjs/repository.js.map +1 -0
  32. package/dist/cjs/repository.spec.js +21 -0
  33. package/dist/cjs/repository.spec.js.map +1 -0
  34. package/dist/cjs/step.js +45 -0
  35. package/dist/cjs/step.js.map +1 -0
  36. package/dist/cjs/step.spec.js +29 -0
  37. package/dist/cjs/step.spec.js.map +1 -0
  38. package/dist/cjs/time-or-rule.js +19 -0
  39. package/dist/cjs/time-or-rule.js.map +1 -0
  40. package/dist/cjs/unit-id.js +13 -13
  41. package/dist/cjs/unit-id.js.map +1 -1
  42. package/dist/esm/aggregate-id.d.ts +1 -1
  43. package/dist/esm/aggregate-id.js +1 -1
  44. package/dist/esm/consumption-type-id.d.ts +1 -1
  45. package/dist/esm/consumption-type-id.js +1 -1
  46. package/dist/esm/index.d.ts +16 -8
  47. package/dist/esm/index.js +9 -4
  48. package/dist/esm/index.js.map +1 -1
  49. package/dist/esm/measure-id.d.ts +1 -0
  50. package/dist/esm/measure-id.js +2 -0
  51. package/dist/esm/measure-id.js.map +1 -0
  52. package/dist/esm/number-formatter.d.ts +17 -8
  53. package/dist/esm/number-formatter.js +76 -47
  54. package/dist/esm/number-formatter.js.map +1 -1
  55. package/dist/esm/{register-converter.spec.d.ts → number-formatter.spec.d.ts} +1 -1
  56. package/dist/esm/number-formatter.spec.js +510 -0
  57. package/dist/esm/number-formatter.spec.js.map +1 -0
  58. package/dist/esm/optional.d.ts +1 -0
  59. package/dist/esm/optional.js +2 -0
  60. package/dist/esm/optional.js.map +1 -0
  61. package/dist/esm/precision-number.d.ts +11 -0
  62. package/dist/esm/precision-number.js +38 -0
  63. package/dist/esm/precision-number.js.map +1 -0
  64. package/dist/esm/precision-number.spec.d.ts +1 -0
  65. package/dist/esm/precision-number.spec.js +25 -0
  66. package/dist/esm/precision-number.spec.js.map +1 -0
  67. package/dist/esm/register-convention.d.ts +14 -0
  68. package/dist/esm/register-convention.js +54 -0
  69. package/dist/esm/register-convention.js.map +1 -0
  70. package/dist/esm/register-convention.spec.d.ts +1 -0
  71. package/dist/esm/register-convention.spec.js +37 -0
  72. package/dist/esm/register-convention.spec.js.map +1 -0
  73. package/dist/esm/register-definition.d.ts +13 -9
  74. package/dist/esm/register-definition.js +1 -1
  75. package/dist/esm/register-id.d.ts +1 -1
  76. package/dist/esm/register-id.js +1 -1
  77. package/dist/esm/register-library.d.ts +15 -11
  78. package/dist/esm/register-library.js +49 -38
  79. package/dist/esm/register-library.js.map +1 -1
  80. package/dist/esm/register-library.spec.d.ts +1 -1
  81. package/dist/esm/register-library.spec.js +34 -27
  82. package/dist/esm/register-library.spec.js.map +1 -1
  83. package/dist/esm/register-pack.d.ts +4 -4
  84. package/dist/esm/register-pack.js +1 -1
  85. package/dist/esm/register-utils.d.ts +3 -0
  86. package/dist/esm/register-utils.js +4 -0
  87. package/dist/esm/register-utils.js.map +1 -0
  88. package/dist/esm/repository.d.ts +9 -0
  89. package/dist/esm/repository.js +22 -0
  90. package/dist/esm/repository.js.map +1 -0
  91. package/dist/esm/repository.spec.d.ts +1 -0
  92. package/dist/esm/repository.spec.js +19 -0
  93. package/dist/esm/repository.spec.js.map +1 -0
  94. package/dist/esm/step.d.ts +15 -0
  95. package/dist/esm/step.js +42 -0
  96. package/dist/esm/step.js.map +1 -0
  97. package/dist/esm/step.spec.d.ts +1 -0
  98. package/dist/esm/step.spec.js +27 -0
  99. package/dist/esm/step.spec.js.map +1 -0
  100. package/dist/esm/time-or-rule.d.ts +7 -0
  101. package/dist/esm/time-or-rule.js +16 -0
  102. package/dist/esm/time-or-rule.js.map +1 -0
  103. package/dist/esm/unit-id.d.ts +2 -2
  104. package/dist/esm/unit-id.js +10 -10
  105. package/dist/esm/unit-id.js.map +1 -1
  106. package/locales/da/aggregates.json +8 -0
  107. package/locales/da/consumption-types.json +7 -0
  108. package/locales/da/measures.json +16 -0
  109. package/locales/da/registers.json +371 -0
  110. package/locales/da/units.json +93 -0
  111. package/locales/en/aggregates.json +8 -0
  112. package/locales/en/consumption-types.json +7 -0
  113. package/locales/en/measures.json +16 -0
  114. package/locales/en/registers.json +380 -0
  115. package/locales/en/units.json +93 -0
  116. package/package.json +17 -14
  117. package/src/aggregate-id.ts +9 -9
  118. package/src/consumption-type-id.ts +8 -8
  119. package/src/index.ts +39 -17
  120. package/src/measure-id.ts +15 -0
  121. package/src/number-formatter.spec.ts +549 -77
  122. package/src/number-formatter.ts +118 -50
  123. package/src/optional.ts +1 -0
  124. package/src/precision-number.spec.ts +28 -0
  125. package/src/precision-number.ts +44 -0
  126. package/src/register-convention.spec.ts +52 -29
  127. package/src/register-convention.ts +70 -45
  128. package/src/register-definition.ts +23 -14
  129. package/src/register-id.ts +270 -191
  130. package/src/register-library.spec.ts +41 -33
  131. package/src/register-library.ts +63 -37
  132. package/src/register-pack.ts +5 -5
  133. package/src/register-utils.ts +6 -6
  134. package/src/repository.spec.ts +20 -0
  135. package/src/repository.ts +24 -0
  136. package/src/step.spec.ts +37 -0
  137. package/src/step.ts +45 -0
  138. package/src/time-or-rule.ts +17 -0
  139. package/src/unit-id.ts +110 -100
  140. package/tsconfig.cjs.json +7 -8
  141. package/tsconfig.json +16 -14
  142. package/web-test-runner.config.mjs +7 -0
  143. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/base.css +0 -224
  144. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/block-navigation.js +0 -79
  145. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/favicon.png +0 -0
  146. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/index.html +0 -96
  147. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.css +0 -1
  148. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.js +0 -2
  149. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sort-arrow-sprite.png +0 -0
  150. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sorter.js +0 -170
  151. package/dist/cjs/register-converter.js +0 -47
  152. package/dist/cjs/register-converter.js.map +0 -1
  153. package/dist/cjs/register-converter.spec.js +0 -29
  154. package/dist/cjs/register-converter.spec.js.map +0 -1
  155. package/dist/esm/register-converter.d.ts +0 -9
  156. package/dist/esm/register-converter.js +0 -44
  157. package/dist/esm/register-converter.js.map +0 -1
  158. package/dist/esm/register-converter.spec.js +0 -27
  159. package/dist/esm/register-converter.spec.js.map +0 -1
  160. package/karma.config.js +0 -32
  161. package/src/register-converter.spec.ts +0 -31
  162. package/src/register-converter.ts +0 -29
@@ -1,77 +1,549 @@
1
- import { expect } from 'chai';
2
- import { NumberFormatter } from './number-formatter';
3
-
4
- const localeDecimalSeparator = (0.1).toLocaleString()[1];
5
-
6
- describe("toLocalePrecision", () => {
7
- it("should format correctly", () => {
8
- const data: [number, number, string][] = [
9
- [77.1274, 1, "8e+1"],
10
- [77.1274, 2, "77"],
11
- [77.1274, 4, "77.13"],
12
- [77.1274, 5, "77.127"],
13
- [77.1274, 6, "77.1274"],
14
- [77.1274, 7, "77.12740"],
15
- [77.1274, 15, "77.1274000000000"],
16
- [77.1274, 16, "77.12739999999999"],
17
- [77.1274, 17, "77.127399999999994"],
18
- [77.1274, 18, "77.1273999999999944"],
19
- [77.1274, 19, "77.12739999999999441"],
20
- [77.1274, 20, "77.127399999999994407"],
21
- [77.1274, 21, "77.1273999999999944066"],
22
- [0.00000123, 2, "0.0000012"],
23
- [0.000000123, 2, "1.2e-7"],
24
- [0.00000123, 21, "0.00000123000000000000008198"],
25
- [123456, 3, "1.23e+5"],
26
- [Number.MAX_VALUE, 3, "1.80e+308"],
27
- [123456, 15, "123456.000000000"],
28
- [0, 5, "0.0000"],
29
- [0.000001, 1, "0.000001"],
30
- [0.000001, 10, "0.000001000000000"],
31
- [0.0000001, 10, "1.000000000e-7"],
32
- [0.00000000001, 1, "1e-11"],
33
- [0.00000000001, 2, "1.0e-11"],
34
- [55, 2, "55"],
35
- [55, 1, "6e+1"],
36
- [-55, 2, "-55"],
37
- [-55, 1, "-6e+1"],
38
- [9.59, 1, "1e+1"],
39
- [9.59, 2, "10"],
40
- [9.95, 2, "10"],
41
- [9.96, 2, "10"],
42
- [-555555555555555555555, 1, "-6e+20"],
43
- [-5555555555555555555555, 1, "-6e+21"],
44
- [18014398509482012, 21, "18014398509482012.0000"],
45
- [180143985094820121234, 21, "180143985094820134912"],
46
- [0.1, 21, "0.100000000000000005551"],
47
- [Number.EPSILON, 2, "2.2e-16"],
48
- [Number.EPSILON, 21, "2.22044604925031308085e-16"],
49
- [Number.NaN, 3, "NaN"],
50
- [Number.POSITIVE_INFINITY, 3, "Infinity"],
51
- [Number.NEGATIVE_INFINITY, 3, "-Infinity"],
52
- ];
53
-
54
- for (const [value, precision, expected] of data) {
55
- expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(expected.replace(".", localeDecimalSeparator));
56
- }
57
- })
58
- })
59
-
60
- describe("toLocaleFixed", () => {
61
- it("should format correctly", () => {
62
- const data: [number, number, string][] = [
63
- [10, 1, "10"],
64
- [10, 2, "10"],
65
- [10, 3, "10.0"],
66
- [10, 4, "10.00"],
67
- [10.2, 1, "10"],
68
- [10.2, 2, "10"],
69
- [10.2, 3, "10.2"],
70
- [10.2, 4, "10.20"],
71
- ];
72
-
73
- for (const [value, precision, expected] of data) {
74
- expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(expected.replace(".", localeDecimalSeparator));
75
- }
76
- })
77
- })
1
+ import {
2
+ NumberFormatter,
3
+ Parts,
4
+ invariantDecimalSeparator,
5
+ localeDecimalSeparator,
6
+ } from "./number-formatter";
7
+
8
+ import { expect } from "@esm-bundle/chai";
9
+
10
+ describe("toParts and toPrecision", () => {
11
+ it("should format correctly", () => {
12
+ const data: [number, number, Parts][] = [
13
+ [
14
+ 0,
15
+ 0,
16
+ {
17
+ invariantPrecisionString: "0",
18
+ invariantCoefficientString: "0",
19
+ exponent: 0,
20
+ noOfCoefficientDecimals: 0,
21
+ },
22
+ ],
23
+ [
24
+ 0.0,
25
+ 1,
26
+ {
27
+ invariantPrecisionString: "0.0",
28
+ invariantCoefficientString: "0.0",
29
+ exponent: 0,
30
+ noOfCoefficientDecimals: 1,
31
+ },
32
+ ],
33
+ [
34
+ 77.1274,
35
+ 1,
36
+ {
37
+ invariantPrecisionString: "8e+1",
38
+ invariantCoefficientString: "8",
39
+ exponent: 1,
40
+ noOfCoefficientDecimals: 0,
41
+ },
42
+ ],
43
+ [
44
+ 77.1274,
45
+ 2,
46
+ {
47
+ invariantPrecisionString: "77",
48
+ invariantCoefficientString: "77",
49
+ exponent: 0,
50
+ noOfCoefficientDecimals: 0,
51
+ },
52
+ ],
53
+ [
54
+ 77.1274,
55
+ 4,
56
+ {
57
+ invariantPrecisionString: "77.13",
58
+ invariantCoefficientString: "77.13",
59
+ exponent: 0,
60
+ noOfCoefficientDecimals: 2,
61
+ },
62
+ ],
63
+ [
64
+ 77.1274,
65
+ 5,
66
+ {
67
+ invariantPrecisionString: "77.127",
68
+ invariantCoefficientString: "77.127",
69
+ exponent: 0,
70
+ noOfCoefficientDecimals: 3,
71
+ },
72
+ ],
73
+ [
74
+ 77.1274,
75
+ 6,
76
+ {
77
+ invariantPrecisionString: "77.1274",
78
+ invariantCoefficientString: "77.1274",
79
+ exponent: 0,
80
+ noOfCoefficientDecimals: 4,
81
+ },
82
+ ],
83
+ [
84
+ 77.1274,
85
+ 7,
86
+ {
87
+ invariantPrecisionString: "77.12740",
88
+ invariantCoefficientString: "77.12740",
89
+ exponent: 0,
90
+ noOfCoefficientDecimals: 5,
91
+ },
92
+ ],
93
+ [
94
+ 77.1274,
95
+ 15,
96
+ {
97
+ invariantPrecisionString: "77.1274000000000",
98
+ invariantCoefficientString: "77.1274000000000",
99
+ exponent: 0,
100
+ noOfCoefficientDecimals: 13,
101
+ },
102
+ ],
103
+ [
104
+ 77.1274,
105
+ 16,
106
+ {
107
+ invariantPrecisionString: "77.12739999999999",
108
+ invariantCoefficientString: "77.12739999999999",
109
+ exponent: 0,
110
+ noOfCoefficientDecimals: 14,
111
+ },
112
+ ],
113
+ [
114
+ 77.1274,
115
+ 17,
116
+ {
117
+ invariantPrecisionString: "77.127399999999994",
118
+ invariantCoefficientString: "77.127399999999994",
119
+ exponent: 0,
120
+ noOfCoefficientDecimals: 15,
121
+ },
122
+ ],
123
+ [
124
+ 77.1274,
125
+ 18,
126
+ {
127
+ invariantPrecisionString: "77.1273999999999944",
128
+ invariantCoefficientString: "77.1273999999999944",
129
+ exponent: 0,
130
+ noOfCoefficientDecimals: 16,
131
+ },
132
+ ],
133
+ [
134
+ 77.1274,
135
+ 19,
136
+ {
137
+ invariantPrecisionString: "77.12739999999999441",
138
+ invariantCoefficientString: "77.12739999999999441",
139
+ exponent: 0,
140
+ noOfCoefficientDecimals: 17,
141
+ },
142
+ ],
143
+ [
144
+ 77.1274,
145
+ 20,
146
+ {
147
+ invariantPrecisionString: "77.127399999999994407",
148
+ invariantCoefficientString: "77.127399999999994407",
149
+ exponent: 0,
150
+ noOfCoefficientDecimals: 18,
151
+ },
152
+ ],
153
+ [
154
+ 77.1274,
155
+ 21,
156
+ {
157
+ invariantPrecisionString: "77.1273999999999944066",
158
+ invariantCoefficientString: "77.1273999999999944066",
159
+ exponent: 0,
160
+ noOfCoefficientDecimals: 19,
161
+ },
162
+ ],
163
+ [
164
+ 0.00000123,
165
+ 2,
166
+ {
167
+ invariantPrecisionString: "0.0000012",
168
+ invariantCoefficientString: "0.0000012",
169
+ exponent: 0,
170
+ noOfCoefficientDecimals: 7,
171
+ },
172
+ ],
173
+ [
174
+ 0.000000123,
175
+ 2,
176
+ {
177
+ invariantPrecisionString: "1.2e-7",
178
+ invariantCoefficientString: "1.2",
179
+ exponent: -7,
180
+ noOfCoefficientDecimals: 1,
181
+ },
182
+ ],
183
+ [
184
+ 0.00000123,
185
+ 21,
186
+ {
187
+ invariantPrecisionString: "0.00000123000000000000008198",
188
+ invariantCoefficientString: "0.00000123000000000000008198",
189
+ exponent: 0,
190
+ noOfCoefficientDecimals: 26,
191
+ },
192
+ ],
193
+ [
194
+ 123456,
195
+ 3,
196
+ {
197
+ invariantPrecisionString: "1.23e+5",
198
+ invariantCoefficientString: "1.23",
199
+ exponent: 5,
200
+ noOfCoefficientDecimals: 2,
201
+ },
202
+ ],
203
+ [
204
+ Number.MAX_VALUE,
205
+ 3,
206
+ {
207
+ invariantPrecisionString: "1.80e+308",
208
+ invariantCoefficientString: "1.80",
209
+ exponent: 308,
210
+ noOfCoefficientDecimals: 2,
211
+ },
212
+ ],
213
+ [
214
+ 123456,
215
+ 15,
216
+ {
217
+ invariantPrecisionString: "123456.000000000",
218
+ invariantCoefficientString: "123456.000000000",
219
+ exponent: 0,
220
+ noOfCoefficientDecimals: 9,
221
+ },
222
+ ],
223
+ [
224
+ 0,
225
+ 5,
226
+ {
227
+ invariantPrecisionString: "0.00000",
228
+ invariantCoefficientString: "0.00000",
229
+ exponent: 0,
230
+ noOfCoefficientDecimals: 5,
231
+ },
232
+ ],
233
+ [
234
+ 0.000001,
235
+ 1,
236
+ {
237
+ invariantPrecisionString: "0.000001",
238
+ invariantCoefficientString: "0.000001",
239
+ exponent: 0,
240
+ noOfCoefficientDecimals: 6,
241
+ },
242
+ ],
243
+ [
244
+ 0.000001,
245
+ 10,
246
+ {
247
+ invariantPrecisionString: "0.000001000000000",
248
+ invariantCoefficientString: "0.000001000000000",
249
+ exponent: 0,
250
+ noOfCoefficientDecimals: 15,
251
+ },
252
+ ],
253
+ [
254
+ 0.0000001,
255
+ 10,
256
+ {
257
+ invariantPrecisionString: "1.000000000e-7",
258
+ invariantCoefficientString: "1.000000000",
259
+ exponent: -7,
260
+ noOfCoefficientDecimals: 9,
261
+ },
262
+ ],
263
+ [
264
+ 0.00000000001,
265
+ 1,
266
+ {
267
+ invariantPrecisionString: "1e-11",
268
+ invariantCoefficientString: "1",
269
+ exponent: -11,
270
+ noOfCoefficientDecimals: 0,
271
+ },
272
+ ],
273
+ [
274
+ 0.00000000001,
275
+ 2,
276
+ {
277
+ invariantPrecisionString: "1.0e-11",
278
+ invariantCoefficientString: "1.0",
279
+ exponent: -11,
280
+ noOfCoefficientDecimals: 1,
281
+ },
282
+ ],
283
+ [
284
+ 55,
285
+ 2,
286
+ {
287
+ invariantPrecisionString: "55",
288
+ invariantCoefficientString: "55",
289
+ exponent: 0,
290
+ noOfCoefficientDecimals: 0,
291
+ },
292
+ ],
293
+ [
294
+ 55,
295
+ 1,
296
+ {
297
+ invariantPrecisionString: "6e+1",
298
+ invariantCoefficientString: "6",
299
+ exponent: 1,
300
+ noOfCoefficientDecimals: 0,
301
+ },
302
+ ],
303
+ [
304
+ -55,
305
+ 2,
306
+ {
307
+ invariantPrecisionString: "-55",
308
+ invariantCoefficientString: "-55",
309
+ exponent: 0,
310
+ noOfCoefficientDecimals: 0,
311
+ },
312
+ ],
313
+ [
314
+ -55,
315
+ 1,
316
+ {
317
+ invariantPrecisionString: "-6e+1",
318
+ invariantCoefficientString: "-6",
319
+ exponent: 1,
320
+ noOfCoefficientDecimals: 0,
321
+ },
322
+ ],
323
+ [
324
+ 9.59,
325
+ 1,
326
+ {
327
+ invariantPrecisionString: "1e+1",
328
+ invariantCoefficientString: "1",
329
+ exponent: 1,
330
+ noOfCoefficientDecimals: 0,
331
+ },
332
+ ],
333
+ [
334
+ 9.59,
335
+ 2,
336
+ {
337
+ invariantPrecisionString: "10",
338
+ invariantCoefficientString: "10",
339
+ exponent: 0,
340
+ noOfCoefficientDecimals: 0,
341
+ },
342
+ ],
343
+ [
344
+ 9.95,
345
+ 2,
346
+ {
347
+ invariantPrecisionString: "10",
348
+ invariantCoefficientString: "10",
349
+ exponent: 0,
350
+ noOfCoefficientDecimals: 0,
351
+ },
352
+ ],
353
+ [
354
+ 9.96,
355
+ 2,
356
+ {
357
+ invariantPrecisionString: "10",
358
+ invariantCoefficientString: "10",
359
+ exponent: 0,
360
+ noOfCoefficientDecimals: 0,
361
+ },
362
+ ],
363
+ [
364
+ -555555555555555555555,
365
+ 1,
366
+ {
367
+ invariantPrecisionString: "-6e+20",
368
+ invariantCoefficientString: "-6",
369
+ exponent: 20,
370
+ noOfCoefficientDecimals: 0,
371
+ },
372
+ ],
373
+ [
374
+ -5555555555555555555555,
375
+ 1,
376
+ {
377
+ invariantPrecisionString: "-6e+21",
378
+ invariantCoefficientString: "-6",
379
+ exponent: 21,
380
+ noOfCoefficientDecimals: 0,
381
+ },
382
+ ],
383
+ [
384
+ 18014398509482012,
385
+ 21,
386
+ {
387
+ invariantPrecisionString: "18014398509482012.0000",
388
+ invariantCoefficientString: "18014398509482012.0000",
389
+ exponent: 0,
390
+ noOfCoefficientDecimals: 4,
391
+ },
392
+ ],
393
+ [
394
+ 180143985094820121234,
395
+ 21,
396
+ {
397
+ invariantPrecisionString: "180143985094820134912",
398
+ invariantCoefficientString: "180143985094820134912",
399
+ exponent: 0,
400
+ noOfCoefficientDecimals: 0,
401
+ },
402
+ ],
403
+ [
404
+ 0.1,
405
+ 21,
406
+ {
407
+ invariantPrecisionString: "0.100000000000000005551",
408
+ invariantCoefficientString: "0.100000000000000005551",
409
+ exponent: 0,
410
+ noOfCoefficientDecimals: 21,
411
+ },
412
+ ],
413
+ [
414
+ Number.EPSILON,
415
+ 2,
416
+ {
417
+ invariantPrecisionString: "2.2e-16",
418
+ invariantCoefficientString: "2.2",
419
+ exponent: -16,
420
+ noOfCoefficientDecimals: 1,
421
+ },
422
+ ],
423
+ [
424
+ Number.EPSILON,
425
+ 21,
426
+ {
427
+ invariantPrecisionString: "2.22044604925031308085e-16",
428
+ invariantCoefficientString: "2.22044604925031308085",
429
+ exponent: -16,
430
+ noOfCoefficientDecimals: 20,
431
+ },
432
+ ],
433
+ [
434
+ Number.NaN,
435
+ 3,
436
+ {
437
+ invariantPrecisionString: "NaN",
438
+ invariantCoefficientString: "NaN",
439
+ exponent: 0,
440
+ noOfCoefficientDecimals: 0,
441
+ },
442
+ ],
443
+ [
444
+ Number.POSITIVE_INFINITY,
445
+ 3,
446
+ {
447
+ invariantPrecisionString: "Infinity",
448
+ invariantCoefficientString: "Infinity",
449
+ exponent: 0,
450
+ noOfCoefficientDecimals: 0,
451
+ },
452
+ ],
453
+ [
454
+ Number.NEGATIVE_INFINITY,
455
+ 3,
456
+ {
457
+ invariantPrecisionString: "-Infinity",
458
+ invariantCoefficientString: "-Infinity",
459
+ exponent: 0,
460
+ noOfCoefficientDecimals: 0,
461
+ },
462
+ ],
463
+ [
464
+ 164735280,
465
+ 7,
466
+ {
467
+ invariantPrecisionString: "1.647353e+8",
468
+ invariantCoefficientString: "1.647353",
469
+ exponent: 8,
470
+ noOfCoefficientDecimals: 6,
471
+ },
472
+ ],
473
+ ];
474
+
475
+ for (const [value, precision, expectedParts] of data) {
476
+ expect(NumberFormatter.toPrecision(value, precision)).to.equal(
477
+ expectedParts.invariantPrecisionString,
478
+ );
479
+
480
+ const localePrecisionString =
481
+ expectedParts.invariantPrecisionString.replace(
482
+ invariantDecimalSeparator,
483
+ localeDecimalSeparator,
484
+ );
485
+ expect(
486
+ NumberFormatter.toLocalePrecision(value, precision),
487
+ ).to.equal(localePrecisionString);
488
+
489
+ const parts = NumberFormatter.getParts(value, precision);
490
+ expect(parts.invariantPrecisionString).to.equal(
491
+ expectedParts.invariantPrecisionString,
492
+ );
493
+ expect(parts.invariantCoefficientString).to.equal(
494
+ expectedParts.invariantCoefficientString,
495
+ );
496
+ expect(parts.exponent).to.equal(expectedParts.exponent);
497
+ expect(parts.noOfCoefficientDecimals).to.equal(
498
+ expectedParts.noOfCoefficientDecimals,
499
+ );
500
+ }
501
+ });
502
+ });
503
+
504
+ describe("toFixed", () => {
505
+ it("should format correctly", () => {
506
+ const data: [number, number, string][] = [
507
+ [0, 0, "0"],
508
+ [0.0, 1, "0.0"],
509
+ [10, 1, "10"],
510
+ [10, 2, "10"],
511
+ [10, 3, "10.0"],
512
+ [10, 4, "10.00"],
513
+ [10.2, 1, "10"],
514
+ [10.2, 2, "10"],
515
+ [10.2, 3, "10.2"],
516
+ [10.2, 4, "10.20"],
517
+ [217887000000, 6, "217887000000"],
518
+ ];
519
+
520
+ for (const [value, precision, invariantExpected] of data) {
521
+ expect(NumberFormatter.toFixed(value, precision)).to.equal(
522
+ invariantExpected,
523
+ );
524
+
525
+ const localeExpected = invariantExpected.replace(
526
+ invariantDecimalSeparator,
527
+ localeDecimalSeparator,
528
+ );
529
+ expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(
530
+ localeExpected,
531
+ );
532
+ }
533
+ });
534
+ });
535
+
536
+ describe("toLocaleFixed", () => {
537
+ it("should format correctly", () => {
538
+ expect(NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
539
+ });
540
+
541
+ it("should format correctly with max decimals", () => {
542
+ expect(NumberFormatter.toLocaleFixed(35.123456, 8, 2)).to.equal(
543
+ "35.12",
544
+ );
545
+ expect(NumberFormatter.toLocaleFixed(35.129999, 8, 2)).to.equal(
546
+ "35.13",
547
+ );
548
+ });
549
+ });