@utiliread/primitives 1.0.0-6 → 1.0.0-60

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 (146) hide show
  1. package/dist/cjs/index.js +17 -5
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/cjs/measure-id.js +3 -0
  4. package/dist/cjs/measure-id.js.map +1 -0
  5. package/dist/cjs/number-formatter.js +66 -39
  6. package/dist/cjs/number-formatter.js.map +1 -1
  7. package/dist/cjs/number-formatter.spec.js +94 -0
  8. package/dist/cjs/number-formatter.spec.js.map +1 -0
  9. package/dist/cjs/optional.js +3 -0
  10. package/dist/cjs/optional.js.map +1 -0
  11. package/dist/cjs/precision-number.js +36 -0
  12. package/dist/cjs/precision-number.js.map +1 -0
  13. package/dist/cjs/precision-number.spec.js +27 -0
  14. package/dist/cjs/precision-number.spec.js.map +1 -0
  15. package/dist/cjs/register-convention.js +56 -0
  16. package/dist/cjs/register-convention.js.map +1 -0
  17. package/dist/cjs/register-convention.spec.js +39 -0
  18. package/dist/cjs/register-convention.spec.js.map +1 -0
  19. package/dist/cjs/register-library.js +31 -22
  20. package/dist/cjs/register-library.js.map +1 -1
  21. package/dist/cjs/register-library.spec.js +20 -13
  22. package/dist/cjs/register-library.spec.js.map +1 -1
  23. package/dist/cjs/register-utils.js +8 -0
  24. package/dist/cjs/register-utils.js.map +1 -0
  25. package/dist/cjs/repository.js +26 -0
  26. package/dist/cjs/repository.js.map +1 -0
  27. package/dist/cjs/repository.spec.js +21 -0
  28. package/dist/cjs/repository.spec.js.map +1 -0
  29. package/dist/cjs/step.js +41 -0
  30. package/dist/cjs/step.js.map +1 -0
  31. package/dist/cjs/step.spec.js +29 -0
  32. package/dist/cjs/step.spec.js.map +1 -0
  33. package/dist/cjs/time-or-rule.js +19 -0
  34. package/dist/cjs/time-or-rule.js.map +1 -0
  35. package/dist/cjs/unit-id.js.map +1 -1
  36. package/dist/esm/index.d.ts +15 -7
  37. package/dist/esm/index.js +8 -3
  38. package/dist/esm/index.js.map +1 -1
  39. package/dist/esm/measure-id.d.ts +1 -0
  40. package/dist/esm/measure-id.js +2 -0
  41. package/dist/esm/measure-id.js.map +1 -0
  42. package/dist/esm/number-formatter.d.ts +9 -0
  43. package/dist/esm/number-formatter.js +69 -42
  44. package/dist/esm/number-formatter.js.map +1 -1
  45. package/dist/esm/{register-converter.spec.d.ts → number-formatter.spec.d.ts} +0 -0
  46. package/dist/esm/number-formatter.spec.js +92 -0
  47. package/dist/esm/number-formatter.spec.js.map +1 -0
  48. package/dist/esm/optional.d.ts +1 -0
  49. package/dist/esm/optional.js +2 -0
  50. package/dist/esm/optional.js.map +1 -0
  51. package/dist/esm/precision-number.d.ts +11 -0
  52. package/dist/esm/precision-number.js +32 -0
  53. package/dist/esm/precision-number.js.map +1 -0
  54. package/dist/esm/precision-number.spec.d.ts +1 -0
  55. package/dist/esm/precision-number.spec.js +25 -0
  56. package/dist/esm/precision-number.spec.js.map +1 -0
  57. package/dist/esm/register-convention.d.ts +14 -0
  58. package/dist/esm/register-convention.js +52 -0
  59. package/dist/esm/register-convention.js.map +1 -0
  60. package/dist/esm/register-convention.spec.d.ts +1 -0
  61. package/dist/esm/register-convention.spec.js +37 -0
  62. package/dist/esm/register-convention.spec.js.map +1 -0
  63. package/dist/esm/register-definition.d.ts +4 -0
  64. package/dist/esm/register-id.d.ts +1 -1
  65. package/dist/esm/register-library.d.ts +4 -0
  66. package/dist/esm/register-library.js +32 -23
  67. package/dist/esm/register-library.js.map +1 -1
  68. package/dist/esm/register-library.spec.js +16 -9
  69. package/dist/esm/register-library.spec.js.map +1 -1
  70. package/dist/esm/register-utils.d.ts +3 -0
  71. package/dist/esm/register-utils.js +4 -0
  72. package/dist/esm/register-utils.js.map +1 -0
  73. package/dist/esm/repository.d.ts +9 -0
  74. package/dist/esm/repository.js +22 -0
  75. package/dist/esm/repository.js.map +1 -0
  76. package/dist/esm/repository.spec.d.ts +1 -0
  77. package/dist/esm/repository.spec.js +19 -0
  78. package/dist/esm/repository.spec.js.map +1 -0
  79. package/dist/esm/step.d.ts +15 -0
  80. package/dist/esm/step.js +38 -0
  81. package/dist/esm/step.js.map +1 -0
  82. package/dist/esm/step.spec.d.ts +1 -0
  83. package/dist/esm/step.spec.js +27 -0
  84. package/dist/esm/step.spec.js.map +1 -0
  85. package/dist/esm/time-or-rule.d.ts +7 -0
  86. package/dist/esm/time-or-rule.js +16 -0
  87. package/dist/esm/time-or-rule.js.map +1 -0
  88. package/dist/esm/unit-id.d.ts +1 -1
  89. package/dist/esm/unit-id.js +1 -1
  90. package/dist/esm/unit-id.js.map +1 -1
  91. package/karma.config.js +11 -14
  92. package/locales/da/aggregates.json +8 -0
  93. package/locales/da/consumption-types.json +7 -0
  94. package/locales/da/measures.json +15 -0
  95. package/locales/da/registers.json +198 -0
  96. package/locales/da/units.json +90 -0
  97. package/locales/en/aggregates.json +8 -0
  98. package/locales/en/consumption-types.json +7 -0
  99. package/locales/en/measures.json +15 -0
  100. package/locales/en/registers.json +198 -0
  101. package/locales/en/units.json +90 -0
  102. package/package.json +11 -4
  103. package/src/aggregate-id.ts +9 -9
  104. package/src/consumption-type-id.ts +8 -8
  105. package/src/index.ts +29 -11
  106. package/src/measure-id.ts +14 -0
  107. package/src/number-formatter.spec.ts +99 -0
  108. package/src/number-formatter.ts +88 -52
  109. package/src/optional.ts +1 -0
  110. package/src/precision-number.spec.ts +28 -0
  111. package/src/precision-number.ts +36 -0
  112. package/src/register-convention.spec.ts +43 -0
  113. package/src/register-convention.ts +61 -0
  114. package/src/register-definition.ts +23 -10
  115. package/src/register-id.ts +80 -6
  116. package/src/register-library.spec.ts +41 -33
  117. package/src/register-library.ts +57 -37
  118. package/src/register-pack.ts +5 -5
  119. package/src/register-utils.ts +6 -0
  120. package/src/repository.spec.ts +20 -0
  121. package/src/repository.ts +24 -0
  122. package/src/step.spec.ts +30 -0
  123. package/src/step.ts +37 -0
  124. package/src/time-or-rule.ts +17 -0
  125. package/src/unit-id.ts +126 -100
  126. package/tsconfig.cjs.json +7 -8
  127. package/tsconfig.json +15 -14
  128. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/base.css +0 -224
  129. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/block-navigation.js +0 -79
  130. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/favicon.png +0 -0
  131. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/index.html +0 -96
  132. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.css +0 -1
  133. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.js +0 -2
  134. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sort-arrow-sprite.png +0 -0
  135. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sorter.js +0 -170
  136. package/dist/cjs/register-converter.js +0 -47
  137. package/dist/cjs/register-converter.js.map +0 -1
  138. package/dist/cjs/register-converter.spec.js +0 -29
  139. package/dist/cjs/register-converter.spec.js.map +0 -1
  140. package/dist/esm/register-converter.d.ts +0 -9
  141. package/dist/esm/register-converter.js +0 -44
  142. package/dist/esm/register-converter.js.map +0 -1
  143. package/dist/esm/register-converter.spec.js +0 -27
  144. package/dist/esm/register-converter.spec.js.map +0 -1
  145. package/src/register-converter.spec.ts +0 -31
  146. package/src/register-converter.ts +0 -48
@@ -0,0 +1,90 @@
1
+ {
2
+ "unitless": [""],
3
+
4
+ "milliWattHour": ["mWh"],
5
+ "wattHour": ["Wh"],
6
+ "kiloWattHour": ["kWh"],
7
+ "megaWattHour": ["MWh"],
8
+ "gigaWattHour": ["GWh"],
9
+ "calorie": ["cal"],
10
+ "kiloCalorie": ["kcal"],
11
+ "megaCalorie": ["Mcal"],
12
+
13
+ "joule": ["J"],
14
+ "kiloJoule": ["kJ"],
15
+ "megaJoule": ["MJ"],
16
+ "gigaJoule": ["GJ"],
17
+
18
+ "liter": ["L"],
19
+ "cubicMeter": ["m\u00B3"],
20
+
21
+ "fluidOunce": ["fl oz"],
22
+ "gallon": ["gal"],
23
+
24
+ "gram": ["g"],
25
+ "kiloGram": ["kg"],
26
+ "tonne": ["T"],
27
+
28
+ "ounce": ["oz"],
29
+ "pound": ["lbs"],
30
+ "ton": ["T"],
31
+
32
+ "second": ["s"],
33
+ "minute": ["mi"],
34
+ "hour": ["h", "Hour"],
35
+ "day": ["d", "Day"],
36
+ "week": ["w", "Week"],
37
+ "month": ["mo", "Month"],
38
+ "year": ["y", "Year"],
39
+
40
+ "milliWatt": ["mW"],
41
+ "watt": ["W"],
42
+ "kiloWatt": ["kW"],
43
+ "megaWatt": ["MW"],
44
+ "gigaWatt": ["GW"],
45
+
46
+ "joulesPerSecond": ["J/s"],
47
+ "joulesPerMinute": ["J/mi"],
48
+ "joulesPerHour": ["J/t"],
49
+
50
+ "litersPerSecond": ["L/s"],
51
+ "litersPerMinute": ["L/mi"],
52
+ "litersPerHour": ["L/t"],
53
+
54
+ "cubicMetersPerSecond": ["m\u00B3/s"],
55
+ "cubicMetersPerMinute": ["m\u00B3/mi"],
56
+ "cubicMetersPerHour": ["m\u00B3/t"],
57
+
58
+ "fluidOuncesPerSecond": ["fl oz/s"],
59
+ "fluidOuncesPerMinute": ["fl oz/mi"],
60
+ "fluidOuncesPerHour": ["fl oz/t"],
61
+
62
+ "gallonsPerSecond": ["gal/s"],
63
+ "gallonsPerMinute": ["gal/mi"],
64
+ "gallonsPerHour": ["gal/t"],
65
+
66
+ "gramsPerSecond": ["g/s"],
67
+ "gramsPerMinute": ["g/mi"],
68
+ "gramsPerHour": ["g/t"],
69
+
70
+ "kiloGramsPerSecond": ["kg/s"],
71
+ "kiloGramsPerMinute": ["kg/mi"],
72
+ "kiloGramsPerHour": ["kg/t"],
73
+
74
+ "celcius": ["\u00B0C"],
75
+ "kelvin": ["K"],
76
+ "fahrenheit": ["F"],
77
+
78
+ "var": ["var"],
79
+ "kiloVar": ["kvar"],
80
+ "megaVar": ["Mvar"],
81
+
82
+ "varHour": ["varh"],
83
+ "kiloVarHour": ["kvarh"],
84
+ "megaVarHour": ["Mvarh"],
85
+
86
+ "percentage": ["%"],
87
+
88
+ "literCelcius": ["l\u00B7\u2103"],
89
+ "cubicMeterCelcius": ["m\u00B3\u00B7\u2103"]
90
+ }
package/package.json CHANGED
@@ -1,29 +1,36 @@
1
1
  {
2
2
  "name": "@utiliread/primitives",
3
- "version": "1.0.0-6",
3
+ "version": "1.0.0-60",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/esm/index.d.ts",
7
+ "sideEffects": false,
7
8
  "scripts": {
8
9
  "build": "tsc && tsc -p tsconfig.cjs.json",
9
10
  "test": "karma start --single-run --browsers ChromeHeadless karma.config.js",
10
11
  "watch": "karma start --auto-watch --browsers ChromeDebugging karma.config.js",
12
+ "chrome": "karma start --auto-watch --browsers Chrome karma.config.js",
11
13
  "clean": "rimraf dist"
12
14
  },
13
15
  "publishConfig": {
14
16
  "access": "public"
15
17
  },
16
18
  "devDependencies": {
17
- "@types/chai": "^4.2.16",
18
- "@types/mocha": "^8.2.2",
19
+ "@types/chai": "^4.2.18",
20
+ "@types/luxon": "^2.3.2",
21
+ "@types/mocha": "^9.0.0",
19
22
  "chai": "^4.3.4",
20
23
  "karma": "^6.3.2",
21
24
  "karma-chai": "^0.1.0",
22
25
  "karma-chrome-launcher": "^3.1.0",
23
26
  "karma-mocha": "^2.0.1",
24
27
  "karma-typescript": "^5.5.1",
25
- "mocha": "^8.3.2",
28
+ "luxon": "^2.1.0",
29
+ "mocha": "^9.1.3",
26
30
  "rimraf": "^3.0.2",
27
31
  "typescript": "^4.1.5"
32
+ },
33
+ "peerDependencies": {
34
+ "luxon": "^2.1.0"
28
35
  }
29
36
  }
@@ -1,9 +1,9 @@
1
- export type AggregateId =
2
- "instant" |
3
- "diff" |
4
- "min" |
5
- "max" |
6
- "avg" |
7
- "sum" |
8
- "cnt" |
9
- "ssq";
1
+ export type AggregateId =
2
+ "instant" |
3
+ "diff" |
4
+ "min" |
5
+ "max" |
6
+ "avg" |
7
+ "sum" |
8
+ "cnt" |
9
+ "ssq";
@@ -1,8 +1,8 @@
1
- export type ConsumptionTypeId =
2
- "none" |
3
- "heat" |
4
- "water" |
5
- "cooling" |
6
- "electricity" |
7
- "infrastructure" |
8
- "all";
1
+ export type ConsumptionTypeId =
2
+ "none" |
3
+ "heat" |
4
+ "water" |
5
+ "cooling" |
6
+ "electricity" |
7
+ "infrastructure" |
8
+ "all";
package/src/index.ts CHANGED
@@ -1,17 +1,35 @@
1
- import { ConsumptionTypeId } from "./consumption-type-id";
2
- import { RegisterId } from "./register-id";
3
- import { UnitId } from "./unit-id";
1
+ import type { ConsumptionTypeId } from "./consumption-type-id";
2
+ import type { RegisterId } from "./register-id";
3
+ import type { UnitId } from "./unit-id";
4
4
  import { RegisterLibrary } from "./register-library";
5
- import { RegisterDefinition } from "./register-definition";
6
- import { RegisterConverter } from "./register-converter";
7
- import { NumberFormatter } from "./number-formatter";
5
+ import type { RegisterDefinition } from "./register-definition";
6
+ import { NumberFormatter, invariantDecimalSeparator, localeDecimalSeparator } from "./number-formatter";
7
+ import { RegisterConvention } from "./register-convention";
8
+ import type { AggregateId } from "./aggregate-id";
9
+ import type { MeasureId } from "./measure-id";
10
+ import { Step, DayOfWeek } from "./step";
11
+ import { TimeOrRule } from "./time-or-rule";
12
+ import { PrecisionNumber } from "./precision-number";
13
+ import "./repository"; // We need this to get the Repository namespace exported
14
+ import { Repository } from "./repository";
15
+ import type { Optional } from "./optional";
8
16
 
9
17
  export {
18
+ AggregateId,
10
19
  ConsumptionTypeId,
11
- RegisterId,
20
+ DayOfWeek,
21
+ MeasureId,
22
+ NumberFormatter,
23
+ PrecisionNumber,
24
+ RegisterConvention,
12
25
  RegisterDefinition,
13
- UnitId,
26
+ RegisterId,
14
27
  RegisterLibrary,
15
- RegisterConverter,
16
- NumberFormatter,
17
- };
28
+ Repository,
29
+ Step,
30
+ TimeOrRule,
31
+ UnitId,
32
+ invariantDecimalSeparator,
33
+ localeDecimalSeparator,
34
+ Optional,
35
+ };
@@ -0,0 +1,14 @@
1
+ export type MeasureId =
2
+ "none" |
3
+ "duration" |
4
+ "energy" |
5
+ "humidity" |
6
+ "mass" |
7
+ "massFlow" |
8
+ "power" |
9
+ "reactiveEnergy" |
10
+ "reactivePower" |
11
+ "temperature" |
12
+ "time" |
13
+ "volume" |
14
+ "volumeFlow";
@@ -0,0 +1,99 @@
1
+ import { expect } from 'chai';
2
+ import { NumberFormatter, Parts, localeDecimalSeparator, invariantDecimalSeparator } from './number-formatter';
3
+
4
+ describe("toParts and toPrecision", () => {
5
+ it("should format correctly", () => {
6
+ const data: [number, number, Parts][] = [
7
+ [0, 0, { invariantPrecisionString: "0", invariantCoefficientString: "0", exponent: 0, noOfCoefficientDecimals: 0 }],
8
+ [0.0, 1, { invariantPrecisionString: "0.0", invariantCoefficientString: "0.0", exponent: 0, noOfCoefficientDecimals: 1 }],
9
+ [77.1274, 1, { invariantPrecisionString: "8e+1", invariantCoefficientString: "8", exponent: 1, noOfCoefficientDecimals: 0 }],
10
+ [77.1274, 2, { invariantPrecisionString: "77", invariantCoefficientString: "77", exponent: 0, noOfCoefficientDecimals: 0 }],
11
+ [77.1274, 4, { invariantPrecisionString: "77.13", invariantCoefficientString: "77.13", exponent: 0, noOfCoefficientDecimals: 2 }],
12
+ [77.1274, 5, { invariantPrecisionString: "77.127", invariantCoefficientString: "77.127", exponent: 0, noOfCoefficientDecimals: 3 }],
13
+ [77.1274, 6, { invariantPrecisionString: "77.1274", invariantCoefficientString: "77.1274", exponent: 0, noOfCoefficientDecimals: 4 }],
14
+ [77.1274, 7, { invariantPrecisionString: "77.12740", invariantCoefficientString: "77.12740", exponent: 0, noOfCoefficientDecimals: 5 }],
15
+ [77.1274, 15, { invariantPrecisionString: "77.1274000000000", invariantCoefficientString: "77.1274000000000", exponent: 0, noOfCoefficientDecimals: 13 }],
16
+ [77.1274, 16, { invariantPrecisionString: "77.12739999999999", invariantCoefficientString: "77.12739999999999", exponent: 0, noOfCoefficientDecimals: 14 }],
17
+ [77.1274, 17, { invariantPrecisionString: "77.127399999999994", invariantCoefficientString: "77.127399999999994", exponent: 0, noOfCoefficientDecimals: 15 }],
18
+ [77.1274, 18, { invariantPrecisionString: "77.1273999999999944", invariantCoefficientString: "77.1273999999999944", exponent: 0, noOfCoefficientDecimals: 16 }],
19
+ [77.1274, 19, { invariantPrecisionString: "77.12739999999999441", invariantCoefficientString: "77.12739999999999441", exponent: 0, noOfCoefficientDecimals: 17 }],
20
+ [77.1274, 20, { invariantPrecisionString: "77.127399999999994407", invariantCoefficientString: "77.127399999999994407", exponent: 0, noOfCoefficientDecimals: 18 }],
21
+ [77.1274, 21, { invariantPrecisionString: "77.1273999999999944066", invariantCoefficientString: "77.1273999999999944066", exponent: 0, noOfCoefficientDecimals: 19 }],
22
+ [0.00000123, 2, { invariantPrecisionString: "0.0000012", invariantCoefficientString: "0.0000012", exponent: 0, noOfCoefficientDecimals: 7 }],
23
+ [0.000000123, 2, { invariantPrecisionString: "1.2e-7", invariantCoefficientString: "1.2", exponent: -7, noOfCoefficientDecimals: 1 }],
24
+ [0.00000123, 21, { invariantPrecisionString: "0.00000123000000000000008198", invariantCoefficientString: "0.00000123000000000000008198", exponent: 0, noOfCoefficientDecimals: 26 }],
25
+ [123456, 3, { invariantPrecisionString: "1.23e+5", invariantCoefficientString: "1.23", exponent: 5, noOfCoefficientDecimals: 2 }],
26
+ [Number.MAX_VALUE, 3, { invariantPrecisionString: "1.80e+308", invariantCoefficientString: "1.80", exponent: 308, noOfCoefficientDecimals: 2 }],
27
+ [123456, 15, { invariantPrecisionString: "123456.000000000", invariantCoefficientString: "123456.000000000", exponent: 0, noOfCoefficientDecimals: 9 }],
28
+ [0, 5, { invariantPrecisionString: "0.00000", invariantCoefficientString: "0.00000", exponent: 0, noOfCoefficientDecimals: 5 }],
29
+ [0.000001, 1, { invariantPrecisionString: "0.000001", invariantCoefficientString: "0.000001", exponent: 0, noOfCoefficientDecimals: 6 }],
30
+ [0.000001, 10, { invariantPrecisionString: "0.000001000000000", invariantCoefficientString: "0.000001000000000", exponent: 0, noOfCoefficientDecimals: 15 }],
31
+ [0.0000001, 10, { invariantPrecisionString: "1.000000000e-7", invariantCoefficientString: "1.000000000", exponent: -7, noOfCoefficientDecimals: 9 }],
32
+ [0.00000000001, 1, { invariantPrecisionString: "1e-11", invariantCoefficientString: "1", exponent: -11, noOfCoefficientDecimals: 0 }],
33
+ [0.00000000001, 2, { invariantPrecisionString: "1.0e-11", invariantCoefficientString: "1.0", exponent: -11, noOfCoefficientDecimals: 1 }],
34
+ [55, 2, { invariantPrecisionString: "55", invariantCoefficientString: "55", exponent: 0, noOfCoefficientDecimals: 0 }],
35
+ [55, 1, { invariantPrecisionString: "6e+1", invariantCoefficientString: "6", exponent: 1, noOfCoefficientDecimals: 0 }],
36
+ [-55, 2, { invariantPrecisionString: "-55", invariantCoefficientString: "-55", exponent: 0, noOfCoefficientDecimals: 0 }],
37
+ [-55, 1, { invariantPrecisionString: "-6e+1", invariantCoefficientString: "-6", exponent: 1, noOfCoefficientDecimals: 0 }],
38
+ [9.59, 1, { invariantPrecisionString: "1e+1", invariantCoefficientString: "1", exponent: 1, noOfCoefficientDecimals: 0 }],
39
+ [9.59, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
40
+ [9.95, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
41
+ [9.96, 2, { invariantPrecisionString: "10", invariantCoefficientString: "10", exponent: 0, noOfCoefficientDecimals: 0 }],
42
+ [-555555555555555555555, 1, { invariantPrecisionString: "-6e+20", invariantCoefficientString: "-6", exponent: 20, noOfCoefficientDecimals: 0 }],
43
+ [-5555555555555555555555, 1, { invariantPrecisionString: "-6e+21", invariantCoefficientString: "-6", exponent: 21, noOfCoefficientDecimals: 0 }],
44
+ [18014398509482012, 21, { invariantPrecisionString: "18014398509482012.0000", invariantCoefficientString: "18014398509482012.0000", exponent: 0, noOfCoefficientDecimals: 4 }],
45
+ [180143985094820121234, 21, { invariantPrecisionString: "180143985094820134912", invariantCoefficientString: "180143985094820134912", exponent: 0, noOfCoefficientDecimals: 0 }],
46
+ [0.1, 21, { invariantPrecisionString: "0.100000000000000005551", invariantCoefficientString: "0.100000000000000005551", exponent: 0, noOfCoefficientDecimals: 21 }],
47
+ [Number.EPSILON, 2, { invariantPrecisionString: "2.2e-16", invariantCoefficientString: "2.2", exponent: -16, noOfCoefficientDecimals: 1 }],
48
+ [Number.EPSILON, 21, { invariantPrecisionString: "2.22044604925031308085e-16", invariantCoefficientString: "2.22044604925031308085", exponent: -16, noOfCoefficientDecimals: 20 }],
49
+ [Number.NaN, 3, { invariantPrecisionString: "NaN", invariantCoefficientString: "NaN", exponent: 0, noOfCoefficientDecimals: 0 }],
50
+ [Number.POSITIVE_INFINITY, 3, { invariantPrecisionString: "Infinity", invariantCoefficientString: "Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
51
+ [Number.NEGATIVE_INFINITY, 3, { invariantPrecisionString: "-Infinity", invariantCoefficientString: "-Infinity", exponent: 0, noOfCoefficientDecimals: 0 }],
52
+ [164735280, 7, { invariantPrecisionString: "1.647353e+8", invariantCoefficientString: "1.647353", exponent: 8, noOfCoefficientDecimals: 6 }],
53
+ ];
54
+
55
+ for (const [value, precision, expectedParts] of data) {
56
+ expect(NumberFormatter.toPrecision(value, precision)).to.equal(expectedParts.invariantPrecisionString);
57
+
58
+ const localePrecisionString = expectedParts.invariantPrecisionString.replace(invariantDecimalSeparator, localeDecimalSeparator);
59
+ expect(NumberFormatter.toLocalePrecision(value, precision)).to.equal(localePrecisionString);
60
+
61
+ const parts = NumberFormatter.getParts(value, precision);
62
+ expect(parts.invariantPrecisionString).to.equal(expectedParts.invariantPrecisionString);
63
+ expect(parts.invariantCoefficientString).to.equal(expectedParts.invariantCoefficientString);
64
+ expect(parts.exponent).to.equal(expectedParts.exponent);
65
+ expect(parts.noOfCoefficientDecimals).to.equal(expectedParts.noOfCoefficientDecimals);
66
+ }
67
+ })
68
+ })
69
+
70
+ describe("toFixed", () => {
71
+ it("should format correctly", () => {
72
+ const data: [number, number, string][] = [
73
+ [0, 0, "0"],
74
+ [0.0, 1, "0.0"],
75
+ [10, 1, "10"],
76
+ [10, 2, "10"],
77
+ [10, 3, "10.0"],
78
+ [10, 4, "10.00"],
79
+ [10.2, 1, "10"],
80
+ [10.2, 2, "10"],
81
+ [10.2, 3, "10.2"],
82
+ [10.2, 4, "10.20"],
83
+ [217887000000, 6, "217887000000"],
84
+ ];
85
+
86
+ for (const [value, precision, invariantExpected] of data) {
87
+ expect(NumberFormatter.toFixed(value, precision)).to.equal(invariantExpected);
88
+
89
+ const localeExpected = invariantExpected.replace(invariantDecimalSeparator, localeDecimalSeparator);
90
+ expect(NumberFormatter.toLocaleFixed(value, precision)).to.equal(localeExpected);
91
+ }
92
+ })
93
+ })
94
+
95
+ describe("toLocaleFixed", () => {
96
+ it("should format correctly", () => {
97
+ expect(NumberFormatter.toLocaleFixed(400, 2)).to.equal("400");
98
+ })
99
+ });
@@ -1,52 +1,88 @@
1
- export const invariantDecimalSeparator = (0.1).toString()[1];
2
- export const localeDecimalSeparator = (0.1).toLocaleString()[1];
3
-
4
- class Formatter {
5
- public toLocalePrecision(value: number, precision: number) {
6
- const absValue = Math.abs(value);
7
- if (absValue < 1) {
8
- return toPrecision(value, precision);
9
- }
10
-
11
- const noOfIntegerDigits = Math.round(absValue).toString().length;
12
- return precision >= noOfIntegerDigits
13
- ? toFixed(value, precision - noOfIntegerDigits)
14
- : toPrecision(value, precision);
15
- }
16
-
17
- public toLocaleFixed(value: number, precision: number) {
18
- const formatted = this.toLocalePrecision(value, precision);
19
- const match = /(.*)e\+(\d)$/.exec(formatted);
20
- if (match) {
21
- let number = match[1];
22
- let exponent = parseInt(match[2]);
23
- const decimalSeparatorIndex = number.indexOf(localeDecimalSeparator);
24
- if (decimalSeparatorIndex === -1) {
25
- return `${number}${"0".repeat(exponent)}`;
26
- }
27
- else if (decimalSeparatorIndex >= 0) {
28
- const noOfDecimals = number.length - decimalSeparatorIndex - 1;
29
-
30
- if (noOfDecimals < exponent) {
31
- number = number.replace(localeDecimalSeparator, "");
32
- exponent -= noOfDecimals;
33
-
34
- return `${number}${"0".repeat(exponent)}`;
35
- }
36
- }
37
- }
38
- return formatted;
39
- }
40
- }
41
-
42
- function toFixed(value: number, fractionDigits: number) {
43
- const formatted = value.toFixed(fractionDigits);
44
- return formatted.replace(invariantDecimalSeparator, localeDecimalSeparator);
45
- }
46
-
47
- function toPrecision(value: number, precision: number) {
48
- const formatted = value.toPrecision(precision);
49
- return formatted.replace(invariantDecimalSeparator, localeDecimalSeparator);
50
- }
51
-
52
- 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) {
13
+ if (value === 0) {
14
+ // (0).toPrecision(1) == "0"
15
+ // (0).toPrecision(2) == "0.0"
16
+ // (0).toPrecision(3) == "0.00"
17
+ // However, we want:
18
+ // Formatter.toPrecision(0, 0) == "0"
19
+ // Formatter.toPrecision(0, 1) == "0.0"
20
+ // Formatter.toPrecision(0, 2) == "0.00"
21
+ return value.toPrecision(precision + 1);
22
+ }
23
+ const absValue = Math.abs(value);
24
+ if (absValue < 1) {
25
+ return value.toPrecision(precision);
26
+ }
27
+
28
+ const noOfIntegerDigits = Math.round(absValue).toString().length;
29
+ return precision >= noOfIntegerDigits
30
+ ? value.toFixed(precision - noOfIntegerDigits)
31
+ : value.toPrecision(precision);
32
+ }
33
+
34
+ public toLocalePrecision(value: number, precision: number) {
35
+ return this.toPrecision(value, precision)
36
+ .replace(invariantDecimalSeparator, localeDecimalSeparator);
37
+ }
38
+
39
+ public toFixed(value: number, precision: number) {
40
+ const parts = this.getParts(value, precision);
41
+ if (parts.noOfCoefficientDecimals < parts.exponent) {
42
+ const number = parts.invariantCoefficientString.replace(invariantDecimalSeparator, "");
43
+ const zeros = parts.exponent - parts.noOfCoefficientDecimals;
44
+ return `${number}${"0".repeat(zeros)}`;
45
+ }
46
+ else {
47
+ return parts.invariantPrecisionString;
48
+ }
49
+ }
50
+
51
+ public toLocaleFixed(value: number, precision: number) {
52
+ return this.toFixed(value, precision)
53
+ .replace(invariantDecimalSeparator, localeDecimalSeparator);
54
+ }
55
+
56
+ public getParts(value: number, precision: number): Parts {
57
+ const precisionString = this.toPrecision(value, precision);
58
+ if (precisionString.indexOf("e") >= 0) {
59
+ const match = /(.*)e([\+-]\d+)$/.exec(precisionString)!;
60
+ const coefficientString = match[1];
61
+ const exponent = parseInt(match[2]);
62
+ const decimalSeparatorIndex = coefficientString.indexOf(invariantDecimalSeparator);
63
+ const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
64
+ ? coefficientString.length - decimalSeparatorIndex - 1
65
+ : 0;
66
+ return {
67
+ invariantPrecisionString: precisionString,
68
+ invariantCoefficientString: coefficientString,
69
+ exponent,
70
+ noOfCoefficientDecimals,
71
+ };
72
+ }
73
+ else {
74
+ const decimalSeparatorIndex = precisionString.indexOf(invariantDecimalSeparator);
75
+ const noOfCoefficientDecimals = decimalSeparatorIndex >= 0
76
+ ? precisionString.length - decimalSeparatorIndex - 1
77
+ : 0;
78
+ return {
79
+ invariantPrecisionString: precisionString,
80
+ invariantCoefficientString: precisionString,
81
+ exponent: 0,
82
+ noOfCoefficientDecimals,
83
+ };
84
+ }
85
+ }
86
+ }
87
+
88
+ export const NumberFormatter = new Formatter();
@@ -0,0 +1 @@
1
+ export type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
@@ -0,0 +1,28 @@
1
+ import { expect } from "chai";
2
+ import { PrecisionNumber } from "./precision-number";
3
+
4
+ describe("precision-number", () => {
5
+ it("should not parse ''", () => {
6
+ const number = PrecisionNumber.parse("");
7
+ expect(Number.isNaN(number.value)).to.equal(true);
8
+ expect(number.precision).to.equal(0);
9
+ });
10
+
11
+ it("should parse '1'", () => {
12
+ const number = PrecisionNumber.parse("1");
13
+ expect(number.value).to.equal(1);
14
+ expect(number.precision).to.equal(1);
15
+ });
16
+
17
+ it("should parse '1.'", () => {
18
+ const number = PrecisionNumber.parse("1.");
19
+ expect(number.value).to.equal(1);
20
+ expect(number.precision).to.equal(2);
21
+ });
22
+
23
+ it("should parse '1.0'", () => {
24
+ const number = PrecisionNumber.parse("1.0");
25
+ expect(number.value).to.equal(1);
26
+ expect(number.precision).to.equal(2);
27
+ });
28
+ });
@@ -0,0 +1,36 @@
1
+ import { invariantDecimalSeparator } from ".";
2
+ import { localeDecimalSeparator, NumberFormatter } from "./number-formatter";
3
+
4
+ export class PrecisionNumber {
5
+ constructor(public value: number, public precision: number) {
6
+ }
7
+
8
+ static parse(input: string) {
9
+ let normalized = input.trim().replace(",", ".").replace(localeDecimalSeparator, invariantDecimalSeparator);
10
+ if (normalized.endsWith(invariantDecimalSeparator)) {
11
+ normalized += "0";
12
+ }
13
+ const precision = normalized.replace(".", "").replace("+", "").replace("-", "").length;
14
+ return new PrecisionNumber(parseFloat(normalized), precision);
15
+ }
16
+
17
+ toString() {
18
+ return this.value.toString();
19
+ }
20
+
21
+ toFixed() {
22
+ return NumberFormatter.toFixed(this.value, this.precision);
23
+ }
24
+
25
+ toPrecision() {
26
+ return NumberFormatter.toPrecision(this.value, this.precision);
27
+ }
28
+
29
+ toLocaleFixed() {
30
+ return NumberFormatter.toLocaleFixed(this.value, this.precision);
31
+ }
32
+
33
+ toLocalePrecision() {
34
+ return NumberFormatter.toLocalePrecision(this.value, this.precision);
35
+ }
36
+ }
@@ -0,0 +1,43 @@
1
+ import { expect } from "chai";
2
+ import { RegisterConvention } from "./register-convention";
3
+
4
+ describe("getAggregate", () => {
5
+ it("instant register is instant", () => {
6
+ const result = RegisterConvention.getAggregate("flowTemperature");
7
+ expect(result).to.equal("instant");
8
+ });
9
+
10
+ it("avg register is avg", () => {
11
+ const result = RegisterConvention.getAggregate("avgFlowTemperature");
12
+ expect(result).to.equal("avg");
13
+ });
14
+ });
15
+
16
+ describe("toInstant", () => {
17
+ it("should convert instant", () => {
18
+ const registerId = RegisterConvention.toInstant("flowTemperature");
19
+ expect(registerId).to.equal("flowTemperature");
20
+ });
21
+
22
+ it("should convert aggregate", () => {
23
+ const registerId = RegisterConvention.toInstant("avgFlowTemperature");
24
+ expect(registerId).to.equal("flowTemperature");
25
+ });
26
+ });
27
+
28
+ describe("toAggregate", () => {
29
+ it("should convert instant to aggregate", () => {
30
+ const registerId = RegisterConvention.toAggregate("flowTemperature", "avg");
31
+ expect(registerId).to.equal("avgFlowTemperature");
32
+ });
33
+
34
+ it("should convert aggregate to same aggregate", () => {
35
+ const registerId = RegisterConvention.toAggregate("avgFlowTemperature", "avg");
36
+ expect(registerId).to.equal("avgFlowTemperature");
37
+ });
38
+
39
+ it("should convert aggregate to other aggregate", () => {
40
+ const registerId = RegisterConvention.toAggregate("avgFlowTemperature", "min");
41
+ expect(registerId).to.equal("minFlowTemperature");
42
+ });
43
+ });
@@ -0,0 +1,61 @@
1
+ import { RegisterDefinition } from ".";
2
+ import { AggregateId } from "./aggregate-id";
3
+ import { RegisterId } from "./register-id";
4
+ import { getRegisterId } from "./register-utils";
5
+
6
+ export const aggregatePrefixes: { [aggregateId in AggregateId]: string } = {
7
+ instant: "",
8
+ diff: "diff",
9
+ min: "min",
10
+ max: "max",
11
+ avg: "avg",
12
+ sum: "sum",
13
+ cnt: "cnt",
14
+ ssq: "ssq",
15
+ };
16
+
17
+ class Convention {
18
+ public getAggregate(register: RegisterId | RegisterDefinition) {
19
+ const registerId = getRegisterId(register);
20
+ // Brute force all prefixes - if none of them, then it must be instant.
21
+ for (const aggregateString of Object.keys(aggregatePrefixes)) {
22
+ const aggregate = <AggregateId>aggregateString;
23
+ const prefix = aggregatePrefixes[aggregate];
24
+ if (prefix.length && isPrefix(registerId, prefix)) {
25
+ // Not instant - it was any of the others.
26
+ return aggregate;
27
+ }
28
+ }
29
+ return "instant";
30
+ }
31
+
32
+ toInstant(register: RegisterId | RegisterDefinition) {
33
+ const registerId = getRegisterId(register);
34
+ for (const prefix of Object.values(aggregatePrefixes)) {
35
+ if (prefix.length && isPrefix(registerId, prefix)) {
36
+ const tail = registerId.substr(prefix.length);
37
+ return <RegisterId>(tail[0].toLowerCase() + tail.substr(1));
38
+ }
39
+ }
40
+ return registerId;
41
+ }
42
+
43
+ toAggregate(register: RegisterId | RegisterDefinition, aggregate: AggregateId) {
44
+ const instantRegisterId = this.toInstant(register);
45
+ if (aggregate === "instant") {
46
+ return instantRegisterId;
47
+ }
48
+ const prefix = aggregatePrefixes[aggregate];
49
+ return <RegisterId>(prefix + instantRegisterId[0].toUpperCase() + instantRegisterId.substr(1));
50
+ }
51
+ }
52
+
53
+ export function isPrefix(registerId: RegisterId, prefix: string) {
54
+ return registerId.startsWith(prefix) && isUppercase(registerId[prefix.length]);
55
+ }
56
+
57
+ function isUppercase(input: string) {
58
+ return input.toUpperCase() === input;
59
+ }
60
+
61
+ export const RegisterConvention = new Convention();