@utiliread/primitives 1.0.0-94 → 1.0.0-96
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 +58 -0
- package/dist/cjs/breakdown-rule.js.map +1 -0
- package/dist/cjs/breakdown-rule.spec.js +41 -0
- package/dist/cjs/breakdown-rule.spec.js.map +1 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/number-formatter.spec.js +1 -1
- package/dist/cjs/number-formatter.spec.js.map +1 -1
- package/dist/cjs/precision-number.spec.js +1 -1
- package/dist/cjs/precision-number.spec.js.map +1 -1
- package/dist/cjs/register-convention.spec.js +1 -1
- package/dist/cjs/register-convention.spec.js.map +1 -1
- package/dist/cjs/register-library.spec.js +1 -1
- package/dist/cjs/register-library.spec.js.map +1 -1
- package/dist/cjs/repository.spec.js +1 -1
- package/dist/cjs/repository.spec.js.map +1 -1
- package/dist/cjs/step.spec.js +1 -1
- package/dist/cjs/step.spec.js.map +1 -1
- package/dist/esm/breakdown-rule.d.ts +20 -0
- package/dist/esm/breakdown-rule.js +54 -0
- package/dist/esm/breakdown-rule.js.map +1 -0
- package/dist/esm/breakdown-rule.spec.d.ts +1 -0
- package/dist/esm/breakdown-rule.spec.js +39 -0
- package/dist/esm/breakdown-rule.spec.js.map +1 -0
- package/dist/esm/index.d.ts +3 -1
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/number-formatter.spec.js +1 -1
- package/dist/esm/number-formatter.spec.js.map +1 -1
- package/dist/esm/precision-number.spec.js +1 -1
- package/dist/esm/precision-number.spec.js.map +1 -1
- package/dist/esm/register-convention.spec.js +1 -1
- package/dist/esm/register-convention.spec.js.map +1 -1
- package/dist/esm/register-library.spec.js +1 -1
- package/dist/esm/register-library.spec.js.map +1 -1
- package/dist/esm/repository.spec.js +1 -1
- package/dist/esm/repository.spec.js.map +1 -1
- package/dist/esm/step.spec.js +1 -1
- package/dist/esm/step.spec.js.map +1 -1
- package/package.json +3 -2
- package/src/breakdown-rule.spec.ts +44 -0
- package/src/breakdown-rule.ts +75 -0
- package/src/index.ts +10 -6
- 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-library.spec.ts +41 -41
- package/src/repository.spec.ts +20 -20
- package/src/step.spec.ts +37 -37
- package/tsconfig.cjs.json +1 -0
- package/tsconfig.json +1 -1
package/dist/esm/step.spec.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DayOfWeek, Step } from "./step";
|
|
2
2
|
import { DateTime } from "luxon";
|
|
3
|
-
import { expect } from "
|
|
3
|
+
import { expect } from "chai";
|
|
4
4
|
describe("startOf", () => {
|
|
5
5
|
it("returns start of day", () => {
|
|
6
6
|
const dt = DateTime.fromISO("2020-01-11T05:00:00+01:00");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"step.spec.js","sourceRoot":"","sources":["../../src/step.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEzC,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"step.spec.js","sourceRoot":"","sources":["../../src/step.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAEzC,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAE9B,QAAQ,CAAC,SAAS,EAAE,GAAG,EAAE;IACrB,EAAE,CAAC,sBAAsB,EAAE,GAAG,EAAE;QAC5B,MAAM,EAAE,GAAG,QAAQ,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC;QACzD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CAAC,CAAC;AACP,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@utiliread/primitives",
|
|
3
|
-
"version": "1.0.0-
|
|
3
|
+
"version": "1.0.0-96",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
6
6
|
"types": "dist/esm/index.d.ts",
|
|
@@ -27,11 +27,12 @@
|
|
|
27
27
|
"./*.d.ts": "./dist/esm/*.d.ts"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@
|
|
30
|
+
"@types/chai": "^4.3.11",
|
|
31
31
|
"@types/luxon": "^3.0.1",
|
|
32
32
|
"@types/mocha": "^10.0.0",
|
|
33
33
|
"@web/dev-server-esbuild": "^1.0.1",
|
|
34
34
|
"@web/test-runner": "^0.18.0",
|
|
35
|
+
"chai": "^5.0.0",
|
|
35
36
|
"luxon": "^3.0.4",
|
|
36
37
|
"mocha": "^10.0.0",
|
|
37
38
|
"prettier": "^3.1.0",
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BreakdownRule } from "./breakdown-rule";
|
|
2
|
+
import { expect } from "chai";
|
|
3
|
+
|
|
4
|
+
describe("ctor", () => {
|
|
5
|
+
it("creates", () => {
|
|
6
|
+
expect(BreakdownRule({ count: 1, period: "hour"})).to.equal("1hour");
|
|
7
|
+
expect(BreakdownRule({ count: 2, period: "hour"})).to.equal("2hours");
|
|
8
|
+
expect(BreakdownRule({ origin: "", count: 1, period: "hour"})).to.equal("1hour");
|
|
9
|
+
expect(BreakdownRule({ origin: "", count: 2, period: "hour"})).to.equal("2hours");
|
|
10
|
+
expect(BreakdownRule({ origin: "day", count: 1, period: "hour"})).to.equal("day/1hour");
|
|
11
|
+
expect(BreakdownRule({ origin: "day", count: 2, period: "hour"})).to.equal("day/2hours");
|
|
12
|
+
|
|
13
|
+
expect(BreakdownRule(1, "hour")).to.equal("1hour");
|
|
14
|
+
expect(BreakdownRule(2, "hour")).to.equal("2hours");
|
|
15
|
+
|
|
16
|
+
expect(BreakdownRule("", 1, "hour")).to.equal("1hour");
|
|
17
|
+
expect(BreakdownRule("", 2, "hour")).to.equal("2hours");
|
|
18
|
+
expect(BreakdownRule("day", 1, "hour")).to.equal("day/1hour");
|
|
19
|
+
expect(BreakdownRule("day", 2, "hour")).to.equal("day/2hours");
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("split", () => {
|
|
24
|
+
it("splits without origin", () => {
|
|
25
|
+
const { origin, count, period } = BreakdownRule.split("1hour");
|
|
26
|
+
expect(origin).to.equal("");
|
|
27
|
+
expect(count).to.equal(1);
|
|
28
|
+
expect(period).to.equal("hour");
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("splits with simple origin", () => {
|
|
32
|
+
const { origin, count, period } = BreakdownRule.split("day/1hour");
|
|
33
|
+
expect(origin).to.equal("day");
|
|
34
|
+
expect(count).to.equal(1);
|
|
35
|
+
expect(period).to.equal("hour");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("splits with complex origin and plural count", () => {
|
|
39
|
+
const { origin, count, period } = BreakdownRule.split("day+PT1H_year^month/10hours");
|
|
40
|
+
expect(origin).to.equal("day+PT1H_year^month");
|
|
41
|
+
expect(count).to.equal(10);
|
|
42
|
+
expect(period).to.equal("hour");
|
|
43
|
+
});
|
|
44
|
+
});
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Step } from "./step";
|
|
2
|
+
import { TimeOrRule } from "./time-or-rule";
|
|
3
|
+
|
|
4
|
+
export type BreakdownRule = string;
|
|
5
|
+
export type BreakdownPeriod = Step;
|
|
6
|
+
|
|
7
|
+
interface BreakdownRuleInit {
|
|
8
|
+
origin?: TimeOrRule;
|
|
9
|
+
count: number;
|
|
10
|
+
period: BreakdownPeriod;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function BreakdownRule(init: BreakdownRuleInit): BreakdownRule;
|
|
14
|
+
export function BreakdownRule(
|
|
15
|
+
count: number,
|
|
16
|
+
period: BreakdownPeriod,
|
|
17
|
+
): BreakdownRule;
|
|
18
|
+
export function BreakdownRule(
|
|
19
|
+
origin: TimeOrRule,
|
|
20
|
+
count: number,
|
|
21
|
+
period: BreakdownPeriod,
|
|
22
|
+
): BreakdownRule;
|
|
23
|
+
export function BreakdownRule(...params: any[]): BreakdownRule {
|
|
24
|
+
let init: BreakdownRuleInit;
|
|
25
|
+
switch (params.length) {
|
|
26
|
+
case 1:
|
|
27
|
+
init = params[0];
|
|
28
|
+
break;
|
|
29
|
+
case 2:
|
|
30
|
+
init = {
|
|
31
|
+
origin: "",
|
|
32
|
+
count: params[0],
|
|
33
|
+
period: params[1],
|
|
34
|
+
};
|
|
35
|
+
break;
|
|
36
|
+
case 3:
|
|
37
|
+
init = {
|
|
38
|
+
origin: params[0],
|
|
39
|
+
count: params[1],
|
|
40
|
+
period: params[2],
|
|
41
|
+
};
|
|
42
|
+
break;
|
|
43
|
+
default:
|
|
44
|
+
throw new TypeError("Invalid breakdown rule format");
|
|
45
|
+
}
|
|
46
|
+
const origin = init.origin ?? "";
|
|
47
|
+
const periodName =
|
|
48
|
+
Math.abs(init.count) == 1 ? init.period : (init.period + "s");
|
|
49
|
+
return TimeOrRule.isRule(origin) && origin === ""
|
|
50
|
+
? `${init.count}${periodName}`
|
|
51
|
+
: `${origin}/${init.count}${periodName}`;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export namespace BreakdownRule {
|
|
55
|
+
const regex = new RegExp(
|
|
56
|
+
"((.*)/)?([-+]?[0-9]+)(hour|day|week|month|year)s?",
|
|
57
|
+
);
|
|
58
|
+
export function split(rule: BreakdownRule) {
|
|
59
|
+
const match = regex.exec(rule);
|
|
60
|
+
if (!match) {
|
|
61
|
+
throw new Error("Invalid breakdown rule format");
|
|
62
|
+
}
|
|
63
|
+
const origin = match[2] ?? "";
|
|
64
|
+
const count = parseInt(match[3]);
|
|
65
|
+
let period = match[4];
|
|
66
|
+
if (period.endsWith("s")) {
|
|
67
|
+
period = period.substring(0, period.length - 1);
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
origin: origin as TimeOrRule,
|
|
71
|
+
count,
|
|
72
|
+
period: period as BreakdownPeriod,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import "./repository"; // We need this to get the Repository namespace exported
|
|
2
2
|
|
|
3
|
-
import * as registers from "./registers"
|
|
3
|
+
import * as registers from "./registers";
|
|
4
4
|
|
|
5
5
|
import { DayOfWeek, Step } from "./step";
|
|
6
6
|
import {
|
|
@@ -10,6 +10,8 @@ import {
|
|
|
10
10
|
} from "./number-formatter";
|
|
11
11
|
|
|
12
12
|
import type { AggregateId } from "./aggregate-id";
|
|
13
|
+
import type { BreakdownPeriod } from "./breakdown-rule";
|
|
14
|
+
import { BreakdownRule } from "./breakdown-rule";
|
|
13
15
|
import type { MeasureId } from "./measure-id";
|
|
14
16
|
import type { Medium } from "./medium";
|
|
15
17
|
import type { Optional } from "./optional";
|
|
@@ -24,21 +26,23 @@ import type { UnitId } from "./unit-id";
|
|
|
24
26
|
|
|
25
27
|
export {
|
|
26
28
|
AggregateId,
|
|
27
|
-
|
|
29
|
+
BreakdownPeriod,
|
|
30
|
+
BreakdownRule,
|
|
28
31
|
DayOfWeek,
|
|
32
|
+
invariantDecimalSeparator,
|
|
33
|
+
localeDecimalSeparator,
|
|
29
34
|
MeasureId,
|
|
35
|
+
Medium,
|
|
30
36
|
NumberFormatter,
|
|
37
|
+
Optional,
|
|
31
38
|
PrecisionNumber,
|
|
32
39
|
RegisterConvention,
|
|
33
40
|
RegisterDefinition,
|
|
34
41
|
RegisterId,
|
|
35
42
|
RegisterLibrary,
|
|
43
|
+
registers,
|
|
36
44
|
Repository,
|
|
37
45
|
Step,
|
|
38
46
|
TimeOrRule,
|
|
39
47
|
UnitId,
|
|
40
|
-
invariantDecimalSeparator,
|
|
41
|
-
localeDecimalSeparator,
|
|
42
|
-
Optional,
|
|
43
|
-
registers,
|
|
44
48
|
};
|