@rsdk/common 0.14.0
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/.eslintignore +4 -0
- package/.eslintrc.js +4 -0
- package/.prettierignore +1 -0
- package/.prettierrc.json +1 -0
- package/dist/convert/convert.class.d.ts +61 -0
- package/dist/convert/convert.class.js +144 -0
- package/dist/convert/convert.class.js.map +1 -0
- package/dist/convert/index.d.ts +1 -0
- package/dist/convert/index.js +18 -0
- package/dist/convert/index.js.map +1 -0
- package/dist/enums/enums.functions.d.ts +15 -0
- package/dist/enums/enums.functions.js +31 -0
- package/dist/enums/enums.functions.js.map +1 -0
- package/dist/enums/index.d.ts +2 -0
- package/dist/enums/index.js +19 -0
- package/dist/enums/index.js.map +1 -0
- package/dist/enums/types.d.ts +4 -0
- package/dist/enums/types.js +3 -0
- package/dist/enums/types.js.map +1 -0
- package/dist/functions/index.d.ts +2 -0
- package/dist/functions/index.js +19 -0
- package/dist/functions/index.js.map +1 -0
- package/dist/functions/misc.d.ts +33 -0
- package/dist/functions/misc.js +57 -0
- package/dist/functions/misc.js.map +1 -0
- package/dist/functions/obj-normalization.d.ts +27 -0
- package/dist/functions/obj-normalization.js +73 -0
- package/dist/functions/obj-normalization.js.map +1 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/serialization/errors/index.d.ts +3 -0
- package/dist/serialization/errors/index.js +20 -0
- package/dist/serialization/errors/index.js.map +1 -0
- package/dist/serialization/errors/not-absolute-path.error.d.ts +3 -0
- package/dist/serialization/errors/not-absolute-path.error.js +10 -0
- package/dist/serialization/errors/not-absolute-path.error.js.map +1 -0
- package/dist/serialization/errors/unknown-file-ext.error.d.ts +3 -0
- package/dist/serialization/errors/unknown-file-ext.error.js +10 -0
- package/dist/serialization/errors/unknown-file-ext.error.js.map +1 -0
- package/dist/serialization/errors/unknown-format.error.d.ts +5 -0
- package/dist/serialization/errors/unknown-format.error.js +11 -0
- package/dist/serialization/errors/unknown-format.error.js.map +1 -0
- package/dist/serialization/index.d.ts +5 -0
- package/dist/serialization/index.js +22 -0
- package/dist/serialization/index.js.map +1 -0
- package/dist/serialization/read-obj-sync.d.ts +9 -0
- package/dist/serialization/read-obj-sync.js +25 -0
- package/dist/serialization/read-obj-sync.js.map +1 -0
- package/dist/serialization/read-obj.d.ts +8 -0
- package/dist/serialization/read-obj.js +24 -0
- package/dist/serialization/read-obj.js.map +1 -0
- package/dist/serialization/reviving-serializer.d.ts +8 -0
- package/dist/serialization/reviving-serializer.js +30 -0
- package/dist/serialization/reviving-serializer.js.map +1 -0
- package/dist/serialization/save-obj.d.ts +10 -0
- package/dist/serialization/save-obj.js +25 -0
- package/dist/serialization/save-obj.js.map +1 -0
- package/dist/serialization/serializer.class.d.ts +14 -0
- package/dist/serialization/serializer.class.js +64 -0
- package/dist/serialization/serializer.class.js.map +1 -0
- package/dist/size/index.d.ts +1 -0
- package/dist/size/index.js +18 -0
- package/dist/size/index.js.map +1 -0
- package/dist/size/size.class.d.ts +16 -0
- package/dist/size/size.class.js +60 -0
- package/dist/size/size.class.js.map +1 -0
- package/dist/time/index.d.ts +4 -0
- package/dist/time/index.js +21 -0
- package/dist/time/index.js.map +1 -0
- package/dist/time/repeat.fn.d.ts +8 -0
- package/dist/time/repeat.fn.js +23 -0
- package/dist/time/repeat.fn.js.map +1 -0
- package/dist/time/stopwatch.class.d.ts +61 -0
- package/dist/time/stopwatch.class.js +97 -0
- package/dist/time/stopwatch.class.js.map +1 -0
- package/dist/time/timer.class.d.ts +9 -0
- package/dist/time/timer.class.js +18 -0
- package/dist/time/timer.class.js.map +1 -0
- package/dist/time/timespan.class.d.ts +49 -0
- package/dist/time/timespan.class.js +116 -0
- package/dist/time/timespan.class.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types/assert.class.d.ts +5 -0
- package/dist/types/assert.class.js +25 -0
- package/dist/types/assert.class.js.map +1 -0
- package/dist/types/constructor.type.d.ts +2 -0
- package/dist/types/constructor.type.js +3 -0
- package/dist/types/constructor.type.js.map +1 -0
- package/dist/types/deep-partial.type.d.ts +11 -0
- package/dist/types/deep-partial.type.js +3 -0
- package/dist/types/deep-partial.type.js.map +1 -0
- package/dist/types/error-like.type.d.ts +5 -0
- package/dist/types/error-like.type.js +6 -0
- package/dist/types/error-like.type.js.map +1 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/index.js +22 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/primitive.type.d.ts +2 -0
- package/dist/types/primitive.type.js +6 -0
- package/dist/types/primitive.type.js.map +1 -0
- package/dist/types/required-fields.d.ts +5 -0
- package/dist/types/required-fields.js +3 -0
- package/dist/types/required-fields.js.map +1 -0
- package/package.json +32 -0
- package/src/convert/convert.class.ts +172 -0
- package/src/convert/convert.spec.ts +271 -0
- package/src/convert/index.ts +1 -0
- package/src/enums/enums.functions.ts +34 -0
- package/src/enums/enums.spec.ts +59 -0
- package/src/enums/index.ts +2 -0
- package/src/enums/types.ts +5 -0
- package/src/functions/index.ts +2 -0
- package/src/functions/misc.ts +70 -0
- package/src/functions/obj-normalization.ts +104 -0
- package/src/index.ts +8 -0
- package/src/serialization/errors/index.ts +3 -0
- package/src/serialization/errors/not-absolute-path.error.ts +5 -0
- package/src/serialization/errors/unknown-file-ext.error.ts +5 -0
- package/src/serialization/errors/unknown-format.error.ts +9 -0
- package/src/serialization/index.ts +5 -0
- package/src/serialization/read-obj-sync.ts +24 -0
- package/src/serialization/read-obj.ts +23 -0
- package/src/serialization/reviving-serializer.ts +32 -0
- package/src/serialization/save-obj.ts +31 -0
- package/src/serialization/serializer.class.ts +85 -0
- package/src/size/index.ts +1 -0
- package/src/size/size.class.ts +77 -0
- package/src/size/size.spec.ts +74 -0
- package/src/time/index.ts +4 -0
- package/src/time/repeat.fn.ts +27 -0
- package/src/time/stopwatch.class.ts +108 -0
- package/src/time/timer.class.ts +15 -0
- package/src/time/timespan.class.ts +161 -0
- package/src/time/timespan.spec.ts +81 -0
- package/src/types/assert.class.ts +25 -0
- package/src/types/constructor.type.ts +2 -0
- package/src/types/deep-partial.type.ts +23 -0
- package/src/types/error-like.type.ts +8 -0
- package/src/types/index.ts +6 -0
- package/src/types/primitive.type.ts +4 -0
- package/src/types/required-fields.ts +8 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
2
|
+
|
|
3
|
+
import { text } from '../functions';
|
|
4
|
+
|
|
5
|
+
type Unit = 'milliseconds' | 'seconds' | 'minutes' | 'hours' | 'days';
|
|
6
|
+
type UnitAlias = 'd' | 'h' | 'm' | 's' | 'ms';
|
|
7
|
+
type TimespanString = `${number}${Unit | UnitAlias}`;
|
|
8
|
+
|
|
9
|
+
const factors: Record<Unit, number> = {
|
|
10
|
+
milliseconds: 1,
|
|
11
|
+
seconds: 1000,
|
|
12
|
+
|
|
13
|
+
minutes: 1000 * 60,
|
|
14
|
+
|
|
15
|
+
hours: 1000 * 60 * 60,
|
|
16
|
+
|
|
17
|
+
days: 1000 * 60 * 60 * 24,
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const aliasesToUnits: Record<UnitAlias, Unit> = {
|
|
21
|
+
d: 'days',
|
|
22
|
+
h: 'hours',
|
|
23
|
+
m: 'minutes',
|
|
24
|
+
s: 'seconds',
|
|
25
|
+
|
|
26
|
+
ms: 'milliseconds',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const unitsToAliases: Record<Unit, UnitAlias> = {
|
|
30
|
+
days: 'd',
|
|
31
|
+
hours: 'h',
|
|
32
|
+
minutes: 'm',
|
|
33
|
+
seconds: 's',
|
|
34
|
+
|
|
35
|
+
milliseconds: 'ms',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const TIMESPAN_STRING_PATTERN = /^(\d{1,10})(h|m|s|ms|d)$/i;
|
|
39
|
+
|
|
40
|
+
class InvalidTimespan extends Error {
|
|
41
|
+
constructor(str: string) {
|
|
42
|
+
super(text`
|
|
43
|
+
Invalid timespan string: <${str}>.
|
|
44
|
+
Timespan string should contain numeric and unit parts.
|
|
45
|
+
Acceptable units: D, H, M, S, MS (case doesn't matter).
|
|
46
|
+
Ex: "30D, 10H, 2M, 100MS"
|
|
47
|
+
`);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Represents time period (duration)
|
|
53
|
+
*/
|
|
54
|
+
export class Timespan {
|
|
55
|
+
private readonly _millis: number;
|
|
56
|
+
|
|
57
|
+
constructor(value: number, u: Unit | UnitAlias) {
|
|
58
|
+
const unit = this.isUnit(u) ? u : aliasesToUnits[u];
|
|
59
|
+
|
|
60
|
+
this._millis = value * factors[unit];
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
static parse(str: string): Timespan {
|
|
64
|
+
const match = TIMESPAN_STRING_PATTERN.exec(str);
|
|
65
|
+
|
|
66
|
+
if (!match) {
|
|
67
|
+
throw new InvalidTimespan(str);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const [, rawValue, rawUnit] = match;
|
|
71
|
+
|
|
72
|
+
const value = parseInt(rawValue!, 10);
|
|
73
|
+
const unit = aliasesToUnits[rawUnit!.toLowerCase() as UnitAlias];
|
|
74
|
+
|
|
75
|
+
return new Timespan(value, unit);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Get value in milliseconds
|
|
80
|
+
*/
|
|
81
|
+
millis(): number {
|
|
82
|
+
return this._millis;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Get value in seconds
|
|
87
|
+
*/
|
|
88
|
+
seconds(): number {
|
|
89
|
+
return this._millis / factors.seconds;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* Get value in minutes
|
|
94
|
+
*/
|
|
95
|
+
minutes(): number {
|
|
96
|
+
return this._millis / factors.minutes;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get value in hours
|
|
101
|
+
*/
|
|
102
|
+
hours(): number {
|
|
103
|
+
return this._millis / factors.hours;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Get value in days
|
|
108
|
+
*/
|
|
109
|
+
days(): number {
|
|
110
|
+
return this._millis / factors.days;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Add time to timespan
|
|
115
|
+
*/
|
|
116
|
+
add(value: number, unit: Unit): Timespan;
|
|
117
|
+
add(value: string): Timespan;
|
|
118
|
+
add(timespan: Timespan): Timespan;
|
|
119
|
+
add(value: number | string | Timespan, unit?: Unit): Timespan {
|
|
120
|
+
const addition =
|
|
121
|
+
typeof value === 'number'
|
|
122
|
+
? value * factors[unit!]
|
|
123
|
+
: typeof value === 'string'
|
|
124
|
+
? Timespan.parse(value).millis()
|
|
125
|
+
: value._millis;
|
|
126
|
+
|
|
127
|
+
return new Timespan(this._millis + addition, 'milliseconds');
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Subtract time from timespan
|
|
132
|
+
*/
|
|
133
|
+
subtract(value: number, unit: Unit): Timespan;
|
|
134
|
+
subtract(value: string): Timespan;
|
|
135
|
+
subtract(timespan: Timespan): Timespan;
|
|
136
|
+
subtract(value: number | string | Timespan, unit?: Unit): Timespan {
|
|
137
|
+
const subtraction =
|
|
138
|
+
typeof value === 'number'
|
|
139
|
+
? value / factors[unit!]
|
|
140
|
+
: typeof value === 'string'
|
|
141
|
+
? Timespan.parse(value).millis()
|
|
142
|
+
: value._millis;
|
|
143
|
+
|
|
144
|
+
return new Timespan(this._millis - subtraction, 'milliseconds');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
toString(u: Unit | UnitAlias = 's'): string {
|
|
148
|
+
const unit = this.isUnit(u) ? u : aliasesToUnits[u];
|
|
149
|
+
const value = this._millis / factors[unit];
|
|
150
|
+
|
|
151
|
+
const alias = unitsToAliases[unit];
|
|
152
|
+
|
|
153
|
+
return `${value}${alias}`;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private isUnit(u: Unit | UnitAlias): u is Unit {
|
|
157
|
+
return Object.values(aliasesToUnits).includes(u as Unit);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export const timespan = (str: TimespanString): Timespan => Timespan.parse(str);
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/* eslint-disable sort-keys */
|
|
2
|
+
/* eslint-disable prettier/prettier */
|
|
3
|
+
|
|
4
|
+
import { Timespan } from './timespan.class';
|
|
5
|
+
|
|
6
|
+
describe('Timespan', () => {
|
|
7
|
+
describe('Casting', () => {
|
|
8
|
+
interface TestCase {
|
|
9
|
+
name: string;
|
|
10
|
+
t: Timespan;
|
|
11
|
+
d: number;
|
|
12
|
+
h: number;
|
|
13
|
+
m: number;
|
|
14
|
+
s: number;
|
|
15
|
+
ms: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const tt: TestCase[] = [
|
|
19
|
+
// Days
|
|
20
|
+
{ name: '1d', t: new Timespan(1, 'd'), d: 1, h: 24, m: 1_440, s: 86_400, ms: 86_400_000 },
|
|
21
|
+
{ name: '2d', t: new Timespan(2, 'd'), d: 2, h: 48, m: 2_880, s: 172_800, ms: 172_800_000 },
|
|
22
|
+
|
|
23
|
+
// Hours
|
|
24
|
+
{ name: '1h', t: new Timespan(1, 'h'), d: 0.042, h: 1, m: 60, s: 3_600, ms: 3_600_000 },
|
|
25
|
+
{ name: '2h', t: new Timespan(2, 'h'), d: 0.083, h: 2, m: 120, s: 7_200, ms: 7_200_000 },
|
|
26
|
+
|
|
27
|
+
// Minutes
|
|
28
|
+
{ name: '1m', t: new Timespan(1, 'm'), d: 0.001, h: 0.017, m: 1, s: 60, ms: 60_000 },
|
|
29
|
+
{ name: '2m', t: new Timespan(2, 'm'), d: 0.001, h: 0.033, m: 2, s: 120, ms: 120_000 },
|
|
30
|
+
|
|
31
|
+
// Seconds
|
|
32
|
+
{ name: '1s', t: new Timespan(1, 's'), d: 0.0, h: 0.0, m: 0.017, s: 1, ms: 1_000 },
|
|
33
|
+
{ name: '2s', t: new Timespan(2, 's'), d: 0.0, h: 0.001, m: 0.033, s: 2, ms: 2_000 },
|
|
34
|
+
|
|
35
|
+
// Milliseconds
|
|
36
|
+
{ name: '1ms', t: new Timespan(1, 'ms'), d: 0.0, h: 0.0, m: 0.0, s: 0.001, ms: 1 },
|
|
37
|
+
{ name: '2ms', t: new Timespan(2, 'ms'), d: 0.0, h: 0.0, m: 0.0, s: 0.002, ms: 2 },
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
for (const { name, t, d, h, m, s, ms } of tt) {
|
|
41
|
+
it(name, () => {
|
|
42
|
+
/**
|
|
43
|
+
* Всё округляется до 3-го знака после запятой
|
|
44
|
+
*/
|
|
45
|
+
expect(t.days().toFixed(3)).toEqual(d.toFixed(3));
|
|
46
|
+
expect(t.hours().toFixed(3)).toEqual(h.toFixed(3));
|
|
47
|
+
expect(t.minutes().toFixed(3)).toEqual(m.toFixed(3));
|
|
48
|
+
expect(t.seconds().toFixed(3)).toEqual(s.toFixed(3));
|
|
49
|
+
expect(t.millis().toFixed(3)).toEqual(ms.toFixed(3));
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('Parsing', () => {
|
|
55
|
+
interface TestCase {
|
|
56
|
+
str: string;
|
|
57
|
+
correct: boolean;
|
|
58
|
+
ms: number | null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const tt: TestCase[] = [
|
|
62
|
+
{ str: '1d', correct: true, ms: 86_400_000 },
|
|
63
|
+
{ str: '1h', correct: true, ms: 3_600_000 },
|
|
64
|
+
{ str: '1m', correct: true, ms: 60_000 },
|
|
65
|
+
{ str: '1s', correct: true, ms: 1_000 },
|
|
66
|
+
{ str: '1ms', correct: true, ms: 1 },
|
|
67
|
+
{ str: '1me', correct: false, ms: null },
|
|
68
|
+
{ str: 'Time', correct: false, ms: null },
|
|
69
|
+
];
|
|
70
|
+
|
|
71
|
+
for (const { str, correct, ms } of tt) {
|
|
72
|
+
it(str, () => {
|
|
73
|
+
if (correct) {
|
|
74
|
+
expect(Timespan.parse(str).millis()).toEqual(ms);
|
|
75
|
+
} else {
|
|
76
|
+
expect(() => Timespan.parse(str)).toThrow();
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export class Assert {
|
|
2
|
+
static isString(x: unknown): asserts x is string {
|
|
3
|
+
if (typeof x === 'string') {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
throw new Error(`<${x}> is not a string.`);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
static isNumber(x: unknown): asserts x is number {
|
|
11
|
+
if (typeof x === 'number') {
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
throw new Error(`<${x}> is not a number.`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static isError(x: Error): asserts x is Error {
|
|
19
|
+
if (x instanceof Error) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
throw new Error(`<${x}> is not instance of Error.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
type Builtin =
|
|
3
|
+
| Date
|
|
4
|
+
| Function
|
|
5
|
+
| Uint8Array
|
|
6
|
+
| string
|
|
7
|
+
| number
|
|
8
|
+
| boolean
|
|
9
|
+
| undefined;
|
|
10
|
+
|
|
11
|
+
export type DeepPartial<T> = T extends Builtin
|
|
12
|
+
? T
|
|
13
|
+
: T extends Array<infer U>
|
|
14
|
+
? Array<DeepPartial<U>>
|
|
15
|
+
: T extends ReadonlyArray<infer U>
|
|
16
|
+
? ReadonlyArray<DeepPartial<U>>
|
|
17
|
+
: T extends { $case: string }
|
|
18
|
+
? { [K in keyof Omit<T, '$case'>]?: DeepPartial<T[K]> } & {
|
|
19
|
+
$case: T['$case'];
|
|
20
|
+
}
|
|
21
|
+
: T extends {}
|
|
22
|
+
? { [K in keyof T]?: DeepPartial<T[K]> }
|
|
23
|
+
: Partial<T>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* В базовом typescript есть helper `Required` который убирает флаг `?` со всех полей
|
|
3
|
+
* Но иногда нам необходимо убрать этот флаг только у некоторых полей
|
|
4
|
+
*/
|
|
5
|
+
export type RequiredFields<
|
|
6
|
+
TRecord extends Record<PropertyKey, any>,
|
|
7
|
+
TRequiredKeys extends keyof TRecord,
|
|
8
|
+
> = Omit<TRecord, TRequiredKeys> & Required<Pick<TRecord, TRequiredKeys>>;
|
package/tsconfig.json
ADDED