@utiliread/primitives 1.0.0-31 → 1.0.0-35

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 (60) hide show
  1. package/dist/cjs/precision-number.js +6 -0
  2. package/dist/cjs/precision-number.js.map +1 -1
  3. package/dist/esm/precision-number.d.ts +2 -0
  4. package/dist/esm/precision-number.js +6 -0
  5. package/dist/esm/precision-number.js.map +1 -1
  6. package/dist/esm/register-id.d.ts +1 -1
  7. package/package.json +36 -36
  8. package/src/aggregate-id.ts +9 -9
  9. package/src/consumption-type-id.ts +8 -8
  10. package/src/index.ts +30 -28
  11. package/src/measure-id.ts +14 -14
  12. package/src/number-formatter.spec.ts +93 -93
  13. package/src/number-formatter.ts +78 -82
  14. package/src/precision-number.ts +26 -18
  15. package/src/register-convention.spec.ts +42 -42
  16. package/src/register-convention.ts +61 -61
  17. package/src/register-definition.ts +17 -17
  18. package/src/register-id.ts +197 -191
  19. package/src/register-library.spec.ts +41 -41
  20. package/src/register-library.ts +57 -57
  21. package/src/register-pack.ts +5 -5
  22. package/src/register-utils.ts +6 -6
  23. package/src/step.spec.ts +29 -29
  24. package/src/step.ts +37 -37
  25. package/src/time-or-rule.ts +17 -17
  26. package/src/unit-id.ts +100 -100
  27. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/base.css +0 -224
  28. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/block-navigation.js +0 -79
  29. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/favicon.png +0 -0
  30. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/index.html +0 -96
  31. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/prettify.css +0 -1
  32. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/prettify.js +0 -2
  33. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/sort-arrow-sprite.png +0 -0
  34. package/coverage/Chrome 90.0.4430.85 (Windows 10)/html/sorter.js +0 -170
  35. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/base.css +0 -224
  36. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/block-navigation.js +0 -79
  37. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/favicon.png +0 -0
  38. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/index.html +0 -96
  39. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.css +0 -1
  40. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/prettify.js +0 -2
  41. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sort-arrow-sprite.png +0 -0
  42. package/coverage/Chrome Headless 90.0.4430.85 (Windows 10)/html/sorter.js +0 -170
  43. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/base.css +0 -224
  44. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/block-navigation.js +0 -79
  45. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/favicon.png +0 -0
  46. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/index.html +0 -96
  47. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/prettify.css +0 -1
  48. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/prettify.js +0 -2
  49. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/sort-arrow-sprite.png +0 -0
  50. package/coverage/Chrome Headless 90.0.4430.93 (Windows 10)/html/sorter.js +0 -170
  51. package/dist/cjs/register-converter.js +0 -31
  52. package/dist/cjs/register-converter.js.map +0 -1
  53. package/dist/cjs/register-converter.spec.js +0 -29
  54. package/dist/cjs/register-converter.spec.js.map +0 -1
  55. package/dist/esm/register-converter.d.ts +0 -9
  56. package/dist/esm/register-converter.js +0 -28
  57. package/dist/esm/register-converter.js.map +0 -1
  58. package/dist/esm/register-converter.spec.d.ts +0 -1
  59. package/dist/esm/register-converter.spec.js +0 -27
  60. package/dist/esm/register-converter.spec.js.map +0 -1
@@ -1,41 +1,41 @@
1
- import { expect } from "chai";
2
- import { RegisterDefinition } from ".";
3
- import { RegisterLibrary } from "./register-library";
4
-
5
- const volume: RegisterDefinition = {
6
- id: "volume",
7
- measure: "volume",
8
- aggregate: "instant",
9
- units: ["cubicMeter"]
10
- };
11
-
12
- describe("library", () => {
13
- beforeEach(() => {
14
- RegisterLibrary.reset();
15
- });
16
-
17
- it("should get added definition", () => {
18
- RegisterLibrary.add(volume);
19
- const definition = RegisterLibrary.get("volume");
20
- expect(definition.id).to.equal("volume");
21
- });
22
-
23
- it("should add all definitions in pack", () => {
24
- RegisterLibrary.add({
25
- someRandomKey: volume
26
- });
27
- const definition = RegisterLibrary.get("volume");
28
- expect(definition.id).to.equal("volume");
29
- });
30
-
31
- it("should throw when getting not added register", () => {
32
- expect(() => RegisterLibrary.get("volume")).to.throw();
33
- });
34
-
35
- it("should return all definitions when invoking all()", () => {
36
- RegisterLibrary.add(volume);
37
- RegisterLibrary.add(volume); // Only added once.
38
- const all = RegisterLibrary.all();
39
- expect(all.length).to.equal(1);
40
- })
41
- });
1
+ import { expect } from "chai";
2
+ import { RegisterDefinition } from ".";
3
+ import { RegisterLibrary } from "./register-library";
4
+
5
+ const volume: RegisterDefinition = {
6
+ id: "volume",
7
+ measure: "volume",
8
+ aggregate: "instant",
9
+ units: ["cubicMeter"]
10
+ };
11
+
12
+ describe("library", () => {
13
+ beforeEach(() => {
14
+ RegisterLibrary.reset();
15
+ });
16
+
17
+ it("should get added definition", () => {
18
+ RegisterLibrary.add(volume);
19
+ const definition = RegisterLibrary.get("volume");
20
+ expect(definition.id).to.equal("volume");
21
+ });
22
+
23
+ it("should add all definitions in pack", () => {
24
+ RegisterLibrary.add({
25
+ someRandomKey: volume
26
+ });
27
+ const definition = RegisterLibrary.get("volume");
28
+ expect(definition.id).to.equal("volume");
29
+ });
30
+
31
+ it("should throw when getting not added register", () => {
32
+ expect(() => RegisterLibrary.get("volume")).to.throw();
33
+ });
34
+
35
+ it("should return all definitions when invoking all()", () => {
36
+ RegisterLibrary.add(volume);
37
+ RegisterLibrary.add(volume); // Only added once.
38
+ const all = RegisterLibrary.all();
39
+ expect(all.length).to.equal(1);
40
+ })
41
+ });
@@ -1,57 +1,57 @@
1
- import { RegisterDefinition } from "./register-definition";
2
- import { RegisterId } from "./register-id";
3
- import { RegisterPack } from "./register-pack";
4
-
5
- class Library {
6
- private definitions: Partial<{ [id in RegisterId]: RegisterDefinition }> = {};
7
- public add(...definitions: (RegisterDefinition | RegisterPack)[]) {
8
- for (const definition of definitions) {
9
- if (isDefinition(definition)) {
10
- this.tryAdd(definition);
11
- }
12
- else {
13
- for (const definitionInPack of Object.values(definition)) {
14
- this.tryAdd(definitionInPack);
15
- }
16
- }
17
- }
18
- }
19
-
20
- private tryAdd(definition: RegisterDefinition) {
21
- if (!this.definitions[definition.id]) {
22
- this.definitions[definition.id] = definition;
23
- return true;
24
- }
25
- return false;
26
- }
27
-
28
- public has(id: RegisterId) {
29
- return !!this.definitions[id];
30
- }
31
-
32
- public tryGet(id: RegisterId) {
33
- return this.definitions[id];
34
- }
35
-
36
- public get(id: RegisterId) {
37
- const definition = this.definitions[id];
38
- if (!definition) {
39
- throw new Error(`Defintion was not loaded for register ${id}`);
40
- }
41
- return definition;
42
- }
43
-
44
- public all() {
45
- return Object.values(this.definitions).map(x => x!);
46
- }
47
-
48
- public reset() {
49
- this.definitions = {};
50
- }
51
- }
52
-
53
- function isDefinition(definitionOrPack: RegisterDefinition | RegisterPack): definitionOrPack is RegisterDefinition {
54
- return !!(definitionOrPack.id && definitionOrPack.aggregate && definitionOrPack.units);
55
- }
56
-
57
- export const RegisterLibrary = new Library();
1
+ import { RegisterDefinition } from "./register-definition";
2
+ import { RegisterId } from "./register-id";
3
+ import { RegisterPack } from "./register-pack";
4
+
5
+ class Library {
6
+ private definitions: Partial<{ [id in RegisterId]: RegisterDefinition }> = {};
7
+ public add(...definitions: (RegisterDefinition | RegisterPack)[]) {
8
+ for (const definition of definitions) {
9
+ if (isDefinition(definition)) {
10
+ this.tryAdd(definition);
11
+ }
12
+ else {
13
+ for (const definitionInPack of Object.values(definition)) {
14
+ this.tryAdd(definitionInPack);
15
+ }
16
+ }
17
+ }
18
+ }
19
+
20
+ private tryAdd(definition: RegisterDefinition) {
21
+ if (!this.definitions[definition.id]) {
22
+ this.definitions[definition.id] = definition;
23
+ return true;
24
+ }
25
+ return false;
26
+ }
27
+
28
+ public has(id: RegisterId) {
29
+ return !!this.definitions[id];
30
+ }
31
+
32
+ public tryGet(id: RegisterId) {
33
+ return this.definitions[id];
34
+ }
35
+
36
+ public get(id: RegisterId) {
37
+ const definition = this.definitions[id];
38
+ if (!definition) {
39
+ throw new Error(`Defintion was not loaded for register ${id}`);
40
+ }
41
+ return definition;
42
+ }
43
+
44
+ public all() {
45
+ return Object.values(this.definitions).map(x => x!);
46
+ }
47
+
48
+ public reset() {
49
+ this.definitions = {};
50
+ }
51
+ }
52
+
53
+ function isDefinition(definitionOrPack: RegisterDefinition | RegisterPack): definitionOrPack is RegisterDefinition {
54
+ return !!(definitionOrPack.id && definitionOrPack.aggregate && definitionOrPack.units);
55
+ }
56
+
57
+ export const RegisterLibrary = new Library();
@@ -1,5 +1,5 @@
1
- import { RegisterDefinition } from "./register-definition";
2
-
3
- export interface RegisterPack {
4
- [key: string]: RegisterDefinition;
5
- }
1
+ import { RegisterDefinition } from "./register-definition";
2
+
3
+ export interface RegisterPack {
4
+ [key: string]: RegisterDefinition;
5
+ }
@@ -1,6 +1,6 @@
1
- import { RegisterDefinition } from "./register-definition";
2
- import { RegisterId } from "./register-id";
3
-
4
- export function getRegisterId(register: RegisterId | RegisterDefinition) {
5
- return typeof register === "string" ? register : register.id;
6
- }
1
+ import { RegisterDefinition } from "./register-definition";
2
+ import { RegisterId } from "./register-id";
3
+
4
+ export function getRegisterId(register: RegisterId | RegisterDefinition) {
5
+ return typeof register === "string" ? register : register.id;
6
+ }
package/src/step.spec.ts CHANGED
@@ -1,30 +1,30 @@
1
- import { expect } from "chai";
2
- import { DateTime } from "luxon";
3
- import { DayOfWeek, Step } from "./step";
4
-
5
- describe("startOf", () => {
6
- it("returns start of day", () => {
7
- const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
8
- const result = Step.startOf(dt, "day");
9
- expect(+result).to.equal(+DateTime.fromISO("2020-01-11T00:00:00+01:00"));
10
- });
11
-
12
- it("returns previous monday from saturday", () => {
13
- const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
14
- const result = Step.startOf(dt, "week", DayOfWeek.Monday);
15
- expect(+result).to.equal(+DateTime.fromISO("2020-01-06T00:00:00+01:00"));
16
- });
17
-
18
- it("returns same monday from monday with hour", () => {
19
- const dt = DateTime.fromISO("2020-01-13T05:00:00+01:00");
20
- const result = Step.startOf(dt, "week", DayOfWeek.Monday);
21
- expect(+result).to.equal(+DateTime.fromISO("2020-01-13T00:00:00+01:00"));
22
- });
1
+ import { expect } from "chai";
2
+ import { DateTime } from "luxon";
3
+ import { DayOfWeek, Step } from "./step";
4
+
5
+ describe("startOf", () => {
6
+ it("returns start of day", () => {
7
+ const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
8
+ const result = Step.startOf(dt, "day");
9
+ expect(+result).to.equal(+DateTime.fromISO("2020-01-11T00:00:00+01:00"));
10
+ });
11
+
12
+ it("returns previous monday from saturday", () => {
13
+ const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
14
+ const result = Step.startOf(dt, "week", DayOfWeek.Monday);
15
+ expect(+result).to.equal(+DateTime.fromISO("2020-01-06T00:00:00+01:00"));
16
+ });
17
+
18
+ it("returns same monday from monday with hour", () => {
19
+ const dt = DateTime.fromISO("2020-01-13T05:00:00+01:00");
20
+ const result = Step.startOf(dt, "week", DayOfWeek.Monday);
21
+ expect(+result).to.equal(+DateTime.fromISO("2020-01-13T00:00:00+01:00"));
22
+ });
23
+ });
24
+
25
+ describe("asDuration", () => {
26
+ it("should return equivalent duration", () => {
27
+ const duration = Step.asDuration("week");
28
+ expect(duration.days).to.equal(7);
29
+ });
23
30
  });
24
-
25
- describe("asDuration", () => {
26
- it("should return equivalent duration", () => {
27
- const duration = Step.asDuration("week");
28
- expect(duration.days).to.equal(7);
29
- });
30
- });
package/src/step.ts CHANGED
@@ -1,37 +1,37 @@
1
- import { DateTime, Duration } from "luxon";
2
-
3
- export type Step = "hour" | "day" | "week" | "month" | "year";
4
-
5
- export enum DayOfWeek {
6
- Monday = 1,
7
- Tuesday = 2,
8
- Wednesday = 3,
9
- Thursday = 4,
10
- Friday = 5,
11
- Saturday = 6,
12
- Sunday = 7,
13
- };
14
-
15
- export namespace Step {
16
- export function startOf(dateTime: DateTime, step: Step, firstDayOfWeek?: DayOfWeek) {
17
- if (step === "week") {
18
- const value = dateTime.startOf("day").set({ weekday: firstDayOfWeek ?? DayOfWeek.Monday });
19
- return value > dateTime
20
- ? value.minus({ days: 7 })
21
- : value;
22
- }
23
- else {
24
- return dateTime.startOf(step);
25
- }
26
- }
27
-
28
- export function asDuration(step: Step, amount = 1): Duration {
29
- switch (step) {
30
- case "hour": return Duration.fromObject({ hours: amount });
31
- case "day": return Duration.fromObject({ days: amount });
32
- case "week": return Duration.fromObject({ days: 7 * amount });
33
- case "month": return Duration.fromObject({ months: amount });
34
- case "year": return Duration.fromObject({ years: amount });
35
- }
36
- }
37
- }
1
+ import { DateTime, Duration } from "luxon";
2
+
3
+ export type Step = "hour" | "day" | "week" | "month" | "year";
4
+
5
+ export enum DayOfWeek {
6
+ Monday = 1,
7
+ Tuesday = 2,
8
+ Wednesday = 3,
9
+ Thursday = 4,
10
+ Friday = 5,
11
+ Saturday = 6,
12
+ Sunday = 7,
13
+ };
14
+
15
+ export namespace Step {
16
+ export function startOf(dateTime: DateTime, step: Step, firstDayOfWeek?: DayOfWeek) {
17
+ if (step === "week") {
18
+ const value = dateTime.startOf("day").set({ weekday: firstDayOfWeek ?? DayOfWeek.Monday });
19
+ return value > dateTime
20
+ ? value.minus({ days: 7 })
21
+ : value;
22
+ }
23
+ else {
24
+ return dateTime.startOf(step);
25
+ }
26
+ }
27
+
28
+ export function asDuration(step: Step, amount = 1): Duration {
29
+ switch (step) {
30
+ case "hour": return Duration.fromObject({ hours: amount });
31
+ case "day": return Duration.fromObject({ days: amount });
32
+ case "week": return Duration.fromObject({ days: 7 * amount });
33
+ case "month": return Duration.fromObject({ months: amount });
34
+ case "year": return Duration.fromObject({ years: amount });
35
+ }
36
+ }
37
+ }
@@ -1,17 +1,17 @@
1
- import { DateTime } from "luxon";
2
-
3
- export type TimeOrRule = Date | DateTime | string;
4
-
5
- export namespace TimeOrRule {
6
- export function isTime(input: TimeOrRule): input is Date | DateTime {
7
- return typeof input !== "string";
8
- }
9
-
10
- export function isRule(input: TimeOrRule): input is string {
11
- return typeof input === "string";
12
- }
13
-
14
- export function asJSON(input: TimeOrRule) {
15
- return isRule(input) ? input : input.toJSON();
16
- }
17
- }
1
+ import { DateTime } from "luxon";
2
+
3
+ export type TimeOrRule = Date | DateTime | string;
4
+
5
+ export namespace TimeOrRule {
6
+ export function isTime(input: TimeOrRule): input is Date | DateTime {
7
+ return typeof input !== "string";
8
+ }
9
+
10
+ export function isRule(input: TimeOrRule): input is string {
11
+ return typeof input === "string";
12
+ }
13
+
14
+ export function asJSON(input: TimeOrRule) {
15
+ return isRule(input) ? input : input.toJSON();
16
+ }
17
+ }
package/src/unit-id.ts CHANGED
@@ -1,100 +1,100 @@
1
- export type UnitId =
2
- "unitless" |
3
-
4
- "milliWattHour" |
5
- "wattHour" |
6
- "kiloWattHour" |
7
- "megaWattHour" |
8
- "gigaWattHour" |
9
- "calorie" |
10
- "kiloCalorie" |
11
- "megaCalorie" |
12
-
13
- "joule" |
14
- "kiloJoule" |
15
- "megaJoule" |
16
- "gigaJoule" |
17
-
18
- "liter" |
19
- "cubicMeter" |
20
-
21
- "fluidOunce" |
22
- "gallon" |
23
-
24
- "gram" |
25
- "kiloGram" |
26
- "tonne" |
27
-
28
- "ounce" |
29
- "pound" |
30
- "ton" |
31
-
32
- "second" |
33
- "minute" |
34
- "hour" |
35
- "day" |
36
- "week" |
37
- "month" |
38
- "year" |
39
-
40
- "milliWatt" |
41
- "watt" |
42
- "kiloWatt" |
43
- "megaWatt" |
44
- "gigaWatt" |
45
-
46
- "joulesPerSecond" |
47
- "joulesPerMinute" |
48
- "joulesPerHour" |
49
-
50
- "litersPerSecond" |
51
- "litersPerMinute" |
52
- "litersPerHour" |
53
-
54
- "cubicMetersPerSecond" |
55
- "cubicMetersPerMinute" |
56
- "cubicMetersPerHour" |
57
-
58
- "fluidOuncesPerSecond" |
59
- "fluidOuncesPerMinute" |
60
- "fluidOuncesPerHour" |
61
-
62
- "gallonsPerSecond" |
63
- "gallonsPerMinute" |
64
- "gallonsPerHour" |
65
-
66
- "gramsPerSecond" |
67
- "gramsPerMinute" |
68
- "gramsPerHour" |
69
-
70
- "kiloGramsPerSecond" |
71
- "kiloGramsPerMinute" |
72
- "kiloGramsPerHour" |
73
-
74
- "celcius" |
75
- "kelvin" |
76
- "fahrenheit" |
77
-
78
- "var" |
79
- "kiloVar" |
80
- "megaVar" |
81
-
82
- "varHour" |
83
- "kiloVarHour" |
84
- "megaVarHour" |
85
-
86
- "percentage" |
87
-
88
- "literCelcius" |
89
- "cubicMeterCelcius";
90
-
91
- export const energyUnits: UnitId[] = [
92
- "milliWattHour",
93
- "wattHour",
94
- "kiloWattHour",
95
- "megaWattHour",
96
- "gigaWattHour",
97
- "calorie",
98
- "kiloCalorie",
99
- "megaCalorie",
100
- ];
1
+ export type UnitId =
2
+ "unitless" |
3
+
4
+ "milliWattHour" |
5
+ "wattHour" |
6
+ "kiloWattHour" |
7
+ "megaWattHour" |
8
+ "gigaWattHour" |
9
+ "calorie" |
10
+ "kiloCalorie" |
11
+ "megaCalorie" |
12
+
13
+ "joule" |
14
+ "kiloJoule" |
15
+ "megaJoule" |
16
+ "gigaJoule" |
17
+
18
+ "liter" |
19
+ "cubicMeter" |
20
+
21
+ "fluidOunce" |
22
+ "gallon" |
23
+
24
+ "gram" |
25
+ "kiloGram" |
26
+ "tonne" |
27
+
28
+ "ounce" |
29
+ "pound" |
30
+ "ton" |
31
+
32
+ "second" |
33
+ "minute" |
34
+ "hour" |
35
+ "day" |
36
+ "week" |
37
+ "month" |
38
+ "year" |
39
+
40
+ "milliWatt" |
41
+ "watt" |
42
+ "kiloWatt" |
43
+ "megaWatt" |
44
+ "gigaWatt" |
45
+
46
+ "joulesPerSecond" |
47
+ "joulesPerMinute" |
48
+ "joulesPerHour" |
49
+
50
+ "litersPerSecond" |
51
+ "litersPerMinute" |
52
+ "litersPerHour" |
53
+
54
+ "cubicMetersPerSecond" |
55
+ "cubicMetersPerMinute" |
56
+ "cubicMetersPerHour" |
57
+
58
+ "fluidOuncesPerSecond" |
59
+ "fluidOuncesPerMinute" |
60
+ "fluidOuncesPerHour" |
61
+
62
+ "gallonsPerSecond" |
63
+ "gallonsPerMinute" |
64
+ "gallonsPerHour" |
65
+
66
+ "gramsPerSecond" |
67
+ "gramsPerMinute" |
68
+ "gramsPerHour" |
69
+
70
+ "kiloGramsPerSecond" |
71
+ "kiloGramsPerMinute" |
72
+ "kiloGramsPerHour" |
73
+
74
+ "celcius" |
75
+ "kelvin" |
76
+ "fahrenheit" |
77
+
78
+ "var" |
79
+ "kiloVar" |
80
+ "megaVar" |
81
+
82
+ "varHour" |
83
+ "kiloVarHour" |
84
+ "megaVarHour" |
85
+
86
+ "percentage" |
87
+
88
+ "literCelcius" |
89
+ "cubicMeterCelcius";
90
+
91
+ export const energyUnits: UnitId[] = [
92
+ "milliWattHour",
93
+ "wattHour",
94
+ "kiloWattHour",
95
+ "megaWattHour",
96
+ "gigaWattHour",
97
+ "calorie",
98
+ "kiloCalorie",
99
+ "megaCalorie",
100
+ ];