@rsdk/common 5.7.0-next.2 → 6.0.0-next.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.
Files changed (112) hide show
  1. package/dist/functions/obj-normalization.d.ts +34 -7
  2. package/dist/functions/obj-normalization.js +38 -7
  3. package/dist/functions/obj-normalization.js.map +1 -1
  4. package/dist/index.d.ts +1 -0
  5. package/dist/index.js +1 -0
  6. package/dist/index.js.map +1 -1
  7. package/dist/parsers/array/array.parser.d.ts +11 -0
  8. package/dist/parsers/array/array.parser.js +34 -0
  9. package/dist/parsers/array/array.parser.js.map +1 -0
  10. package/dist/parsers/array/index.d.ts +1 -0
  11. package/dist/parsers/array/index.js +18 -0
  12. package/dist/parsers/array/index.js.map +1 -0
  13. package/dist/parsers/boolean/bool.parser.d.ts +6 -0
  14. package/dist/parsers/boolean/bool.parser.js +21 -0
  15. package/dist/parsers/boolean/bool.parser.js.map +1 -0
  16. package/dist/parsers/boolean/index.d.ts +1 -0
  17. package/dist/parsers/boolean/index.js +18 -0
  18. package/dist/parsers/boolean/index.js.map +1 -0
  19. package/dist/parsers/enum/enum.parser.d.ts +9 -0
  20. package/dist/parsers/enum/enum.parser.js +26 -0
  21. package/dist/parsers/enum/enum.parser.js.map +1 -0
  22. package/dist/parsers/enum/index.d.ts +1 -0
  23. package/dist/parsers/enum/index.js +18 -0
  24. package/dist/parsers/enum/index.js.map +1 -0
  25. package/dist/parsers/index.d.ts +11 -0
  26. package/dist/parsers/index.js +28 -0
  27. package/dist/parsers/index.js.map +1 -0
  28. package/dist/parsers/int/index.d.ts +1 -0
  29. package/dist/parsers/int/index.js +18 -0
  30. package/dist/parsers/int/index.js.map +1 -0
  31. package/dist/parsers/int/int.parser.d.ts +6 -0
  32. package/dist/parsers/int/int.parser.js +21 -0
  33. package/dist/parsers/int/int.parser.js.map +1 -0
  34. package/dist/parsers/json/index.d.ts +1 -0
  35. package/dist/parsers/json/index.js +18 -0
  36. package/dist/parsers/json/index.js.map +1 -0
  37. package/dist/parsers/json/json.parser.d.ts +6 -0
  38. package/dist/parsers/json/json.parser.js +16 -0
  39. package/dist/parsers/json/json.parser.js.map +1 -0
  40. package/dist/parsers/number/index.d.ts +1 -0
  41. package/dist/parsers/number/index.js +18 -0
  42. package/dist/parsers/number/index.js.map +1 -0
  43. package/dist/parsers/number/number.parser.d.ts +6 -0
  44. package/dist/parsers/number/number.parser.js +24 -0
  45. package/dist/parsers/number/number.parser.js.map +1 -0
  46. package/dist/parsers/parser.interface.d.ts +20 -0
  47. package/dist/parsers/parser.interface.js +3 -0
  48. package/dist/parsers/parser.interface.js.map +1 -0
  49. package/dist/parsers/size/index.d.ts +1 -0
  50. package/dist/parsers/size/index.js +18 -0
  51. package/dist/parsers/size/index.js.map +1 -0
  52. package/dist/parsers/size/size.parser.d.ts +7 -0
  53. package/dist/parsers/size/size.parser.js +23 -0
  54. package/dist/parsers/size/size.parser.js.map +1 -0
  55. package/dist/parsers/string/index.d.ts +1 -0
  56. package/dist/parsers/string/index.js +18 -0
  57. package/dist/parsers/string/index.js.map +1 -0
  58. package/dist/parsers/string/string.parser.d.ts +8 -0
  59. package/dist/parsers/string/string.parser.js +25 -0
  60. package/dist/parsers/string/string.parser.js.map +1 -0
  61. package/dist/parsers/timespan/index.d.ts +1 -0
  62. package/dist/parsers/timespan/index.js +18 -0
  63. package/dist/parsers/timespan/index.js.map +1 -0
  64. package/dist/parsers/timespan/timespan.parser.d.ts +7 -0
  65. package/dist/parsers/timespan/timespan.parser.js +23 -0
  66. package/dist/parsers/timespan/timespan.parser.js.map +1 -0
  67. package/dist/parsers/url/errors.d.ts +6 -0
  68. package/dist/parsers/url/errors.js +24 -0
  69. package/dist/parsers/url/errors.js.map +1 -0
  70. package/dist/parsers/url/index.d.ts +1 -0
  71. package/dist/parsers/url/index.js +18 -0
  72. package/dist/parsers/url/index.js.map +1 -0
  73. package/dist/parsers/url/url.parser.d.ts +9 -0
  74. package/dist/parsers/url/url.parser.js +38 -0
  75. package/dist/parsers/url/url.parser.js.map +1 -0
  76. package/dist/types/assert.class.d.ts +2 -0
  77. package/dist/types/assert.class.js +12 -0
  78. package/dist/types/assert.class.js.map +1 -1
  79. package/package.json +2 -2
  80. package/src/functions/obj-normalization.ts +39 -7
  81. package/src/index.ts +1 -0
  82. package/src/parsers/array/array.parser.ts +34 -0
  83. package/src/parsers/array/index.ts +1 -0
  84. package/src/parsers/boolean/bool.parser.ts +20 -0
  85. package/src/parsers/boolean/bool.spec.ts +4 -0
  86. package/src/parsers/boolean/index.ts +1 -0
  87. package/src/parsers/enum/enum.parser.ts +24 -0
  88. package/src/parsers/enum/enum.spec.ts +5 -0
  89. package/src/parsers/enum/index.ts +1 -0
  90. package/src/parsers/index.ts +11 -0
  91. package/src/parsers/int/index.ts +1 -0
  92. package/src/parsers/int/int.parser.ts +20 -0
  93. package/src/parsers/int/int.spec.ts +5 -0
  94. package/src/parsers/json/index.ts +1 -0
  95. package/src/parsers/json/json.parser.ts +15 -0
  96. package/src/parsers/number/index.ts +1 -0
  97. package/src/parsers/number/number.parser.ts +23 -0
  98. package/src/parsers/number/number.spec.ts +5 -0
  99. package/src/parsers/parser.interface.ts +22 -0
  100. package/src/parsers/size/index.ts +1 -0
  101. package/src/parsers/size/size.parser.ts +23 -0
  102. package/src/parsers/size/size.spec.ts +4 -0
  103. package/src/parsers/string/index.ts +1 -0
  104. package/src/parsers/string/string.parser.ts +24 -0
  105. package/src/parsers/string/string.spec.ts +10 -0
  106. package/src/parsers/timespan/index.ts +1 -0
  107. package/src/parsers/timespan/timespan.parser.ts +23 -0
  108. package/src/parsers/timespan/timespan.spec.ts +5 -0
  109. package/src/parsers/url/errors.ts +19 -0
  110. package/src/parsers/url/index.ts +1 -0
  111. package/src/parsers/url/url.parser.ts +43 -0
  112. package/src/types/assert.class.ts +16 -0
@@ -0,0 +1,23 @@
1
+ import { Size, SIZE_STRING_PATTERN } from '../../size';
2
+ import { text } from '../../strings';
3
+ import { Assert } from '../../types';
4
+ import type { PropertyParser } from '../parser.interface';
5
+
6
+ export class SizeParser implements PropertyParser<Size> {
7
+ type(): string {
8
+ return 'Timespan';
9
+ }
10
+
11
+ description(): string {
12
+ return text`
13
+ Parses string in size format into Timespan instance.
14
+ Format is: ${SIZE_STRING_PATTERN}. For example: 8b, 10kb, 2gb.
15
+ `;
16
+ }
17
+
18
+ parse(value: unknown): Size {
19
+ Assert.isString(value);
20
+
21
+ return Size.parse(value);
22
+ }
23
+ }
@@ -0,0 +1,4 @@
1
+ describe('SizeParser', () => {
2
+ /* eslint-disable @typescript-eslint/no-empty-function */
3
+ it('See tests in @rsdk/common', () => expect(true).toBe(true));
4
+ });
@@ -0,0 +1 @@
1
+ export * from './string.parser';
@@ -0,0 +1,24 @@
1
+ import { Assert } from '../../types';
2
+ import type { PropertyParser } from '../parser.interface';
3
+
4
+ export class StringParser implements PropertyParser<string> {
5
+ constructor(private readonly pattern?: RegExp) {}
6
+
7
+ type(): string {
8
+ return 'string';
9
+ }
10
+
11
+ description(): string {
12
+ return 'Extract arbitrary string.';
13
+ }
14
+
15
+ parse(value: unknown): string {
16
+ Assert.isString(value);
17
+
18
+ if (this.pattern) {
19
+ Assert.isTrue(this.pattern.test(value));
20
+ }
21
+
22
+ return value;
23
+ }
24
+ }
@@ -0,0 +1,10 @@
1
+ import { StringParser } from './string.parser';
2
+
3
+ describe('StringMatcher', () => {
4
+ it('match', () => {
5
+ const parser = new StringParser(/^[a-z]+$/);
6
+
7
+ expect(parser.parse('abcd')).toBe('abcd');
8
+ expect(() => parser.parse('1234')).toThrow();
9
+ });
10
+ });
@@ -0,0 +1 @@
1
+ export * from './timespan.parser';
@@ -0,0 +1,23 @@
1
+ import { text } from '../../strings';
2
+ import { Timespan, TIMESPAN_STRING_PATTERN } from '../../time';
3
+ import { Assert } from '../../types';
4
+ import type { PropertyParser } from '../parser.interface';
5
+
6
+ export class TimespanParser implements PropertyParser<Timespan> {
7
+ type(): string {
8
+ return 'Timespan';
9
+ }
10
+
11
+ description(): string {
12
+ return text`
13
+ Parses string in timespan format into Timespan instance.
14
+ Format is: ${TIMESPAN_STRING_PATTERN}. For example: 1s, 2h, 4d.
15
+ `;
16
+ }
17
+
18
+ parse(value: unknown): Timespan {
19
+ Assert.isString(value);
20
+
21
+ return Timespan.parse(value);
22
+ }
23
+ }
@@ -0,0 +1,5 @@
1
+ describe('TimespanParser', () => {
2
+ /* eslint-disable @typescript-eslint/no-empty-function */
3
+ it('See tests for Timespan class in @rsdk/common', () =>
4
+ expect(true).toBe(true));
5
+ });
@@ -0,0 +1,19 @@
1
+ export class InvalidUrl extends Error {
2
+ constructor(str: string, cause: unknown) {
3
+ super(`Invalid url string: <${str}>`);
4
+ this.name = 'InvalidUrl';
5
+ this.cause = cause;
6
+ }
7
+ }
8
+
9
+ export class InvalidProtocol extends Error {
10
+ constructor(expectedProtocols: string[], protocol: string, url: URL) {
11
+ super(`Invalid protocol: <${protocol}>, expected: ${expectedProtocols}`);
12
+ this.name = 'InvalidProtocol';
13
+ this.cause = {
14
+ protocol,
15
+ url,
16
+ expected: expectedProtocols,
17
+ };
18
+ }
19
+ }
@@ -0,0 +1 @@
1
+ export * from './url.parser';
@@ -0,0 +1,43 @@
1
+ import { Assert } from '../../types';
2
+ import type { PropertyParser } from '../parser.interface';
3
+
4
+ import { InvalidProtocol, InvalidUrl } from './errors';
5
+
6
+ export class UrlParser implements PropertyParser<URL> {
7
+ private readonly protocols: string[];
8
+
9
+ constructor(...protocols: string[]) {
10
+ this.protocols = protocols;
11
+ }
12
+
13
+ type(): string {
14
+ return 'URL';
15
+ }
16
+
17
+ description(): string {
18
+ return 'Parses string into instance of URL';
19
+ }
20
+
21
+ parse(value: unknown): URL {
22
+ Assert.isString(value);
23
+
24
+ try {
25
+ const url = new URL(value);
26
+
27
+ this.assertProtocol(url);
28
+ return url;
29
+ } catch (error) {
30
+ throw new InvalidUrl(value, error);
31
+ }
32
+ }
33
+
34
+ private assertProtocol(url: URL): void {
35
+ if (this.protocols.length === 0) {
36
+ return;
37
+ }
38
+
39
+ if (!this.protocols.includes(url.protocol)) {
40
+ throw new InvalidProtocol(this.protocols, url.protocol, url);
41
+ }
42
+ }
43
+ }
@@ -15,6 +15,22 @@ export class Assert {
15
15
  throw new Error(`<${x}> is not a number.`);
16
16
  }
17
17
 
18
+ static isBoolean(x: unknown): asserts x is boolean {
19
+ if (typeof x === 'boolean') {
20
+ return;
21
+ }
22
+
23
+ throw new Error(`<${x}> is not a boolean.`);
24
+ }
25
+
26
+ static isTrue(x: unknown): asserts x is true {
27
+ if (x === true) {
28
+ return;
29
+ }
30
+
31
+ throw new Error(`<${x}> is not true.`);
32
+ }
33
+
18
34
  static isError(x: unknown): asserts x is Error {
19
35
  if (x instanceof Error) {
20
36
  return;