@utiliread/primitives 1.0.0-102 → 1.0.0-104
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/dist/cjs/breakdown-rule.js +2 -3
- package/dist/cjs/breakdown-rule.js.map +1 -1
- package/dist/cjs/breakdown-rule.spec.js.map +1 -1
- package/dist/cjs/measures/energy.js +1 -0
- package/dist/cjs/measures/energy.js.map +1 -1
- package/dist/cjs/measures/pressure.js +5 -1
- package/dist/cjs/measures/pressure.js.map +1 -1
- package/dist/cjs/register-convention.js +2 -2
- package/dist/cjs/register-convention.js.map +1 -1
- package/dist/cjs/register-utils.js +1 -2
- package/dist/cjs/register-utils.js.map +1 -1
- package/dist/cjs/registers/kamstrup-multical-infocode.js +13 -1
- package/dist/cjs/registers/kamstrup-multical-infocode.js.map +1 -1
- package/dist/cjs/repository.js +1 -2
- package/dist/cjs/repository.js.map +1 -1
- package/dist/cjs/time-or-rule.js +1 -2
- package/dist/cjs/time-or-rule.js.map +1 -1
- package/dist/esm/breakdown-rule.js +1 -1
- package/dist/esm/breakdown-rule.js.map +1 -1
- package/dist/esm/breakdown-rule.spec.js.map +1 -1
- package/dist/esm/measures/energy.js +1 -0
- package/dist/esm/measures/energy.js.map +1 -1
- package/dist/esm/measures/pressure.js +5 -1
- package/dist/esm/measures/pressure.js.map +1 -1
- package/dist/esm/register-id.d.ts +1 -1
- package/dist/esm/registers/kamstrup-multical-infocode.d.ts +2 -0
- package/dist/esm/registers/kamstrup-multical-infocode.js +12 -0
- package/dist/esm/registers/kamstrup-multical-infocode.js.map +1 -1
- package/dist/esm/time-or-rule.js.map +1 -1
- package/dist/esm/unit-id.d.ts +1 -1
- package/locales/da/aggregates.json +1 -1
- package/locales/da/measures.json +1 -1
- package/locales/da/mediums.json +1 -1
- package/locales/da/registers.json +1 -1
- package/locales/da/units.json +4 -1
- package/locales/en/aggregates.json +1 -1
- package/locales/en/measures.json +1 -1
- package/locales/en/mediums.json +1 -1
- package/locales/en/registers.json +1 -1
- package/locales/en/units.json +4 -1
- package/package.json +2 -2
- package/src/breakdown-rule.spec.ts +61 -63
- package/src/breakdown-rule.ts +58 -60
- package/src/measures/energy.ts +1 -0
- package/src/measures/pressure.ts +5 -1
- package/src/number-formatter.spec.ts +549 -549
- package/src/precision-number.spec.ts +28 -28
- package/src/register-convention.spec.ts +52 -52
- package/src/register-id.ts +2 -0
- package/src/register-library.spec.ts +41 -41
- package/src/registers/kamstrup-multical-infocode.ts +14 -0
- package/src/repository.spec.ts +20 -20
- package/src/step.spec.ts +37 -37
- package/src/time-or-rule.ts +42 -44
- package/src/unit-id.ts +1 -0
|
@@ -1,28 +1,28 @@
|
|
|
1
|
-
import { PrecisionNumber } from "./precision-number";
|
|
2
|
-
import { expect } from "chai";
|
|
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
|
-
});
|
|
1
|
+
import { PrecisionNumber } from "./precision-number";
|
|
2
|
+
import { expect } from "chai";
|
|
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
|
+
});
|
|
@@ -1,52 +1,52 @@
|
|
|
1
|
-
import { RegisterConvention } from "./register-convention";
|
|
2
|
-
import { expect } from "chai";
|
|
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(
|
|
31
|
-
"flowTemperature",
|
|
32
|
-
"avg",
|
|
33
|
-
);
|
|
34
|
-
expect(registerId).to.equal("avgFlowTemperature");
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it("should convert aggregate to same aggregate", () => {
|
|
38
|
-
const registerId = RegisterConvention.toAggregate(
|
|
39
|
-
"avgFlowTemperature",
|
|
40
|
-
"avg",
|
|
41
|
-
);
|
|
42
|
-
expect(registerId).to.equal("avgFlowTemperature");
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it("should convert aggregate to other aggregate", () => {
|
|
46
|
-
const registerId = RegisterConvention.toAggregate(
|
|
47
|
-
"avgFlowTemperature",
|
|
48
|
-
"min",
|
|
49
|
-
);
|
|
50
|
-
expect(registerId).to.equal("minFlowTemperature");
|
|
51
|
-
});
|
|
52
|
-
});
|
|
1
|
+
import { RegisterConvention } from "./register-convention";
|
|
2
|
+
import { expect } from "chai";
|
|
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(
|
|
31
|
+
"flowTemperature",
|
|
32
|
+
"avg",
|
|
33
|
+
);
|
|
34
|
+
expect(registerId).to.equal("avgFlowTemperature");
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should convert aggregate to same aggregate", () => {
|
|
38
|
+
const registerId = RegisterConvention.toAggregate(
|
|
39
|
+
"avgFlowTemperature",
|
|
40
|
+
"avg",
|
|
41
|
+
);
|
|
42
|
+
expect(registerId).to.equal("avgFlowTemperature");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("should convert aggregate to other aggregate", () => {
|
|
46
|
+
const registerId = RegisterConvention.toAggregate(
|
|
47
|
+
"avgFlowTemperature",
|
|
48
|
+
"min",
|
|
49
|
+
);
|
|
50
|
+
expect(registerId).to.equal("minFlowTemperature");
|
|
51
|
+
});
|
|
52
|
+
});
|
package/src/register-id.ts
CHANGED
|
@@ -263,6 +263,8 @@ export type RegisterId =
|
|
|
263
263
|
| "kamstrupMulticalInfocodeType3"
|
|
264
264
|
| "kamstrupMulticalInfocodeType4"
|
|
265
265
|
| "kamstrupMulticalInfocodeType5"
|
|
266
|
+
| "kamstrupMulticalInfocodeType6"
|
|
267
|
+
| "kamstrupMulticalInfocodeType7"
|
|
266
268
|
| "kamstrupMulticalInfocode"
|
|
267
269
|
| "kamstrupModuleType"
|
|
268
270
|
| "diehlInfocodeType1"
|
|
@@ -1,41 +1,41 @@
|
|
|
1
|
-
import { RegisterDefinition } from ".";
|
|
2
|
-
import { RegisterLibrary } from "./register-library";
|
|
3
|
-
import { expect } from "chai";
|
|
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 { RegisterDefinition } from ".";
|
|
2
|
+
import { RegisterLibrary } from "./register-library";
|
|
3
|
+
import { expect } from "chai";
|
|
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
|
+
});
|
|
@@ -42,3 +42,17 @@ export const kamstrupMulticalInfocodeType5: RegisterDefinition = {
|
|
|
42
42
|
aggregate: "instant",
|
|
43
43
|
units,
|
|
44
44
|
};
|
|
45
|
+
|
|
46
|
+
export const kamstrupMulticalInfocodeType6: RegisterDefinition = {
|
|
47
|
+
id: "kamstrupMulticalInfocodeType6",
|
|
48
|
+
measure: "none",
|
|
49
|
+
aggregate: "instant",
|
|
50
|
+
units,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const kamstrupMulticalInfocodeType7: RegisterDefinition = {
|
|
54
|
+
id: "kamstrupMulticalInfocodeType7",
|
|
55
|
+
measure: "none",
|
|
56
|
+
aggregate: "instant",
|
|
57
|
+
units,
|
|
58
|
+
};
|
package/src/repository.spec.ts
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { Repository } from "./repository";
|
|
2
|
-
import { expect } from "chai";
|
|
3
|
-
|
|
4
|
-
describe("ctor", () => {
|
|
5
|
-
it("creates", () => {
|
|
6
|
-
expect(Repository("gpkv", "gjoel")).to.equal("gpkv/gjoel");
|
|
7
|
-
expect(Repository("gpkv/gjoel")).to.equal("gpkv/gjoel");
|
|
8
|
-
});
|
|
9
|
-
it("throws if wrong format", () => {
|
|
10
|
-
expect(() => Repository("invalid")).to.throw();
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
describe("split", () => {
|
|
15
|
-
it("splits into correct parts", () => {
|
|
16
|
-
const { team, name } = Repository.split(Repository("gpkv", "gjoel"));
|
|
17
|
-
expect(team).to.equal("gpkv");
|
|
18
|
-
expect(name).to.equal("gjoel");
|
|
19
|
-
});
|
|
20
|
-
});
|
|
1
|
+
import { Repository } from "./repository";
|
|
2
|
+
import { expect } from "chai";
|
|
3
|
+
|
|
4
|
+
describe("ctor", () => {
|
|
5
|
+
it("creates", () => {
|
|
6
|
+
expect(Repository("gpkv", "gjoel")).to.equal("gpkv/gjoel");
|
|
7
|
+
expect(Repository("gpkv/gjoel")).to.equal("gpkv/gjoel");
|
|
8
|
+
});
|
|
9
|
+
it("throws if wrong format", () => {
|
|
10
|
+
expect(() => Repository("invalid")).to.throw();
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
describe("split", () => {
|
|
15
|
+
it("splits into correct parts", () => {
|
|
16
|
+
const { team, name } = Repository.split(Repository("gpkv", "gjoel"));
|
|
17
|
+
expect(team).to.equal("gpkv");
|
|
18
|
+
expect(name).to.equal("gjoel");
|
|
19
|
+
});
|
|
20
|
+
});
|
package/src/step.spec.ts
CHANGED
|
@@ -1,37 +1,37 @@
|
|
|
1
|
-
import { DayOfWeek, Step } from "./step";
|
|
2
|
-
|
|
3
|
-
import { DateTime } from "luxon";
|
|
4
|
-
import { expect } from "chai";
|
|
5
|
-
|
|
6
|
-
describe("startOf", () => {
|
|
7
|
-
it("returns start of day", () => {
|
|
8
|
-
const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
9
|
-
const result = Step.startOf(dt, "day");
|
|
10
|
-
expect(+result).to.equal(
|
|
11
|
-
+DateTime.fromISO("2020-01-11T00:00:00+01:00"),
|
|
12
|
-
);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it("returns previous monday from saturday", () => {
|
|
16
|
-
const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
17
|
-
const result = Step.startOf(dt, "week", DayOfWeek.Monday);
|
|
18
|
-
expect(+result).to.equal(
|
|
19
|
-
+DateTime.fromISO("2020-01-06T00:00:00+01:00"),
|
|
20
|
-
);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
it("returns same monday from monday with hour", () => {
|
|
24
|
-
const dt = DateTime.fromISO("2020-01-13T05:00:00+01:00");
|
|
25
|
-
const result = Step.startOf(dt, "week", DayOfWeek.Monday);
|
|
26
|
-
expect(+result).to.equal(
|
|
27
|
-
+DateTime.fromISO("2020-01-13T00:00:00+01:00"),
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
describe("asDuration", () => {
|
|
33
|
-
it("should return equivalent duration", () => {
|
|
34
|
-
const duration = Step.asDuration("week");
|
|
35
|
-
expect(duration.days).to.equal(7);
|
|
36
|
-
});
|
|
37
|
-
});
|
|
1
|
+
import { DayOfWeek, Step } from "./step";
|
|
2
|
+
|
|
3
|
+
import { DateTime } from "luxon";
|
|
4
|
+
import { expect } from "chai";
|
|
5
|
+
|
|
6
|
+
describe("startOf", () => {
|
|
7
|
+
it("returns start of day", () => {
|
|
8
|
+
const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
9
|
+
const result = Step.startOf(dt, "day");
|
|
10
|
+
expect(+result).to.equal(
|
|
11
|
+
+DateTime.fromISO("2020-01-11T00:00:00+01:00"),
|
|
12
|
+
);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("returns previous monday from saturday", () => {
|
|
16
|
+
const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
17
|
+
const result = Step.startOf(dt, "week", DayOfWeek.Monday);
|
|
18
|
+
expect(+result).to.equal(
|
|
19
|
+
+DateTime.fromISO("2020-01-06T00:00:00+01:00"),
|
|
20
|
+
);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("returns same monday from monday with hour", () => {
|
|
24
|
+
const dt = DateTime.fromISO("2020-01-13T05:00:00+01:00");
|
|
25
|
+
const result = Step.startOf(dt, "week", DayOfWeek.Monday);
|
|
26
|
+
expect(+result).to.equal(
|
|
27
|
+
+DateTime.fromISO("2020-01-13T00:00:00+01:00"),
|
|
28
|
+
);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
describe("asDuration", () => {
|
|
33
|
+
it("should return equivalent duration", () => {
|
|
34
|
+
const duration = Step.asDuration("week");
|
|
35
|
+
expect(duration.days).to.equal(7);
|
|
36
|
+
});
|
|
37
|
+
});
|
package/src/time-or-rule.ts
CHANGED
|
@@ -1,44 +1,42 @@
|
|
|
1
|
-
import { DateTime } from "luxon";
|
|
2
|
-
|
|
3
|
-
export type TimeOrRule = Date | DateTime<true> | string;
|
|
4
|
-
|
|
5
|
-
export function TimeOrRule(
|
|
6
|
-
timeOrRule: Date | DateTime<true> | string,
|
|
7
|
-
): TimeOrRule {
|
|
8
|
-
return timeOrRule;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export namespace TimeOrRule {
|
|
12
|
-
export function parse(input: string): TimeOrRule {
|
|
13
|
-
const time = DateTime.fromISO(input, { setZone: true });
|
|
14
|
-
return time.isValid ? time : input;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export function isTime(input: TimeOrRule): input is Date | DateTime<true> {
|
|
18
|
-
return typeof input !== "string";
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function isRule(input: TimeOrRule): input is string {
|
|
22
|
-
return typeof input === "string";
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export function asDateTime(input: TimeOrRule): DateTime<true> | undefined {
|
|
26
|
-
if (isRule(input)) {
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
import { DateTime } from "luxon";
|
|
2
|
+
|
|
3
|
+
export type TimeOrRule = Date | DateTime<true> | string;
|
|
4
|
+
|
|
5
|
+
export function TimeOrRule(
|
|
6
|
+
timeOrRule: Date | DateTime<true> | string,
|
|
7
|
+
): TimeOrRule {
|
|
8
|
+
return timeOrRule;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export namespace TimeOrRule {
|
|
12
|
+
export function parse(input: string): TimeOrRule {
|
|
13
|
+
const time = DateTime.fromISO(input, { setZone: true });
|
|
14
|
+
return time.isValid ? time : input;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function isTime(input: TimeOrRule): input is Date | DateTime<true> {
|
|
18
|
+
return typeof input !== "string";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isRule(input: TimeOrRule): input is string {
|
|
22
|
+
return typeof input === "string";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function asDateTime(input: TimeOrRule): DateTime<true> | undefined {
|
|
26
|
+
if (isRule(input)) {
|
|
27
|
+
return;
|
|
28
|
+
} else if (DateTime.isDateTime(input)) {
|
|
29
|
+
return input;
|
|
30
|
+
} else {
|
|
31
|
+
const dateTime = DateTime.fromJSDate(input);
|
|
32
|
+
if (!dateTime.isValid) {
|
|
33
|
+
throw new Error("Unable to convert from Date to DateTime");
|
|
34
|
+
}
|
|
35
|
+
return dateTime;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function toJSON(input: TimeOrRule) {
|
|
40
|
+
return isRule(input) ? input : input.toJSON();
|
|
41
|
+
}
|
|
42
|
+
}
|