@utiliread/primitives 1.0.0-100 → 1.0.0-101

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.
@@ -860,24 +860,24 @@
860
860
  "name": "Kørselstidspunkt"
861
861
  },
862
862
  "instantTime": {
863
- "brief": "Forespørgelsestid, tidspunkt",
864
- "name": "Forespørgelsestid, tidspunkt"
863
+ "brief": "Tidspunkt",
864
+ "name": "Tidspunkt"
865
865
  },
866
866
  "periodStartTime": {
867
- "brief": "Forespørgelsestid, fra",
868
- "name": "Forespørgelsestid, fra"
867
+ "brief": "Periode fra-tidspunkt",
868
+ "name": "Periode fra-tidspunkt"
869
869
  },
870
870
  "periodEndTime": {
871
- "brief": "Forespørgelsestid, til",
872
- "name": "Forespørgelsestid, til"
871
+ "brief": "Periode til-tidspunkt",
872
+ "name": "Periode til-tidspunkt"
873
873
  },
874
874
  "instantOrPeriodStartTime": {
875
- "brief": "Forespørgelsestid, tidspunkt eller fra",
876
- "name": "Forespørgelsestid, tidspunkt eller fra"
875
+ "brief": "Tidspunkt eller periode fra-tidspunkt",
876
+ "name": "Tidspunkt eller periode fra-tidspunkt"
877
877
  },
878
878
  "instantOrPeriodEndTime": {
879
- "brief": "Forespørgelsestid, tidspunkt eller til",
880
- "name": "Forespørgelsestid, tidspunkt eller til"
879
+ "brief": "Tidspunkt eller periode til-tidspunkt",
880
+ "name": "Tidspunkt eller periode til-tidspunkt"
881
881
  },
882
882
  "device": {
883
883
  "brief": "Enhed",
@@ -860,24 +860,24 @@
860
860
  "name": "Run time"
861
861
  },
862
862
  "instantTime": {
863
- "brief": "Forespørgelsestid, tidspunkt",
864
- "name": "Forespørgelsestid, tidspunkt"
863
+ "brief": "Time",
864
+ "name": "Time"
865
865
  },
866
866
  "periodStartTime": {
867
- "brief": "Forespørgelsestid, fra",
868
- "name": "Forespørgelsestid, fra"
867
+ "brief": "Period from-time",
868
+ "name": "Period from-time"
869
869
  },
870
870
  "periodEndTime": {
871
- "brief": "Forespørgelsestid, til",
872
- "name": "Forespørgelsestid, til"
871
+ "brief": "Period to-time",
872
+ "name": "Period to-time"
873
873
  },
874
874
  "instantOrPeriodStartTime": {
875
- "brief": "Forespørgelsestid, tidspunkt eller fra",
876
- "name": "Forespørgelsestid, tidspunkt eller fra"
875
+ "brief": "Time or period from-time",
876
+ "name": "Time or period from-time"
877
877
  },
878
878
  "instantOrPeriodEndTime": {
879
- "brief": "Forespørgelsestid, tidspunkt eller til",
880
- "name": "Forespørgelsestid, tidspunkt eller til"
879
+ "brief": "Time or period to-time",
880
+ "name": "Time or period to-time"
881
881
  },
882
882
  "device": {
883
883
  "brief": "Device",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@utiliread/primitives",
3
- "version": "1.0.0-100",
3
+ "version": "1.0.0-101",
4
4
  "main": "dist/cjs/index.js",
5
5
  "module": "dist/esm/index.js",
6
6
  "types": "dist/esm/index.d.ts",
@@ -1,63 +1,63 @@
1
- import { BreakdownRule } from "./breakdown-rule";
2
- import { DateTime } from "luxon";
3
- import { TimeOrRule } from ".";
4
- import { expect } from "chai";
5
-
6
- describe("ctor", () => {
7
- it("creates", () => {
8
- expect(
9
- BreakdownRule({ origin: "", count: 1, period: "hour" }),
10
- ).to.equal("/1hour");
11
- expect(
12
- BreakdownRule({ origin: "", count: 2, period: "hour" }),
13
- ).to.equal("/2hours");
14
- expect(
15
- BreakdownRule({ origin: "day", count: 1, period: "hour" }),
16
- ).to.equal("day/1hour");
17
- expect(
18
- BreakdownRule({ origin: "day", count: 2, period: "hour" }),
19
- ).to.equal("day/2hours");
20
-
21
- expect(BreakdownRule("", 1, "hour")).to.equal("/1hour");
22
- expect(BreakdownRule("", 2, "hour")).to.equal("/2hours");
23
- expect(BreakdownRule("day", 1, "hour")).to.equal("day/1hour");
24
- expect(BreakdownRule("day", 2, "hour")).to.equal("day/2hours");
25
- });
26
- });
27
-
28
- describe("split", () => {
29
- it("splits without origin", () => {
30
- const { origin, count, period } = BreakdownRule.split("/1hour");
31
- expect(origin).to.equal("");
32
- expect(count).to.equal(1);
33
- expect(period).to.equal("hour");
34
- });
35
-
36
- it("splits with simple origin", () => {
37
- const { origin, count, period } = BreakdownRule.split("day/1hour");
38
- expect(origin).to.equal("day");
39
- expect(count).to.equal(1);
40
- expect(period).to.equal("hour");
41
- });
42
-
43
- it("splits with complex origin and plural count", () => {
44
- const { origin, count, period } = BreakdownRule.split(
45
- "day+PT1H_year^month/10hours",
46
- );
47
- expect(origin).to.equal("day+PT1H_year^month");
48
- expect(count).to.equal(10);
49
- expect(period).to.equal("hour");
50
- });
51
-
52
- it("splits with time origin", () => {
53
- const { origin, count, period } = BreakdownRule.split(
54
- "2024-01-25T00:00:00.000+01:00/1hour",
55
- );
56
- expect(TimeOrRule.isTime(origin)).to.equal(true);
57
- expect(+origin).to.equal(
58
- +DateTime.local(2024, 1, 25),
59
- );
60
- expect(count).to.equal(1);
61
- expect(period).to.equal("hour");
62
- });
63
- });
1
+ import { BreakdownRule } from "./breakdown-rule";
2
+ import { DateTime } from "luxon";
3
+ import { TimeOrRule } from ".";
4
+ import { expect } from "chai";
5
+
6
+ describe("ctor", () => {
7
+ it("creates", () => {
8
+ expect(
9
+ BreakdownRule({ origin: "", count: 1, period: "hour" }),
10
+ ).to.equal("/1hour");
11
+ expect(
12
+ BreakdownRule({ origin: "", count: 2, period: "hour" }),
13
+ ).to.equal("/2hours");
14
+ expect(
15
+ BreakdownRule({ origin: "day", count: 1, period: "hour" }),
16
+ ).to.equal("day/1hour");
17
+ expect(
18
+ BreakdownRule({ origin: "day", count: 2, period: "hour" }),
19
+ ).to.equal("day/2hours");
20
+
21
+ expect(BreakdownRule("", 1, "hour")).to.equal("/1hour");
22
+ expect(BreakdownRule("", 2, "hour")).to.equal("/2hours");
23
+ expect(BreakdownRule("day", 1, "hour")).to.equal("day/1hour");
24
+ expect(BreakdownRule("day", 2, "hour")).to.equal("day/2hours");
25
+ });
26
+ });
27
+
28
+ describe("split", () => {
29
+ it("splits without origin", () => {
30
+ const { origin, count, period } = BreakdownRule.split("/1hour");
31
+ expect(origin).to.equal("");
32
+ expect(count).to.equal(1);
33
+ expect(period).to.equal("hour");
34
+ });
35
+
36
+ it("splits with simple origin", () => {
37
+ const { origin, count, period } = BreakdownRule.split("day/1hour");
38
+ expect(origin).to.equal("day");
39
+ expect(count).to.equal(1);
40
+ expect(period).to.equal("hour");
41
+ });
42
+
43
+ it("splits with complex origin and plural count", () => {
44
+ const { origin, count, period } = BreakdownRule.split(
45
+ "day+PT1H_year^month/10hours",
46
+ );
47
+ expect(origin).to.equal("day+PT1H_year^month");
48
+ expect(count).to.equal(10);
49
+ expect(period).to.equal("hour");
50
+ });
51
+
52
+ it("splits with time origin", () => {
53
+ const { origin, count, period } = BreakdownRule.split(
54
+ "2024-01-25T00:00:00.000+01:00/1hour",
55
+ );
56
+ expect(TimeOrRule.isTime(origin)).to.equal(true);
57
+ expect(+origin).to.equal(
58
+ +DateTime.local(2024, 1, 25),
59
+ );
60
+ expect(count).to.equal(1);
61
+ expect(period).to.equal("hour");
62
+ });
63
+ });
@@ -1,60 +1,60 @@
1
- import { TimeOrRule } from "./time-or-rule";
2
-
3
- export type BreakdownRule = string;
4
- export type BreakdownPeriod = "hour" | "day" | "week" | "month" | "year";
5
-
6
- interface BreakdownRuleInit {
7
- origin: TimeOrRule;
8
- count: number;
9
- period: BreakdownPeriod;
10
- }
11
-
12
- export function BreakdownRule(init: BreakdownRuleInit): BreakdownRule;
13
- export function BreakdownRule(
14
- origin: TimeOrRule,
15
- count: number,
16
- period: BreakdownPeriod,
17
- ): BreakdownRule;
18
- export function BreakdownRule(...params: any[]): BreakdownRule {
19
- let init: BreakdownRuleInit;
20
- switch (params.length) {
21
- case 1:
22
- init = params[0];
23
- break;
24
- case 3:
25
- init = {
26
- origin: params[0],
27
- count: params[1],
28
- period: params[2],
29
- };
30
- break;
31
- default:
32
- throw new TypeError("Invalid breakdown rule format");
33
- }
34
- const periodName =
35
- Math.abs(init.count) == 1 ? init.period : (init.period + "s");
36
- return `${init.origin}/${init.count}${periodName}`;
37
- }
38
-
39
- export namespace BreakdownRule {
40
- const regex = new RegExp(
41
- "(.*)/([-+]?[0-9]+)(hour|day|week|month|year)s?",
42
- );
43
- export function split(rule: BreakdownRule) {
44
- const match = regex.exec(rule);
45
- if (!match) {
46
- throw new Error("Invalid breakdown rule format");
47
- }
48
- const origin = match[1];
49
- const count = parseInt(match[2]);
50
- let period = match[3];
51
- if (period.endsWith("s")) {
52
- period = period.substring(0, period.length - 1);
53
- }
54
- return {
55
- origin: TimeOrRule.parse(origin),
56
- count,
57
- period: period as BreakdownPeriod,
58
- };
59
- }
60
- }
1
+ import { TimeOrRule } from "./time-or-rule";
2
+
3
+ export type BreakdownRule = string;
4
+ export type BreakdownPeriod = "hour" | "day" | "week" | "month" | "year";
5
+
6
+ interface BreakdownRuleInit {
7
+ origin: TimeOrRule;
8
+ count: number;
9
+ period: BreakdownPeriod;
10
+ }
11
+
12
+ export function BreakdownRule(init: BreakdownRuleInit): BreakdownRule;
13
+ export function BreakdownRule(
14
+ origin: TimeOrRule,
15
+ count: number,
16
+ period: BreakdownPeriod,
17
+ ): BreakdownRule;
18
+ export function BreakdownRule(...params: any[]): BreakdownRule {
19
+ let init: BreakdownRuleInit;
20
+ switch (params.length) {
21
+ case 1:
22
+ init = params[0];
23
+ break;
24
+ case 3:
25
+ init = {
26
+ origin: params[0],
27
+ count: params[1],
28
+ period: params[2],
29
+ };
30
+ break;
31
+ default:
32
+ throw new TypeError("Invalid breakdown rule format");
33
+ }
34
+ const periodName =
35
+ Math.abs(init.count) == 1 ? init.period : (init.period + "s");
36
+ return `${init.origin}/${init.count}${periodName}`;
37
+ }
38
+
39
+ export namespace BreakdownRule {
40
+ const regex = new RegExp(
41
+ "(.*)/([-+]?[0-9]+)(hour|day|week|month|year)s?",
42
+ );
43
+ export function split(rule: BreakdownRule) {
44
+ const match = regex.exec(rule);
45
+ if (!match) {
46
+ throw new Error("Invalid breakdown rule format");
47
+ }
48
+ const origin = match[1];
49
+ const count = parseInt(match[2]);
50
+ let period = match[3];
51
+ if (period.endsWith("s")) {
52
+ period = period.substring(0, period.length - 1);
53
+ }
54
+ return {
55
+ origin: TimeOrRule.parse(origin),
56
+ count,
57
+ period: period as BreakdownPeriod,
58
+ };
59
+ }
60
+ }
@@ -1,44 +1,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
- }
29
- else if (DateTime.isDateTime(input)) {
30
- return input;
31
- }
32
- else {
33
- const dateTime = DateTime.fromJSDate(input);
34
- if (!dateTime.isValid) {
35
- throw new Error("Unable to convert from Date to DateTime");
36
- }
37
- return dateTime;
38
- }
39
- }
40
-
41
- export function toJSON(input: TimeOrRule) {
42
- return isRule(input) ? input : input.toJSON();
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
+ }
29
+ else if (DateTime.isDateTime(input)) {
30
+ return input;
31
+ }
32
+ else {
33
+ const dateTime = DateTime.fromJSDate(input);
34
+ if (!dateTime.isValid) {
35
+ throw new Error("Unable to convert from Date to DateTime");
36
+ }
37
+ return dateTime;
38
+ }
39
+ }
40
+
41
+ export function toJSON(input: TimeOrRule) {
42
+ return isRule(input) ? input : input.toJSON();
43
+ }
44
+ }