@utiliread/primitives 1.0.0-146 → 1.0.0-148

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 (123) hide show
  1. package/dist/index.js +120 -25
  2. package/dist/index.js.map +1 -1
  3. package/dist/index.mjs +120 -25
  4. package/dist/index.mjs.map +1 -1
  5. package/dist/precision-number.d.ts +10 -0
  6. package/package.json +9 -6
  7. package/src/aggregate-id.ts +2 -9
  8. package/src/breakdown-rule.spec.ts +61 -61
  9. package/src/breakdown-rule.ts +58 -58
  10. package/src/csv.spec.ts +42 -42
  11. package/src/csv.ts +8 -9
  12. package/src/cursor-navigation-state.spec.ts +194 -194
  13. package/src/cursor-navigation-state.ts +168 -168
  14. package/src/index.ts +61 -61
  15. package/src/measure-id.ts +17 -17
  16. package/src/measures/duration.ts +11 -11
  17. package/src/measures/energy.ts +13 -13
  18. package/src/measures/humidity.ts +3 -3
  19. package/src/measures/infocode.ts +3 -3
  20. package/src/measures/mass-flow.ts +19 -19
  21. package/src/measures/mass.ts +10 -10
  22. package/src/measures/noise.ts +5 -5
  23. package/src/measures/power.ts +23 -23
  24. package/src/measures/pressure.ts +9 -9
  25. package/src/measures/reactive-energy.ts +7 -7
  26. package/src/measures/reactive-power.ts +3 -3
  27. package/src/measures/temperature.ts +9 -9
  28. package/src/measures/time.ts +3 -3
  29. package/src/measures/volume-flow.ts +25 -25
  30. package/src/measures/volume.ts +3 -3
  31. package/src/medium.spec.ts +12 -12
  32. package/src/medium.ts +47 -47
  33. package/src/number-formatter.spec.ts +570 -549
  34. package/src/number-formatter.ts +126 -118
  35. package/src/optional.ts +1 -1
  36. package/src/precision-number.spec.ts +382 -251
  37. package/src/precision-number.ts +229 -123
  38. package/src/register-convention.spec.ts +52 -52
  39. package/src/register-convention.ts +70 -70
  40. package/src/register-definition.ts +23 -23
  41. package/src/register-id.ts +288 -288
  42. package/src/register-library.spec.ts +41 -41
  43. package/src/register-library.ts +64 -64
  44. package/src/register-pack.ts +5 -5
  45. package/src/register-utils.ts +6 -6
  46. package/src/registers/acoustic-noise.ts +52 -52
  47. package/src/registers/ambient-temperature.ts +52 -52
  48. package/src/registers/axioma-infocode.ts +16 -16
  49. package/src/registers/consumer.ts +17 -17
  50. package/src/registers/cooling-energy-inlet.ts +23 -23
  51. package/src/registers/cooling-energy-outlet.ts +23 -23
  52. package/src/registers/cooling-energy.ts +23 -23
  53. package/src/registers/cooling.ts +113 -113
  54. package/src/registers/device-addresses-kamdraft.ts +14 -14
  55. package/src/registers/device-addresses-msisdn.ts +14 -14
  56. package/src/registers/device-addresses-tchlr.ts +14 -14
  57. package/src/registers/device-addresses-wmbus.ts +17 -17
  58. package/src/registers/device-date-time.ts +9 -9
  59. package/src/registers/device-date.ts +9 -9
  60. package/src/registers/device-nominal-diameter.ts +9 -9
  61. package/src/registers/device-permanent-volume-flow.ts +9 -9
  62. package/src/registers/device-registers.ts +9 -9
  63. package/src/registers/device-samples.ts +37 -37
  64. package/src/registers/device-software-version.ts +8 -8
  65. package/src/registers/device-time.ts +9 -9
  66. package/src/registers/device.ts +16 -16
  67. package/src/registers/diehl-infocode.ts +44 -44
  68. package/src/registers/electrical-energy.ts +23 -23
  69. package/src/registers/energy-inlet.ts +23 -23
  70. package/src/registers/energy-outlet.ts +23 -23
  71. package/src/registers/error-operating-time.ts +23 -23
  72. package/src/registers/flow-temperature-aux.ts +52 -52
  73. package/src/registers/flow-temperature-inlet.ts +52 -52
  74. package/src/registers/flow-temperature-outlet.ts +52 -52
  75. package/src/registers/flow-temperature.ts +52 -52
  76. package/src/registers/heat-energy-inlet.ts +65 -65
  77. package/src/registers/heat-energy-outlet.ts +65 -65
  78. package/src/registers/heat-energy.ts +65 -65
  79. package/src/registers/index.ts +71 -71
  80. package/src/registers/infocode.ts +70 -70
  81. package/src/registers/kamstrup-module-type.ts +8 -8
  82. package/src/registers/kamstrup-multical-infocode.ts +58 -58
  83. package/src/registers/landis-gyr-infocode.ts +23 -23
  84. package/src/registers/landis-gyr-mbus-status.ts +23 -23
  85. package/src/registers/mass-flow-inlet.ts +52 -52
  86. package/src/registers/mass-flow-outlet.ts +52 -52
  87. package/src/registers/mass-flow.ts +52 -52
  88. package/src/registers/mass-inlet.ts +23 -23
  89. package/src/registers/mass-outlet.ts +23 -23
  90. package/src/registers/mass.ts +23 -23
  91. package/src/registers/mbus-format-string-signature.ts +8 -8
  92. package/src/registers/none.ts +8 -8
  93. package/src/registers/operating-time.ts +23 -23
  94. package/src/registers/place.ts +31 -31
  95. package/src/registers/power-inlet.ts +52 -52
  96. package/src/registers/power-outlet.ts +52 -52
  97. package/src/registers/power.ts +52 -52
  98. package/src/registers/pressure-inlet.ts +52 -52
  99. package/src/registers/pressure-outlet.ts +52 -52
  100. package/src/registers/pressure.ts +52 -52
  101. package/src/registers/radiator-temperature.ts +52 -52
  102. package/src/registers/reactive-energy.ts +23 -23
  103. package/src/registers/relative-humidity.ts +8 -8
  104. package/src/registers/remaining-battery-time.ts +23 -23
  105. package/src/registers/time-last-exact-overhearing-arrived.ts +19 -19
  106. package/src/registers/time-last-installation-mounted.ts +19 -19
  107. package/src/registers/time-last-possible-candidate-overhearing-arrived.ts +21 -21
  108. package/src/registers/time-last-sampled.ts +19 -19
  109. package/src/registers/time.ts +45 -45
  110. package/src/registers/volume-flow-inlet.ts +52 -52
  111. package/src/registers/volume-flow-outlet.ts +52 -52
  112. package/src/registers/volume-flow.ts +52 -52
  113. package/src/registers/volume-inlet.ts +23 -23
  114. package/src/registers/volume-outlet.ts +23 -23
  115. package/src/registers/volume-pulse-input-a.ts +23 -23
  116. package/src/registers/volume-pulse-input-b.ts +23 -23
  117. package/src/registers/volume.ts +23 -23
  118. package/src/repository.spec.ts +30 -30
  119. package/src/repository.ts +26 -26
  120. package/src/step.spec.ts +37 -37
  121. package/src/step.ts +45 -45
  122. package/src/time-or-rule.ts +42 -42
  123. package/src/unit-id.ts +108 -108
@@ -1,118 +1,126 @@
1
- export const invariantDecimalSeparator = (0.1).toString()[1];
2
- export const localeDecimalSeparator = (0.1).toLocaleString()[1];
3
-
4
- export interface Parts {
5
- invariantPrecisionString: string;
6
- invariantCoefficientString: string;
7
- exponent: number;
8
- noOfCoefficientDecimals: number;
9
- }
10
-
11
- class Formatter {
12
- public toPrecision(value: number, precision: number, maxDecimals?: number) {
13
- const absValue = Math.abs(value);
14
- const noOfIntegerDigits = Math.round(absValue).toString().length;
15
-
16
- // Adjust precision to maxDecimals
17
- if (maxDecimals !== undefined) {
18
- precision = Math.min(precision, noOfIntegerDigits + maxDecimals);
19
- }
20
-
21
- if (value === 0) {
22
- // (0).toPrecision(1) == "0"
23
- // (0).toPrecision(2) == "0.0"
24
- // (0).toPrecision(3) == "0.00"
25
- // However, we want:
26
- // Formatter.toPrecision(0, 0) == "0"
27
- // Formatter.toPrecision(0, 1) == "0.0"
28
- // Formatter.toPrecision(0, 2) == "0.00"
29
- return value.toPrecision(precision + 1);
30
- }
31
-
32
- if (absValue < 1) {
33
- return value.toPrecision(precision);
34
- }
35
-
36
- return precision >= noOfIntegerDigits
37
- ? value.toFixed(precision - noOfIntegerDigits)
38
- : value.toPrecision(precision);
39
- }
40
-
41
- public toLocalePrecision(
42
- value: number,
43
- precision: number,
44
- maxDecimals?: number,
45
- ) {
46
- return this.toPrecision(value, precision, maxDecimals).replace(
47
- invariantDecimalSeparator,
48
- localeDecimalSeparator,
49
- );
50
- }
51
-
52
- public toFixed(value: number, precision: number, maxDecimals?: number) {
53
- const parts = this.getParts(value, precision, maxDecimals);
54
- if (parts.noOfCoefficientDecimals < parts.exponent) {
55
- const number = parts.invariantCoefficientString.replace(
56
- invariantDecimalSeparator,
57
- "",
58
- );
59
- const zeros = parts.exponent - parts.noOfCoefficientDecimals;
60
- return `${number}${"0".repeat(zeros)}`;
61
- } else {
62
- return parts.invariantPrecisionString;
63
- }
64
- }
65
-
66
- public toLocaleFixed(
67
- value: number,
68
- precision: number,
69
- maxDecimals?: number,
70
- ) {
71
- return this.toFixed(value, precision, maxDecimals).replace(
72
- invariantDecimalSeparator,
73
- localeDecimalSeparator,
74
- );
75
- }
76
-
77
- public getParts(
78
- value: number,
79
- precision: number,
80
- maxDecimals?: number,
81
- ): Parts {
82
- const precisionString = this.toPrecision(value, precision, maxDecimals);
83
- if (precisionString.indexOf("e") >= 0) {
84
- const match = /(.*)e([\+-]\d+)$/.exec(precisionString)!;
85
- const coefficientString = match[1];
86
- const exponent = parseInt(match[2]);
87
- const decimalSeparatorIndex = coefficientString.indexOf(
88
- invariantDecimalSeparator,
89
- );
90
- const noOfCoefficientDecimals =
91
- decimalSeparatorIndex >= 0
92
- ? coefficientString.length - decimalSeparatorIndex - 1
93
- : 0;
94
- return {
95
- invariantPrecisionString: precisionString,
96
- invariantCoefficientString: coefficientString,
97
- exponent,
98
- noOfCoefficientDecimals,
99
- };
100
- } else {
101
- const decimalSeparatorIndex = precisionString.indexOf(
102
- invariantDecimalSeparator,
103
- );
104
- const noOfCoefficientDecimals =
105
- decimalSeparatorIndex >= 0
106
- ? precisionString.length - decimalSeparatorIndex - 1
107
- : 0;
108
- return {
109
- invariantPrecisionString: precisionString,
110
- invariantCoefficientString: precisionString,
111
- exponent: 0,
112
- noOfCoefficientDecimals,
113
- };
114
- }
115
- }
116
- }
117
-
118
- export const NumberFormatter = new Formatter();
1
+ export const invariantDecimalSeparator = (0.1).toString()[1];
2
+ export const localeDecimalSeparator = (0.1).toLocaleString()[1];
3
+
4
+ export interface Parts {
5
+ invariantPrecisionString: string;
6
+ invariantCoefficientString: string;
7
+ exponent: number;
8
+ noOfCoefficientDecimals: number;
9
+ }
10
+
11
+ class Formatter {
12
+ public toPrecision(value: number, precision: number, maxDecimals?: number) {
13
+ if (value === 0) {
14
+ // By convention the precision of zero is its number of decimals.
15
+ // (0).toPrecision(1) == "0"
16
+ // (0).toPrecision(2) == "0.0"
17
+ // (0).toPrecision(3) == "0.00"
18
+ // However, we want:
19
+ // Formatter.toPrecision(0, 0) == "0"
20
+ // Formatter.toPrecision(0, 1) == "0.0"
21
+ // Formatter.toPrecision(0, 2) == "0.00"
22
+ const decimals =
23
+ maxDecimals !== undefined
24
+ ? Math.min(precision, maxDecimals)
25
+ : precision;
26
+ return (0).toPrecision(Math.max(decimals, 0) + 1);
27
+ }
28
+
29
+ const absValue = Math.abs(value);
30
+ const noOfIntegerDigits = Math.round(absValue).toString().length;
31
+
32
+ // Adjust precision to maxDecimals
33
+ if (maxDecimals !== undefined) {
34
+ precision = Math.min(precision, noOfIntegerDigits + maxDecimals);
35
+ }
36
+
37
+ // A precision of 0 is only valid for zero, use at least one significant digit for any other value
38
+ precision = Math.max(precision, 1);
39
+
40
+ if (absValue < 1) {
41
+ return value.toPrecision(precision);
42
+ }
43
+
44
+ return precision >= noOfIntegerDigits
45
+ ? value.toFixed(precision - noOfIntegerDigits)
46
+ : value.toPrecision(precision);
47
+ }
48
+
49
+ public toLocalePrecision(
50
+ value: number,
51
+ precision: number,
52
+ maxDecimals?: number,
53
+ ) {
54
+ return this.toPrecision(value, precision, maxDecimals).replace(
55
+ invariantDecimalSeparator,
56
+ localeDecimalSeparator,
57
+ );
58
+ }
59
+
60
+ public toFixed(value: number, precision: number, maxDecimals?: number) {
61
+ const parts = this.getParts(value, precision, maxDecimals);
62
+ if (parts.noOfCoefficientDecimals < parts.exponent) {
63
+ const number = parts.invariantCoefficientString.replace(
64
+ invariantDecimalSeparator,
65
+ "",
66
+ );
67
+ const zeros = parts.exponent - parts.noOfCoefficientDecimals;
68
+ return `${number}${"0".repeat(zeros)}`;
69
+ } else {
70
+ return parts.invariantPrecisionString;
71
+ }
72
+ }
73
+
74
+ public toLocaleFixed(
75
+ value: number,
76
+ precision: number,
77
+ maxDecimals?: number,
78
+ ) {
79
+ return this.toFixed(value, precision, maxDecimals).replace(
80
+ invariantDecimalSeparator,
81
+ localeDecimalSeparator,
82
+ );
83
+ }
84
+
85
+ public getParts(
86
+ value: number,
87
+ precision: number,
88
+ maxDecimals?: number,
89
+ ): Parts {
90
+ const precisionString = this.toPrecision(value, precision, maxDecimals);
91
+ if (precisionString.indexOf("e") >= 0) {
92
+ const match = /(.*)e([\+-]\d+)$/.exec(precisionString)!;
93
+ const coefficientString = match[1];
94
+ const exponent = parseInt(match[2]);
95
+ const decimalSeparatorIndex = coefficientString.indexOf(
96
+ invariantDecimalSeparator,
97
+ );
98
+ const noOfCoefficientDecimals =
99
+ decimalSeparatorIndex >= 0
100
+ ? coefficientString.length - decimalSeparatorIndex - 1
101
+ : 0;
102
+ return {
103
+ invariantPrecisionString: precisionString,
104
+ invariantCoefficientString: coefficientString,
105
+ exponent,
106
+ noOfCoefficientDecimals,
107
+ };
108
+ } else {
109
+ const decimalSeparatorIndex = precisionString.indexOf(
110
+ invariantDecimalSeparator,
111
+ );
112
+ const noOfCoefficientDecimals =
113
+ decimalSeparatorIndex >= 0
114
+ ? precisionString.length - decimalSeparatorIndex - 1
115
+ : 0;
116
+ return {
117
+ invariantPrecisionString: precisionString,
118
+ invariantCoefficientString: precisionString,
119
+ exponent: 0,
120
+ noOfCoefficientDecimals,
121
+ };
122
+ }
123
+ }
124
+ }
125
+
126
+ export const NumberFormatter = new Formatter();
package/src/optional.ts CHANGED
@@ -1 +1 @@
1
- export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
1
+ export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;