@rsdk/common 2.6.0-next.9 → 3.0.2

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/CHANGELOG.md CHANGED
@@ -3,6 +3,44 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [3.0.2](https://github.com/R-Vision/rsdk/compare/v3.0.1...v3.0.2) (2023-08-07)
7
+
8
+ **Note:** Version bump only for package @rsdk/common
9
+
10
+ ## [3.0.0](https://github.com/R-Vision/rsdk/compare/v3.0.0-next.0...v3.0.0) (2023-08-04)
11
+
12
+ **Note:** Version bump only for package @rsdk/common
13
+
14
+ ## [3.0.0-next.0](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.16...v3.0.0-next.0) (2023-08-04)
15
+
16
+ ### ⚠ BREAKING CHANGES
17
+
18
+ * just for bump
19
+
20
+ ### Bug Fixes
21
+
22
+ * just for bump ([6aa5435](https://github.com/R-Vision/rsdk/commit/6aa543537b2c5f8f407761016a44b17f51772646))
23
+
24
+ # [2.6.0-next.15](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.14...v2.6.0-next.15) (2023-08-04)
25
+
26
+ **Note:** Version bump only for package @rsdk/common
27
+
28
+ # [2.6.0-next.13](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.12...v2.6.0-next.13) (2023-08-01)
29
+
30
+ **Note:** Version bump only for package @rsdk/common
31
+
32
+ # [2.6.0-next.12](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.11...v2.6.0-next.12) (2023-07-28)
33
+
34
+ ### Features
35
+
36
+ * **@rsdk/common:** improve Timespan class ([f28a6b6](https://github.com/R-Vision/rsdk/commit/f28a6b68d3dfbf76f4a3f7a7225ef455526ccc0d))
37
+
38
+ # [2.6.0-next.11](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.10...v2.6.0-next.11) (2023-07-26)
39
+
40
+ ### Features
41
+
42
+ * **@rsdk/common:** add support of negative durations in Timespan class ([52ede3c](https://github.com/R-Vision/rsdk/commit/52ede3c69005a4091f591a78264fbd9bc6d0bcd2))
43
+
6
44
  # [2.6.0-next.9](https://github.com/R-Vision/rsdk/compare/v2.6.0-next.8...v2.6.0-next.9) (2023-07-25)
7
45
 
8
46
  **Note:** Version bump only for package @rsdk/common
@@ -1,13 +1,21 @@
1
- type Unit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days';
2
- type UnitAlias = 'd' | 'h' | 'm' | 's' | 'ms';
3
- type TimespanString = `${number}${Unit | UnitAlias}`;
1
+ export type Unit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days';
2
+ export type UnitAlias = 'd' | 'h' | 'm' | 's' | 'ms';
3
+ export type TimespanString = `${number}${Unit | UnitAlias}`;
4
4
  export declare const unitsToAliases: Record<Unit, UnitAlias>;
5
5
  export declare const TIMESPAN_STRING_PATTERN: RegExp;
6
6
  /**
7
7
  * Represents time period (duration)
8
8
  */
9
9
  export declare class Timespan {
10
+ /**
11
+ * Time in milliseconds
12
+ */
10
13
  private readonly _millis;
14
+ /**
15
+ * Original unit that was passed to
16
+ * constructor
17
+ */
18
+ private readonly _unit;
11
19
  constructor(value: number, u: Unit | UnitAlias);
12
20
  static parse(str: string): Timespan;
13
21
  /**
@@ -42,10 +50,9 @@ export declare class Timespan {
42
50
  subtract(value: number, unit: Unit): Timespan;
43
51
  subtract(value: string): Timespan;
44
52
  subtract(timespan: Timespan): Timespan;
45
- toString(u?: Unit | UnitAlias): string;
53
+ toString(useUnit?: Unit | UnitAlias): string;
46
54
  private parseAddition;
47
55
  private parseSubstraction;
48
56
  private isUnit;
49
57
  }
50
58
  export declare const timespan: (str: TimespanString) => Timespan;
51
- export {};
@@ -2,36 +2,40 @@
2
2
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.timespan = exports.Timespan = exports.TIMESPAN_STRING_PATTERN = exports.unitsToAliases = void 0;
5
+ const convert_1 = require("../convert");
5
6
  const strings_1 = require("../strings");
6
7
  const factors = {
7
8
  milliseconds: 1,
8
- seconds: 1000,
9
- minutes: 1000 * 60,
10
- hours: 1000 * 60 * 60,
11
- days: 1000 * 60 * 60 * 24,
9
+ seconds: 1 * 1000,
10
+ minutes: 1 * 1000 * 60,
11
+ hours: 1 * 1000 * 60 * 60,
12
+ days: 1 * 1000 * 60 * 60 * 24,
12
13
  };
13
14
  const aliasesToUnits = {
14
- d: 'days',
15
- h: 'hours',
16
- m: 'minutes',
17
- s: 'seconds',
18
15
  ms: 'milliseconds',
16
+ s: 'seconds',
17
+ m: 'minutes',
18
+ h: 'hours',
19
+ d: 'days',
19
20
  };
20
21
  exports.unitsToAliases = {
21
- days: 'd',
22
- hours: 'h',
23
- minutes: 'm',
24
- seconds: 's',
25
22
  milliseconds: 'ms',
23
+ seconds: 's',
24
+ minutes: 'm',
25
+ hours: 'h',
26
+ days: 'd',
26
27
  };
27
- exports.TIMESPAN_STRING_PATTERN = /^(\d{1,10})(h|m|s|ms|d)$/i;
28
+ // Exported pattern should be string to easier serialization
29
+ exports.TIMESPAN_STRING_PATTERN =
30
+ // eslint-disable-next-line unicorn/no-unsafe-regex
31
+ /^(-?\d{1,10}(\.\d{1,10})?)(h|H|m|M|s|S|ms|MS|d|D)$/;
28
32
  class InvalidTimespan extends Error {
29
33
  constructor(str) {
30
34
  super((0, strings_1.text) `
31
35
  Invalid timespan string: <${str}>.
32
36
  Timespan string should contain numeric and unit parts.
33
37
  Acceptable units: D, H, M, S, MS (case doesn't matter).
34
- Ex: "30D, 10H, 2M, 100MS"
38
+ Ex: "30D, 10H, 2M, 100MS, 0.1s, -5d"
35
39
  `);
36
40
  }
37
41
  }
@@ -39,18 +43,28 @@ class InvalidTimespan extends Error {
39
43
  * Represents time period (duration)
40
44
  */
41
45
  class Timespan {
46
+ /**
47
+ * Time in milliseconds
48
+ */
42
49
  _millis;
50
+ /**
51
+ * Original unit that was passed to
52
+ * constructor
53
+ */
54
+ _unit;
43
55
  constructor(value, u) {
44
56
  const unit = this.isUnit(u) ? u : aliasesToUnits[u];
45
57
  this._millis = value * factors[unit];
58
+ this._unit = unit;
46
59
  }
47
60
  static parse(str) {
48
61
  const match = exports.TIMESPAN_STRING_PATTERN.exec(str);
49
62
  if (!match) {
50
63
  throw new InvalidTimespan(str);
51
64
  }
52
- const [, rawValue, rawUnit] = match;
53
- const value = Number.parseInt(rawValue, 10);
65
+ const rawValue = match[1];
66
+ const rawUnit = match[3] || match[2]; // Will be 3 if value has decimal point
67
+ const value = convert_1.Convert.number(rawValue);
54
68
  const unit = aliasesToUnits[rawUnit.toLowerCase()];
55
69
  return new Timespan(value, unit);
56
70
  }
@@ -92,7 +106,8 @@ class Timespan {
92
106
  const subtraction = this.parseSubstraction(value, unit);
93
107
  return new Timespan(this._millis - subtraction, 'milliseconds');
94
108
  }
95
- toString(u = 's') {
109
+ toString(useUnit) {
110
+ const u = useUnit || this._unit;
96
111
  const unit = this.isUnit(u) ? u : aliasesToUnits[u];
97
112
  const value = this._millis / factors[unit];
98
113
  const alias = exports.unitsToAliases[unit];
@@ -1 +1 @@
1
- {"version":3,"file":"timespan.class.js","sourceRoot":"","sources":["../../src/time/timespan.class.ts"],"names":[],"mappings":";AAAA,6DAA6D;;;AAE7D,wCAAkC;AAMlC,MAAM,OAAO,GAAyB;IACpC,YAAY,EAAE,CAAC;IACf,OAAO,EAAE,IAAK;IAEd,OAAO,EAAE,IAAK,GAAG,EAAE;IAEnB,KAAK,EAAE,IAAK,GAAG,EAAE,GAAG,EAAE;IAEtB,IAAI,EAAE,IAAK,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE;CAC3B,CAAC;AAEF,MAAM,cAAc,GAA4B;IAC9C,CAAC,EAAE,MAAM;IACT,CAAC,EAAE,OAAO;IACV,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,SAAS;IAEZ,EAAE,EAAE,cAAc;CACnB,CAAC;AA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0-next.9",
3
+ "version": "3.0.2",
4
4
  "description": "Useful common classes, functions and types",
5
5
  "license": "Apache License 2.0",
6
6
  "publishConfig": {
@@ -13,5 +13,5 @@
13
13
  "scripts": {
14
14
  "prepublishOnly": "npm run build"
15
15
  },
16
- "gitHead": "6b0189156dc2873f86056e6c608efecbd2024b24"
16
+ "gitHead": "3ee33710bf18bfadf87a6da92b060e6a17481110"
17
17
  }
@@ -1,41 +1,40 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
 
3
+ import { Convert } from '../convert';
3
4
  import { text } from '../strings';
4
5
 
5
- type Unit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days';
6
- type UnitAlias = 'd' | 'h' | 'm' | 's' | 'ms';
7
- type TimespanString = `${number}${Unit | UnitAlias}`;
6
+ export type Unit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days';
7
+ export type UnitAlias = 'd' | 'h' | 'm' | 's' | 'ms';
8
+ export type TimespanString = `${number}${Unit | UnitAlias}`;
8
9
 
9
10
  const factors: Record<Unit, number> = {
10
11
  milliseconds: 1,
11
- seconds: 1_000,
12
-
13
- minutes: 1_000 * 60,
14
-
15
- hours: 1_000 * 60 * 60,
16
-
17
- days: 1_000 * 60 * 60 * 24,
12
+ seconds: 1 * 1_000,
13
+ minutes: 1 * 1_000 * 60,
14
+ hours: 1 * 1_000 * 60 * 60,
15
+ days: 1 * 1_000 * 60 * 60 * 24,
18
16
  };
19
17
 
20
18
  const aliasesToUnits: Record<UnitAlias, Unit> = {
21
- d: 'days',
22
- h: 'hours',
23
- m: 'minutes',
24
- s: 'seconds',
25
-
26
19
  ms: 'milliseconds',
20
+ s: 'seconds',
21
+ m: 'minutes',
22
+ h: 'hours',
23
+ d: 'days',
27
24
  };
28
25
 
29
26
  export const unitsToAliases: Record<Unit, UnitAlias> = {
30
- days: 'd',
31
- hours: 'h',
32
- minutes: 'm',
33
- seconds: 's',
34
-
35
27
  milliseconds: 'ms',
28
+ seconds: 's',
29
+ minutes: 'm',
30
+ hours: 'h',
31
+ days: 'd',
36
32
  };
37
33
 
38
- export const TIMESPAN_STRING_PATTERN = /^(\d{1,10})(h|m|s|ms|d)$/i;
34
+ // Exported pattern should be string to easier serialization
35
+ export const TIMESPAN_STRING_PATTERN =
36
+ // eslint-disable-next-line unicorn/no-unsafe-regex
37
+ /^(-?\d{1,10}(\.\d{1,10})?)(h|H|m|M|s|S|ms|MS|d|D)$/;
39
38
 
40
39
  class InvalidTimespan extends Error {
41
40
  constructor(str: string) {
@@ -43,7 +42,7 @@ class InvalidTimespan extends Error {
43
42
  Invalid timespan string: <${str}>.
44
43
  Timespan string should contain numeric and unit parts.
45
44
  Acceptable units: D, H, M, S, MS (case doesn't matter).
46
- Ex: "30D, 10H, 2M, 100MS"
45
+ Ex: "30D, 10H, 2M, 100MS, 0.1s, -5d"
47
46
  `);
48
47
  }
49
48
  }
@@ -52,12 +51,22 @@ class InvalidTimespan extends Error {
52
51
  * Represents time period (duration)
53
52
  */
54
53
  export class Timespan {
54
+ /**
55
+ * Time in milliseconds
56
+ */
55
57
  private readonly _millis: number;
56
58
 
59
+ /**
60
+ * Original unit that was passed to
61
+ * constructor
62
+ */
63
+ private readonly _unit: Unit;
64
+
57
65
  constructor(value: number, u: Unit | UnitAlias) {
58
66
  const unit = this.isUnit(u) ? u : aliasesToUnits[u];
59
67
 
60
68
  this._millis = value * factors[unit];
69
+ this._unit = unit;
61
70
  }
62
71
 
63
72
  static parse(str: string): Timespan {
@@ -67,9 +76,10 @@ export class Timespan {
67
76
  throw new InvalidTimespan(str);
68
77
  }
69
78
 
70
- const [, rawValue, rawUnit] = match;
79
+ const rawValue = match[1];
80
+ const rawUnit = match[3] || match[2]; // Will be 3 if value has decimal point
71
81
 
72
- const value = Number.parseInt(rawValue, 10);
82
+ const value = Convert.number(rawValue);
73
83
  const unit = aliasesToUnits[rawUnit.toLowerCase() as UnitAlias];
74
84
 
75
85
  return new Timespan(value, unit);
@@ -126,18 +136,16 @@ export class Timespan {
126
136
  * Subtract time from timespan
127
137
  */
128
138
  subtract(value: number, unit: Unit): Timespan;
129
-
130
139
  subtract(value: string): Timespan;
131
-
132
140
  subtract(timespan: Timespan): Timespan;
133
-
134
141
  subtract(value: number | string | Timespan, unit?: Unit): Timespan {
135
142
  const subtraction = this.parseSubstraction(value, unit);
136
143
 
137
144
  return new Timespan(this._millis - subtraction, 'milliseconds');
138
145
  }
139
146
 
140
- toString(u: Unit | UnitAlias = 's'): string {
147
+ toString(useUnit?: Unit | UnitAlias): string {
148
+ const u = useUnit || this._unit;
141
149
  const unit = this.isUnit(u) ? u : aliasesToUnits[u];
142
150
  const value = this._millis / factors[unit];
143
151
 
@@ -1,6 +1,7 @@
1
1
  /* eslint-disable sort-keys */
2
2
  /* eslint-disable prettier/prettier */
3
3
 
4
+ import type { Unit, UnitAlias } from './timespan.class';
4
5
  import { Timespan } from './timespan.class';
5
6
 
6
7
  describe('Timespan', () => {
@@ -63,6 +64,8 @@ describe('Timespan', () => {
63
64
  { str: '1h', correct: true, ms: 3_600_000 },
64
65
  { str: '1m', correct: true, ms: 60_000 },
65
66
  { str: '1s', correct: true, ms: 1_000 },
67
+ { str: '0.1s', correct: true, ms: 100 },
68
+ { str: '-1h', correct: true, ms: -3_600_000 },
66
69
  { str: '1ms', correct: true, ms: 1 },
67
70
  { str: '1me', correct: false, ms: null },
68
71
  { str: 'Time', correct: false, ms: null },
@@ -78,4 +81,29 @@ describe('Timespan', () => {
78
81
  });
79
82
  }
80
83
  });
84
+
85
+ describe('Serialization', () => {
86
+ interface TestCase {
87
+ timespan: Timespan;
88
+ useUnit: Unit | UnitAlias | undefined;
89
+ expected: string;
90
+ }
91
+
92
+ const tt: TestCase[] = [
93
+ { timespan: new Timespan(1, 'd'), useUnit: undefined, expected: '1d' },
94
+ { timespan: new Timespan(1, 'h'), useUnit: undefined, expected: '1h', },
95
+ { timespan: new Timespan(1, 'm'), useUnit: undefined, expected: '1m', },
96
+ { timespan: new Timespan(1, 's'), useUnit: undefined, expected: '1s', },
97
+ { timespan: new Timespan(0.1, 's'), useUnit: undefined, expected: '0.1s', },
98
+ { timespan: new Timespan(-1, 'h'), useUnit: undefined, expected: '-1h', },
99
+ { timespan: new Timespan(1, 'ms'), useUnit: undefined, expected: '1ms', },
100
+ { timespan: new Timespan(10, 'ms'), useUnit: 's', expected: '0.01s' },
101
+ ];
102
+
103
+ for (const { timespan, useUnit, expected } of tt) {
104
+ it(expected, () => {
105
+ expect(timespan.toString(useUnit)).toBe(expected);
106
+ });
107
+ }
108
+ })
81
109
  });