@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 @@
|
|
|
1
|
+
{"version":3,"file":"required-fields.js","sourceRoot":"","sources":["../../src/types/required-fields.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rsdk/common",
|
|
3
|
+
"version": "0.14.0",
|
|
4
|
+
"description": "Useful common classes, functions and types",
|
|
5
|
+
"license": "Apache License 2.0",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"url": "https://github.com/R-Vision/rsdk"
|
|
11
|
+
},
|
|
12
|
+
"main": "dist/index.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "rm -rf dist/ && tsc",
|
|
15
|
+
"clean": "rm -rf dist/ node_modules/",
|
|
16
|
+
"compile:watch": "tsc -w",
|
|
17
|
+
"prepublishOnly": "npm run build",
|
|
18
|
+
"lint": "eslint -f codeframe",
|
|
19
|
+
"lint:fix": "eslint -f codeframe --fix"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"dotenv": "^16.0.3",
|
|
23
|
+
"toml": "^3.0.0",
|
|
24
|
+
"yaml": "^1.10.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@rsdk/prettier-config": "*",
|
|
28
|
+
"@rsdk/tsconfig": "*",
|
|
29
|
+
"eslint-config-rsdk": "*"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "e05337f5e9aa6242fe74f7ca5a4b3122765a77c0"
|
|
32
|
+
}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
/* eslint-disable max-classes-per-file */
|
|
2
|
+
/**
|
|
3
|
+
* Need to use any due to low-level dynamic operations.
|
|
4
|
+
*/
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
|
7
|
+
|
|
8
|
+
import type { EnumLike } from '../enums';
|
|
9
|
+
import { Enum } from '../enums';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Static class which provides some conversion methods.
|
|
13
|
+
*
|
|
14
|
+
* @export
|
|
15
|
+
* @class Convert
|
|
16
|
+
*/
|
|
17
|
+
export class Convert {
|
|
18
|
+
/**
|
|
19
|
+
* Converts any value to int or throws error. Accepts values in decimal or
|
|
20
|
+
* hexadecimal format (prefixed with 0x). Signed and unsigned
|
|
21
|
+
*
|
|
22
|
+
* @static
|
|
23
|
+
* @param {*} value Any input value.
|
|
24
|
+
* @returns {number}
|
|
25
|
+
* @throws {Error} Throws if conversion is impossible.
|
|
26
|
+
* @memberof Convert
|
|
27
|
+
*/
|
|
28
|
+
static int(value: unknown): number {
|
|
29
|
+
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
30
|
+
throw this.intError(value);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (value === '') {
|
|
34
|
+
throw this.intError(value);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Why not parseInt, because parseInt(123,2) === 123;
|
|
38
|
+
const parsed = +value;
|
|
39
|
+
|
|
40
|
+
if (!Number.isInteger(parsed)) {
|
|
41
|
+
throw this.intError(value);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return parsed;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Converts any value to number or throws error. Decimal separator
|
|
49
|
+
* depends on machine locale
|
|
50
|
+
*
|
|
51
|
+
* @static
|
|
52
|
+
* @param {*} value Any input value.
|
|
53
|
+
* @returns {number}
|
|
54
|
+
* @throws {Error} Throws if conversion is impossible.
|
|
55
|
+
* @memberof Convert
|
|
56
|
+
*/
|
|
57
|
+
static number(value: unknown): number {
|
|
58
|
+
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
59
|
+
throw this.numberError(value);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (value === '') {
|
|
63
|
+
throw this.numberError(value);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// See comments to int() method
|
|
67
|
+
const parsed = +value;
|
|
68
|
+
|
|
69
|
+
if (Number.isNaN(parsed)) {
|
|
70
|
+
throw this.numberError(value);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return parsed;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Converts any value to boolean or throws error.
|
|
78
|
+
* Only boolean values or strings 'true' and 'false' (case insensitive) can be converted.
|
|
79
|
+
*
|
|
80
|
+
* @static
|
|
81
|
+
* @param {*} value Any input value.
|
|
82
|
+
* @param {number} [defaultValue] Value to use if input value is undefined.
|
|
83
|
+
* @returns {number}
|
|
84
|
+
* @throws {Error} Throws if conversion is impossible.
|
|
85
|
+
* @memberof Convert
|
|
86
|
+
*/
|
|
87
|
+
static boolean(value: unknown): boolean {
|
|
88
|
+
if (typeof value !== 'string' && typeof value !== 'boolean') {
|
|
89
|
+
throw this.booleanError(value);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (typeof value === 'boolean') {
|
|
93
|
+
return value;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const normalized = value.toLowerCase().trim();
|
|
97
|
+
|
|
98
|
+
if (!['true', 'false'].includes(normalized)) {
|
|
99
|
+
throw this.booleanError(value);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return normalized === 'true';
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Converts unknown value to member of specified enum or throws error.
|
|
107
|
+
*
|
|
108
|
+
* @static
|
|
109
|
+
* @param {*} value Unknown input value.
|
|
110
|
+
* @returns {E}
|
|
111
|
+
* @throws {Error} Throws if conversion is impossible.
|
|
112
|
+
* @memberof Convert
|
|
113
|
+
*/
|
|
114
|
+
static enum<E>(value: unknown, e: EnumLike): E {
|
|
115
|
+
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
116
|
+
throw this.enumError(value, e);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
for (const v of Enum.values(e)) {
|
|
120
|
+
const trimmed = typeof value === 'string' ? value.trim() : value;
|
|
121
|
+
|
|
122
|
+
if (v === trimmed) {
|
|
123
|
+
return value as unknown as E;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
if (
|
|
127
|
+
typeof v === 'number' &&
|
|
128
|
+
typeof value !== 'number' &&
|
|
129
|
+
v === parseInt(value, 10)
|
|
130
|
+
) {
|
|
131
|
+
return v as unknown as E;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const result = e[value];
|
|
136
|
+
|
|
137
|
+
if (result === undefined) {
|
|
138
|
+
throw this.enumError(value, e);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return result as unknown as E;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
private static numberError(value: unknown): Error {
|
|
145
|
+
return new Error(`Value <${value}> can't be converted to number.`);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private static intError(value: unknown): Error {
|
|
149
|
+
return new Error(`Value <${value}> can't be converted to integer.`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
private static booleanError(value: unknown): Error {
|
|
153
|
+
return new Error(`Value <${value}> can't be converted to boolean.`);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
private static wrapIfString(value: unknown): string {
|
|
157
|
+
return typeof value === 'string'
|
|
158
|
+
? `'${value}'`
|
|
159
|
+
: // We use String(...) here because null or undefined doesn't
|
|
160
|
+
// have toString() method
|
|
161
|
+
String(value);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private static enumError(value: any, e: EnumLike): Error {
|
|
165
|
+
return new Error(`<${this.wrapIfString(
|
|
166
|
+
value,
|
|
167
|
+
)}> is not member of enum: <${Enum.values(e)
|
|
168
|
+
.map(this.wrapIfString)
|
|
169
|
+
.join(', ')}>
|
|
170
|
+
`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/* eslint-disable prettier/prettier */
|
|
2
|
+
|
|
3
|
+
import type { EnumLike } from '../enums';
|
|
4
|
+
|
|
5
|
+
import { Convert } from './convert.class';
|
|
6
|
+
|
|
7
|
+
/* eslint-disable sort-keys */
|
|
8
|
+
describe('Convert', () => {
|
|
9
|
+
describe('int', () => {
|
|
10
|
+
interface Case {
|
|
11
|
+
name: string;
|
|
12
|
+
input: any;
|
|
13
|
+
success: boolean;
|
|
14
|
+
output: number | null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const tt: Case[] = [
|
|
18
|
+
// Positive
|
|
19
|
+
{ name: 'Correct integer (string)', input: '123', success: true, output: 123 },
|
|
20
|
+
{ name: 'Correct integer (number)', input: 123, success: true, output: 123 },
|
|
21
|
+
{ name: 'Correct negative integer (number)', input: -123, success: true, output: -123 },
|
|
22
|
+
{ name: 'Correct negative integer (string)', input: '-123', success: true, output: -123 },
|
|
23
|
+
{ name: 'Zero', input: 0, success: true, output: 0 },
|
|
24
|
+
{ name: 'Zero string', input: '0', success: true, output: 0 },
|
|
25
|
+
|
|
26
|
+
// Negative
|
|
27
|
+
{ name: 'Correct float (string)', input: '123.1', success: false, output: null },
|
|
28
|
+
{ name: 'Correct float (number)', input: 123.1, success: false, output: null },
|
|
29
|
+
{ name: 'Correct negative float (number)', input: -123.2, success: false, output: null },
|
|
30
|
+
{ name: 'Correct negative float (string)', input: '-123.4', success: false, output: null },
|
|
31
|
+
{ name: 'Incorrect float (string)', input: '123,1', success: false, output: null },
|
|
32
|
+
{ name: 'Empty string', input: '', success: false, output: null },
|
|
33
|
+
{ name: 'True (boolean)', input: true, success: false, output: null },
|
|
34
|
+
{ name: 'True (string lowercase)', input: 'true', success: false, output: null },
|
|
35
|
+
{ name: 'True (string mixedcase)', input: 'TrUe', success: false, output: null },
|
|
36
|
+
{ name: 'False (boolean)', input: false, success: false, output: null },
|
|
37
|
+
{ name: 'False (string lowercase)', input: 'false', success: false, output: null },
|
|
38
|
+
{ name: 'False (string mixedcase)', input: 'FalSe', success: false, output: null },
|
|
39
|
+
{ name: 'Trash (string)', input: 'sdfg', success: false, output: null },
|
|
40
|
+
{ name: 'Trash (object)', input: { a: 1 }, success: false, output: null },
|
|
41
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
42
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
for (const { name, input, success, output } of tt) {
|
|
46
|
+
if (success) {
|
|
47
|
+
it(name, () => expect(Convert.int(input)).toBe(output));
|
|
48
|
+
} else {
|
|
49
|
+
it(name, () => expect(() => Convert.int(input)).toThrow());
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('number', () => {
|
|
55
|
+
interface Case {
|
|
56
|
+
name: string;
|
|
57
|
+
input: any;
|
|
58
|
+
success: boolean;
|
|
59
|
+
output: number | null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const tt: Case[] = [
|
|
63
|
+
// Positive
|
|
64
|
+
{ name: 'Correct integer (string)', input: '123', success: true, output: 123 },
|
|
65
|
+
{ name: 'Correct integer (number)', input: 123, success: true, output: 123 },
|
|
66
|
+
{ name: 'Correct negative integer (number)', input: -123, success: true, output: -123 },
|
|
67
|
+
{ name: 'Correct negative integer (string)', input: '-123', success: true, output: -123 },
|
|
68
|
+
{ name: 'Correct float (string)', input: '123.1', success: true, output: 123.1 },
|
|
69
|
+
{ name: 'Correct float (number)', input: 123.1, success: true, output: 123.1 },
|
|
70
|
+
{ name: 'Correct negative float (number)', input: -123.4, success: true, output: -123.4 },
|
|
71
|
+
{ name: 'Correct negative float (string)', input: '-123.4', success: true, output: -123.4 },
|
|
72
|
+
{ name: 'Zero', input: 0, success: true, output: 0 },
|
|
73
|
+
{ name: 'Zero string', input: '0', success: true, output: 0 },
|
|
74
|
+
|
|
75
|
+
// Negative
|
|
76
|
+
{ name: 'Incorrect float (string)', input: '123,1', success: false, output: null },
|
|
77
|
+
{ name: 'Empty string', input: '', success: false, output: null },
|
|
78
|
+
{ name: 'True (boolean)', input: true, success: false, output: null },
|
|
79
|
+
{ name: 'True (string lowercase)', input: 'true', success: false, output: null },
|
|
80
|
+
{ name: 'True (string mixedcase)', input: 'TrUe', success: false, output: null },
|
|
81
|
+
{ name: 'False (boolean)', input: false, success: false, output: null },
|
|
82
|
+
{ name: 'False (string lowercase)', input: 'false', success: false, output: null },
|
|
83
|
+
{ name: 'False (string mixedcase)', input: 'FalSe', success: false, output: null },
|
|
84
|
+
{ name: 'Trash (string)', input: 'sdfg', success: false, output: null },
|
|
85
|
+
{ name: 'Trash (object)', input: { a: 1 }, success: false, output: null },
|
|
86
|
+
{ name: 'Trash (object)', input: { a: 1 }, success: false, output: null },
|
|
87
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
88
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
89
|
+
];
|
|
90
|
+
|
|
91
|
+
for (const { name, input, success, output } of tt) {
|
|
92
|
+
if (success) {
|
|
93
|
+
it(name, () => expect(Convert.number(input)).toBe(output));
|
|
94
|
+
} else {
|
|
95
|
+
it(name, () => expect(() => Convert.number(input)).toThrow());
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('boolean', () => {
|
|
101
|
+
interface Case {
|
|
102
|
+
name: string;
|
|
103
|
+
input: any;
|
|
104
|
+
success: boolean;
|
|
105
|
+
output: boolean | null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const tt: Case[] = [
|
|
109
|
+
// Positive
|
|
110
|
+
{ name: 'True (boolean)', input: true, success: true, output: true },
|
|
111
|
+
{ name: 'True (string lowercase)', input: 'true', success: true, output: true },
|
|
112
|
+
{ name: 'True (string mixedcase)', input: 'TrUe', success: true, output: true },
|
|
113
|
+
{ name: 'False (boolean)', input: false, success: true, output: false },
|
|
114
|
+
{ name: 'False (string lowercase)', input: 'false', success: true, output: false },
|
|
115
|
+
{ name: 'False (string mixedcase)', input: 'FalSe', success: true, output: false },
|
|
116
|
+
|
|
117
|
+
// Negative
|
|
118
|
+
{ name: 'Correct integer (string)', input: '123', success: false, output: null },
|
|
119
|
+
{ name: 'Correct integer (number)', input: 123, success: false, output: null },
|
|
120
|
+
{ name: 'Correct float (string)', input: '123.1', success: false, output: null },
|
|
121
|
+
{ name: 'Correct float (number)', input: 123.1, success: false, output: null },
|
|
122
|
+
{ name: 'Incorrect float (string)', input: '123,1', success: false, output: null },
|
|
123
|
+
{ name: 'Empty string', input: '', success: false, output: null },
|
|
124
|
+
{ name: 'Zero', input: 0, success: false, output: null },
|
|
125
|
+
{ name: 'Zero string', input: '0', success: false, output: null },
|
|
126
|
+
{ name: 'Trash (string)', input: 'sdfg', success: false, output: null },
|
|
127
|
+
{ name: 'Trash (object)', input: { a: 1 }, success: false, output: null },
|
|
128
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
129
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
130
|
+
];
|
|
131
|
+
|
|
132
|
+
for (const { name, input, success, output } of tt) {
|
|
133
|
+
if (success) {
|
|
134
|
+
it(name, () => expect(Convert.boolean(input)).toBe(output));
|
|
135
|
+
} else {
|
|
136
|
+
it(name, () => expect(() => Convert.boolean(input)).toThrow());
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
describe('enum', () => {
|
|
142
|
+
enum Implicit {
|
|
143
|
+
KEY_1,
|
|
144
|
+
KEY_2,
|
|
145
|
+
}
|
|
146
|
+
enum Numeric {
|
|
147
|
+
KEY_1 = 0,
|
|
148
|
+
KEY_2 = 1,
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
enum Strings {
|
|
152
|
+
KEY_1 = '0',
|
|
153
|
+
KEY_2 = '1',
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
enum Mixed {
|
|
157
|
+
KEY_1 = '0',
|
|
158
|
+
KEY_2 = 1,
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
interface Subcase {
|
|
162
|
+
name: string;
|
|
163
|
+
input: any;
|
|
164
|
+
success: boolean;
|
|
165
|
+
output: string | number | null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
interface Case {
|
|
169
|
+
name: string;
|
|
170
|
+
e: EnumLike;
|
|
171
|
+
subcases: Subcase[];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const common: Subcase[] = [
|
|
175
|
+
// All negative!
|
|
176
|
+
{ name: 'Random string', input: 'sadfasdf', success: false, output: null },
|
|
177
|
+
{ name: 'Random number', input: 123, success: false, output: null },
|
|
178
|
+
{ name: 'Random boolean', input: true, success: false, output: null },
|
|
179
|
+
{ name: 'Random object', input: { a: 1 }, success: false, output: null },
|
|
180
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
181
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
182
|
+
];
|
|
183
|
+
|
|
184
|
+
const tt: Case[] = [
|
|
185
|
+
{
|
|
186
|
+
name: 'Implicit',
|
|
187
|
+
e: Implicit,
|
|
188
|
+
subcases: [
|
|
189
|
+
// Positive
|
|
190
|
+
{ name: 'Zero (number)', input: 0, success: true, output: Implicit.KEY_1 },
|
|
191
|
+
{ name: 'Zero (string)', input: '0', success: true, output: Implicit.KEY_1 },
|
|
192
|
+
{ name: 'One (number)', input: 1, success: true, output: Implicit.KEY_2 },
|
|
193
|
+
{ name: 'One (number)', input: '1', success: true, output: Implicit.KEY_2 },
|
|
194
|
+
{ name: 'KEY_1', input: 'KEY_1', success: true, output: Implicit.KEY_1 },
|
|
195
|
+
{ name: 'KEY_2', input: 'KEY_2', success: true, output: Implicit.KEY_2 },
|
|
196
|
+
|
|
197
|
+
// Negative
|
|
198
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
199
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
200
|
+
...common,
|
|
201
|
+
],
|
|
202
|
+
},
|
|
203
|
+
{
|
|
204
|
+
name: 'Numeric',
|
|
205
|
+
e: Numeric,
|
|
206
|
+
subcases: [
|
|
207
|
+
// Positive
|
|
208
|
+
{ name: 'Zero (number)', input: 0, success: true, output: Numeric.KEY_1 },
|
|
209
|
+
{ name: 'Zero (string)', input: '0', success: true, output: Numeric.KEY_1 },
|
|
210
|
+
{ name: 'One (number)', input: 1, success: true, output: Numeric.KEY_2 },
|
|
211
|
+
{ name: 'One (number)', input: '1', success: true, output: Numeric.KEY_2 },
|
|
212
|
+
{ name: 'KEY_1', input: 'KEY_1', success: true, output: Numeric.KEY_1 },
|
|
213
|
+
{ name: 'KEY_2', input: 'KEY_2', success: true, output: Numeric.KEY_2 },
|
|
214
|
+
|
|
215
|
+
// Negative
|
|
216
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
217
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
218
|
+
...common,
|
|
219
|
+
]
|
|
220
|
+
},
|
|
221
|
+
{
|
|
222
|
+
name: 'Strings',
|
|
223
|
+
e: Strings,
|
|
224
|
+
subcases: [
|
|
225
|
+
// Positive
|
|
226
|
+
{ name: 'Zero (string)', input: '0', success: true, output: Strings.KEY_1 },
|
|
227
|
+
{ name: 'One (number)', input: '1', success: true, output: Strings.KEY_2 },
|
|
228
|
+
{ name: 'KEY_1', input: 'KEY_1', success: true, output: Strings.KEY_1 },
|
|
229
|
+
{ name: 'KEY_2', input: 'KEY_2', success: true, output: Strings.KEY_2 },
|
|
230
|
+
|
|
231
|
+
// Negative
|
|
232
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
233
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
234
|
+
{ name: 'Zero (number)', input: 0, success: false, output: null },
|
|
235
|
+
{ name: 'One (number)', input: 1, success: false, output: null },
|
|
236
|
+
...common,
|
|
237
|
+
],
|
|
238
|
+
},
|
|
239
|
+
{
|
|
240
|
+
name: 'Mixed',
|
|
241
|
+
e: Mixed,
|
|
242
|
+
subcases: [
|
|
243
|
+
// Positive
|
|
244
|
+
{ name: 'Zero (string)', input: '0', success: true, output: Mixed.KEY_1 },
|
|
245
|
+
{ name: 'One (number)', input: 1, success: true, output: Mixed.KEY_2 },
|
|
246
|
+
{ name: 'One (number)', input: '1', success: true, output: Mixed.KEY_2 },
|
|
247
|
+
{ name: 'KEY_1', input: 'KEY_1', success: true, output: Mixed.KEY_1 },
|
|
248
|
+
{ name: 'KEY_2', input: 'KEY_2', success: true, output: Mixed.KEY_2 },
|
|
249
|
+
|
|
250
|
+
// Negative
|
|
251
|
+
{ name: 'null', input: null, success: false, output: null },
|
|
252
|
+
{ name: 'undefined', input: undefined, success: false, output: null },
|
|
253
|
+
{ name: 'Zero (number)', input: 0, success: false, output: null },
|
|
254
|
+
...common,
|
|
255
|
+
],
|
|
256
|
+
},
|
|
257
|
+
];
|
|
258
|
+
|
|
259
|
+
for (const { name, e, subcases } of tt) {
|
|
260
|
+
describe(name, () => {
|
|
261
|
+
for (const { name, input, success, output } of subcases) {
|
|
262
|
+
if (success) {
|
|
263
|
+
it(name, () => expect(Convert.enum(input, e)).toBe(output));
|
|
264
|
+
} else {
|
|
265
|
+
it(name, () => expect(() => Convert.enum(input, e)).toThrow());
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './convert.class';
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { EnumLike, EnumValue } from './types';
|
|
2
|
+
|
|
3
|
+
const enumMembers = (
|
|
4
|
+
e: EnumLike,
|
|
5
|
+
get: 'keys' | 'values',
|
|
6
|
+
): (string | number)[] => {
|
|
7
|
+
const numeric = Object.values(e)
|
|
8
|
+
.filter((v) => typeof v === 'number')
|
|
9
|
+
.map((n) => `${n}`);
|
|
10
|
+
|
|
11
|
+
return Object.entries(e)
|
|
12
|
+
.filter(([key]) => !numeric.includes(key))
|
|
13
|
+
.map((pair) => pair[get === 'keys' ? 0 : 1] as string | number);
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Easy get keys or values of any typescript enum.
|
|
18
|
+
* Works with numeric, strings or mixed enums.
|
|
19
|
+
*/
|
|
20
|
+
export class Enum {
|
|
21
|
+
/**
|
|
22
|
+
* Get keys of enum (string[])
|
|
23
|
+
*/
|
|
24
|
+
static keys(e: EnumLike): string[] {
|
|
25
|
+
return enumMembers(e, 'keys') as string[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Get values of enum ((string | number)[])
|
|
30
|
+
*/
|
|
31
|
+
static values(e: EnumLike): EnumValue[] {
|
|
32
|
+
return enumMembers(e, 'values');
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/* eslint-disable sort-keys */
|
|
2
|
+
import { Enum } from './enums.functions';
|
|
3
|
+
import type { EnumLike } from './types';
|
|
4
|
+
|
|
5
|
+
describe('Enums', () => {
|
|
6
|
+
enum ImplicitNumeric {
|
|
7
|
+
KEY_1,
|
|
8
|
+
KEY_2,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
enum ExplicitNumeric {
|
|
12
|
+
KEY_1 = 0,
|
|
13
|
+
KEY_2 = 1,
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
enum Strings {
|
|
17
|
+
KEY_1 = '1',
|
|
18
|
+
KEY_2 = '2',
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
enum Mixed {
|
|
22
|
+
KEY_1 = '1',
|
|
23
|
+
KEY_2 = 0,
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface TestCase {
|
|
27
|
+
name: string;
|
|
28
|
+
e: EnumLike;
|
|
29
|
+
keys: string[];
|
|
30
|
+
values: (string | number | boolean)[];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const tt: TestCase[] = [
|
|
34
|
+
{
|
|
35
|
+
name: 'ImplicitNumeric',
|
|
36
|
+
e: ImplicitNumeric,
|
|
37
|
+
keys: ['KEY_1', 'KEY_2'],
|
|
38
|
+
values: [0, 1],
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: 'ExplicitNumeric',
|
|
42
|
+
e: ExplicitNumeric,
|
|
43
|
+
keys: ['KEY_1', 'KEY_2'],
|
|
44
|
+
values: [0, 1],
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
name: 'Strings',
|
|
48
|
+
e: Strings,
|
|
49
|
+
keys: ['KEY_1', 'KEY_2'],
|
|
50
|
+
values: ['1', '2'],
|
|
51
|
+
},
|
|
52
|
+
{ name: 'Mixed', e: Mixed, keys: ['KEY_1', 'KEY_2'], values: ['1', 0] },
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
for (const { name, e, keys, values } of tt) {
|
|
56
|
+
it(`${name} keys`, () => expect(Enum.keys(e)).toEqual(keys));
|
|
57
|
+
it(`${name} values`, () => expect(Enum.values(e)).toEqual(values));
|
|
58
|
+
}
|
|
59
|
+
});
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { createWriteStream } from 'fs';
|
|
2
|
+
|
|
3
|
+
import type { PathLike } from 'fs';
|
|
4
|
+
import type { Readable } from 'stream';
|
|
5
|
+
|
|
6
|
+
import type { Constructor } from '../types';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Helps to create multi-line string literals in code. All double spaces and
|
|
10
|
+
* line breaks will be trimmed to single space (just like HTML)
|
|
11
|
+
* @example text`
|
|
12
|
+
* Some multi-line
|
|
13
|
+
* literal!
|
|
14
|
+
* `
|
|
15
|
+
* will produce just "Some multi-line literal"
|
|
16
|
+
*/
|
|
17
|
+
export const text = (
|
|
18
|
+
mainParts: TemplateStringsArray,
|
|
19
|
+
...intermediateParts: unknown[]
|
|
20
|
+
): string => {
|
|
21
|
+
const textWithSpaces = String.raw(mainParts, ...intermediateParts);
|
|
22
|
+
|
|
23
|
+
if (!textWithSpaces) {
|
|
24
|
+
return textWithSpaces;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return textWithSpaces.trim().replace(/\s+/g, ' ');
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Helps to create exaustive switch/case
|
|
32
|
+
* Example:
|
|
33
|
+
*
|
|
34
|
+
* switch (c: Color) {
|
|
35
|
+
* case Color.Red: return 'red';
|
|
36
|
+
* case Color.Green: return 'green';
|
|
37
|
+
* default: unreachable(c)
|
|
38
|
+
* }
|
|
39
|
+
*/
|
|
40
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function
|
|
41
|
+
export const unreachable = (_value: never): any => {};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Writes contents of readable stream to file
|
|
45
|
+
*/
|
|
46
|
+
export const streamWriteFile = (
|
|
47
|
+
stream: Readable,
|
|
48
|
+
path: PathLike,
|
|
49
|
+
): Promise<void> => {
|
|
50
|
+
return new Promise<void>((resolve, reject) => {
|
|
51
|
+
stream.pipe(createWriteStream(path));
|
|
52
|
+
stream.once('error', reject);
|
|
53
|
+
stream.once('end', resolve);
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export const isRecord = (x: unknown): x is Record<string, unknown> =>
|
|
58
|
+
typeof x === 'object' && !Array.isArray(x) && x !== null;
|
|
59
|
+
|
|
60
|
+
export const isUndefined = <T>(value: T | undefined): value is T =>
|
|
61
|
+
value === undefined;
|
|
62
|
+
|
|
63
|
+
export const hasKey = <T extends object>(obj: T, key: keyof T): boolean =>
|
|
64
|
+
key in obj;
|
|
65
|
+
|
|
66
|
+
export const isOneOf = (value: unknown, types: Constructor[]): boolean =>
|
|
67
|
+
types.some((Type) => value instanceof Type);
|
|
68
|
+
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
70
|
+
export const noop = (): void => {};
|