@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 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/parsers/size/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,gDAA8B"}
@@ -0,0 +1,7 @@
1
+ import { Size } from '../../size';
2
+ import type { PropertyParser } from '../parser.interface';
3
+ export declare class SizeParser implements PropertyParser<Size> {
4
+ type(): string;
5
+ description(): string;
6
+ parse(value: unknown): Size;
7
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SizeParser = void 0;
4
+ const size_1 = require("../../size");
5
+ const strings_1 = require("../../strings");
6
+ const types_1 = require("../../types");
7
+ class SizeParser {
8
+ type() {
9
+ return 'Timespan';
10
+ }
11
+ description() {
12
+ return (0, strings_1.text) `
13
+ Parses string in size format into Timespan instance.
14
+ Format is: ${size_1.SIZE_STRING_PATTERN}. For example: 8b, 10kb, 2gb.
15
+ `;
16
+ }
17
+ parse(value) {
18
+ types_1.Assert.isString(value);
19
+ return size_1.Size.parse(value);
20
+ }
21
+ }
22
+ exports.SizeParser = SizeParser;
23
+ //# sourceMappingURL=size.parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"size.parser.js","sourceRoot":"","sources":["../../../src/parsers/size/size.parser.ts"],"names":[],"mappings":";;;AAAA,qCAAuD;AACvD,2CAAqC;AACrC,uCAAqC;AAGrC,MAAa,UAAU;IACrB,IAAI;QACF,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,WAAW;QACT,OAAO,IAAA,cAAI,EAAA;;mBAEI,0BAAmB;KACjC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,KAAc;QAClB,cAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEvB,OAAO,WAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC3B,CAAC;CACF;AAjBD,gCAiBC"}
@@ -0,0 +1 @@
1
+ export * from './string.parser';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./string.parser"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/parsers/string/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,kDAAgC"}
@@ -0,0 +1,8 @@
1
+ import type { PropertyParser } from '../parser.interface';
2
+ export declare class StringParser implements PropertyParser<string> {
3
+ private readonly pattern?;
4
+ constructor(pattern?: RegExp | undefined);
5
+ type(): string;
6
+ description(): string;
7
+ parse(value: unknown): string;
8
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StringParser = void 0;
4
+ const types_1 = require("../../types");
5
+ class StringParser {
6
+ pattern;
7
+ constructor(pattern) {
8
+ this.pattern = pattern;
9
+ }
10
+ type() {
11
+ return 'string';
12
+ }
13
+ description() {
14
+ return 'Extract arbitrary string.';
15
+ }
16
+ parse(value) {
17
+ types_1.Assert.isString(value);
18
+ if (this.pattern) {
19
+ types_1.Assert.isTrue(this.pattern.test(value));
20
+ }
21
+ return value;
22
+ }
23
+ }
24
+ exports.StringParser = StringParser;
25
+ //# sourceMappingURL=string.parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"string.parser.js","sourceRoot":"","sources":["../../../src/parsers/string/string.parser.ts"],"names":[],"mappings":";;;AAAA,uCAAqC;AAGrC,MAAa,YAAY;IACM;IAA7B,YAA6B,OAAgB;QAAhB,YAAO,GAAP,OAAO,CAAS;IAAG,CAAC;IAEjD,IAAI;QACF,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED,WAAW;QACT,OAAO,2BAA2B,CAAC;IACrC,CAAC;IAED,KAAK,CAAC,KAAc;QAClB,cAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEvB,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACjB,cAAM,CAAC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QAC1C,CAAC;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CACF;AApBD,oCAoBC"}
@@ -0,0 +1 @@
1
+ export * from './timespan.parser';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./timespan.parser"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/parsers/timespan/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,oDAAkC"}
@@ -0,0 +1,7 @@
1
+ import { Timespan } from '../../time';
2
+ import type { PropertyParser } from '../parser.interface';
3
+ export declare class TimespanParser implements PropertyParser<Timespan> {
4
+ type(): string;
5
+ description(): string;
6
+ parse(value: unknown): Timespan;
7
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TimespanParser = void 0;
4
+ const strings_1 = require("../../strings");
5
+ const time_1 = require("../../time");
6
+ const types_1 = require("../../types");
7
+ class TimespanParser {
8
+ type() {
9
+ return 'Timespan';
10
+ }
11
+ description() {
12
+ return (0, strings_1.text) `
13
+ Parses string in timespan format into Timespan instance.
14
+ Format is: ${time_1.TIMESPAN_STRING_PATTERN}. For example: 1s, 2h, 4d.
15
+ `;
16
+ }
17
+ parse(value) {
18
+ types_1.Assert.isString(value);
19
+ return time_1.Timespan.parse(value);
20
+ }
21
+ }
22
+ exports.TimespanParser = TimespanParser;
23
+ //# sourceMappingURL=timespan.parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"timespan.parser.js","sourceRoot":"","sources":["../../../src/parsers/timespan/timespan.parser.ts"],"names":[],"mappings":";;;AAAA,2CAAqC;AACrC,qCAA+D;AAC/D,uCAAqC;AAGrC,MAAa,cAAc;IACzB,IAAI;QACF,OAAO,UAAU,CAAC;IACpB,CAAC;IAED,WAAW;QACT,OAAO,IAAA,cAAI,EAAA;;mBAEI,8BAAuB;KACrC,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,KAAc;QAClB,cAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEvB,OAAO,eAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;CACF;AAjBD,wCAiBC"}
@@ -0,0 +1,6 @@
1
+ export declare class InvalidUrl extends Error {
2
+ constructor(str: string, cause: unknown);
3
+ }
4
+ export declare class InvalidProtocol extends Error {
5
+ constructor(expectedProtocols: string[], protocol: string, url: URL);
6
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.InvalidProtocol = exports.InvalidUrl = void 0;
4
+ class InvalidUrl extends Error {
5
+ constructor(str, cause) {
6
+ super(`Invalid url string: <${str}>`);
7
+ this.name = 'InvalidUrl';
8
+ this.cause = cause;
9
+ }
10
+ }
11
+ exports.InvalidUrl = InvalidUrl;
12
+ class InvalidProtocol extends Error {
13
+ constructor(expectedProtocols, protocol, url) {
14
+ super(`Invalid protocol: <${protocol}>, expected: ${expectedProtocols}`);
15
+ this.name = 'InvalidProtocol';
16
+ this.cause = {
17
+ protocol,
18
+ url,
19
+ expected: expectedProtocols,
20
+ };
21
+ }
22
+ }
23
+ exports.InvalidProtocol = InvalidProtocol;
24
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","sourceRoot":"","sources":["../../../src/parsers/url/errors.ts"],"names":[],"mappings":";;;AAAA,MAAa,UAAW,SAAQ,KAAK;IACnC,YAAY,GAAW,EAAE,KAAc;QACrC,KAAK,CAAC,wBAAwB,GAAG,GAAG,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;IACrB,CAAC;CACF;AAND,gCAMC;AAED,MAAa,eAAgB,SAAQ,KAAK;IACxC,YAAY,iBAA2B,EAAE,QAAgB,EAAE,GAAQ;QACjE,KAAK,CAAC,sBAAsB,QAAQ,gBAAgB,iBAAiB,EAAE,CAAC,CAAC;QACzE,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;QAC9B,IAAI,CAAC,KAAK,GAAG;YACX,QAAQ;YACR,GAAG;YACH,QAAQ,EAAE,iBAAiB;SAC5B,CAAC;IACJ,CAAC;CACF;AAVD,0CAUC"}
@@ -0,0 +1 @@
1
+ export * from './url.parser';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./url.parser"), exports);
18
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/parsers/url/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,+CAA6B"}
@@ -0,0 +1,9 @@
1
+ import type { PropertyParser } from '../parser.interface';
2
+ export declare class UrlParser implements PropertyParser<URL> {
3
+ private readonly protocols;
4
+ constructor(...protocols: string[]);
5
+ type(): string;
6
+ description(): string;
7
+ parse(value: unknown): URL;
8
+ private assertProtocol;
9
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UrlParser = void 0;
4
+ const types_1 = require("../../types");
5
+ const errors_1 = require("./errors");
6
+ class UrlParser {
7
+ protocols;
8
+ constructor(...protocols) {
9
+ this.protocols = protocols;
10
+ }
11
+ type() {
12
+ return 'URL';
13
+ }
14
+ description() {
15
+ return 'Parses string into instance of URL';
16
+ }
17
+ parse(value) {
18
+ types_1.Assert.isString(value);
19
+ try {
20
+ const url = new URL(value);
21
+ this.assertProtocol(url);
22
+ return url;
23
+ }
24
+ catch (error) {
25
+ throw new errors_1.InvalidUrl(value, error);
26
+ }
27
+ }
28
+ assertProtocol(url) {
29
+ if (this.protocols.length === 0) {
30
+ return;
31
+ }
32
+ if (!this.protocols.includes(url.protocol)) {
33
+ throw new errors_1.InvalidProtocol(this.protocols, url.protocol, url);
34
+ }
35
+ }
36
+ }
37
+ exports.UrlParser = UrlParser;
38
+ //# sourceMappingURL=url.parser.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"url.parser.js","sourceRoot":"","sources":["../../../src/parsers/url/url.parser.ts"],"names":[],"mappings":";;;AAAA,uCAAqC;AAGrC,qCAAuD;AAEvD,MAAa,SAAS;IACH,SAAS,CAAW;IAErC,YAAY,GAAG,SAAmB;QAChC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED,IAAI;QACF,OAAO,KAAK,CAAC;IACf,CAAC;IAED,WAAW;QACT,OAAO,oCAAoC,CAAC;IAC9C,CAAC;IAED,KAAK,CAAC,KAAc;QAClB,cAAM,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAEvB,IAAI,CAAC;YACH,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;YAE3B,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,CAAC;YACzB,OAAO,GAAG,CAAC;QACb,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,mBAAU,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IAEO,cAAc,CAAC,GAAQ;QAC7B,IAAI,IAAI,CAAC,SAAS,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YAChC,OAAO;QACT,CAAC;QAED,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC3C,MAAM,IAAI,wBAAe,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;CACF;AArCD,8BAqCC"}
@@ -1,5 +1,7 @@
1
1
  export declare class Assert {
2
2
  static isString(x: unknown): asserts x is string;
3
3
  static isNumber(x: unknown): asserts x is number;
4
+ static isBoolean(x: unknown): asserts x is boolean;
5
+ static isTrue(x: unknown): asserts x is true;
4
6
  static isError(x: unknown): asserts x is Error;
5
7
  }
@@ -14,6 +14,18 @@ class Assert {
14
14
  }
15
15
  throw new Error(`<${x}> is not a number.`);
16
16
  }
17
+ static isBoolean(x) {
18
+ if (typeof x === 'boolean') {
19
+ return;
20
+ }
21
+ throw new Error(`<${x}> is not a boolean.`);
22
+ }
23
+ static isTrue(x) {
24
+ if (x === true) {
25
+ return;
26
+ }
27
+ throw new Error(`<${x}> is not true.`);
28
+ }
17
29
  static isError(x) {
18
30
  if (x instanceof Error) {
19
31
  return;
@@ -1 +1 @@
1
- {"version":3,"file":"assert.class.js","sourceRoot":"","sources":["../../src/types/assert.class.ts"],"names":[],"mappings":";;;AAAA,MAAa,MAAM;IACjB,MAAM,CAAC,QAAQ,CAAC,CAAU;QACxB,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC1B,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,CAAC,QAAQ,CAAC,CAAU;QACxB,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC1B,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,CAAU;QACvB,IAAI,CAAC,YAAY,KAAK,EAAE,CAAC;YACvB,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,6BAA6B,CAAC,CAAC;IACtE,CAAC;CACF;AAxBD,wBAwBC"}
1
+ {"version":3,"file":"assert.class.js","sourceRoot":"","sources":["../../src/types/assert.class.ts"],"names":[],"mappings":";;;AAAA,MAAa,MAAM;IACjB,MAAM,CAAC,QAAQ,CAAC,CAAU;QACxB,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC1B,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,CAAC,QAAQ,CAAC,CAAU;QACxB,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE,CAAC;YAC1B,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IAC7C,CAAC;IAED,MAAM,CAAC,SAAS,CAAC,CAAU;QACzB,IAAI,OAAO,CAAC,KAAK,SAAS,EAAE,CAAC;YAC3B,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;IAC9C,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,CAAU;QACtB,IAAI,CAAC,KAAK,IAAI,EAAE,CAAC;YACf,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC;IACzC,CAAC;IAED,MAAM,CAAC,OAAO,CAAC,CAAU;QACvB,IAAI,CAAC,YAAY,KAAK,EAAE,CAAC;YACvB,OAAO;QACT,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,IAAI,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,6BAA6B,CAAC,CAAC;IACtE,CAAC;CACF;AAxCD,wBAwCC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsdk/common",
3
- "version": "5.7.0-next.2",
3
+ "version": "6.0.0-next.0",
4
4
  "description": "Useful common classes, functions and types",
5
5
  "license": "Apache License 2.0",
6
6
  "publishConfig": {
@@ -16,5 +16,5 @@
16
16
  "dependencies": {
17
17
  "rxjs": "^7.8.1"
18
18
  },
19
- "gitHead": "c85fca0b594a2ea168d06f1d2941a4fb05b59440"
19
+ "gitHead": "215cccea23d95118dd8b6af3ce11c6a35ce19c30"
20
20
  }
@@ -14,15 +14,42 @@ export interface NormalizeOptions {
14
14
  const DEFAULT_MAX_DEPTH = 10;
15
15
 
16
16
  /**
17
- * Prepares object to be stringified with JSON.stringify()
18
- * - removes circular references.
19
- * - makes message, stack and cause properties of Errors enumerable
20
- * - (optional) sorts error keys for human readability
21
- * - (optional) filters out provided keys
17
+ * Creates a function that prepares objects for JSON.stringify().
22
18
  *
23
- * NOTE: Function is recursive
19
+ * This is a higher-order function that creates a closure over a cache Set.
20
+ * The cache is used to detect circular references during normalization.
21
+ * Each call to normalizer() creates a new cache, ensuring thread-safety
22
+ * and preventing memory leaks between different normalization operations.
24
23
  *
25
- * @returns unknown
24
+ * Features:
25
+ * - Removes circular references (replaces with '[circular]')
26
+ * - Makes Error properties (message, stack, cause) enumerable
27
+ * - Handles special types:
28
+ * - Buffer -> '[Buffer size]'
29
+ * - ArrayBuffer -> '[ArrayBuffer size]'
30
+ * - Map -> '[Map size]'
31
+ * - Set -> '[Set size]'
32
+ * - Limits object nesting depth (default: 10)
33
+ * - Protects against large objects (>16M keys)
34
+ *
35
+ * @example
36
+ * // Create two independent normalizers
37
+ * const normalizer1 = normalizer();
38
+ * const normalizer2 = normalizer();
39
+ *
40
+ * // Each normalizer has its own circular reference cache
41
+ * const obj1 = { circular: {} };
42
+ * obj1.circular = obj1;
43
+ * normalizer1(obj1); // Safe: uses first cache
44
+ * normalizer2(obj1); // Safe: uses second cache
45
+ *
46
+ * @returns A normalization function with its own circular reference cache that accepts:
47
+ * @param x - Value to normalize
48
+ * @param options - Normalization options:
49
+ * - filterKeys: Array of keys to exclude
50
+ * - sortKeys: Custom key sorting configuration
51
+ * - maxDepth: Maximum nesting depth (default: 10)
52
+ * @param depth - Current recursion depth (internal use)
26
53
  */
27
54
  export const normalizer = (): ((
28
55
  x: unknown,
@@ -53,6 +80,11 @@ export const normalizer = (): ((
53
80
  return `[Buffer ${Buffer.byteLength(xObject)}]`;
54
81
  }
55
82
 
83
+ // Array is not serializable
84
+ if (xObject instanceof ArrayBuffer) {
85
+ return `[ArrayBuffer ${xObject.byteLength}]`;
86
+ }
87
+
56
88
  // TODO: maybe serialize Map as record/dict
57
89
  if (xObject instanceof Map) {
58
90
  return `[Map ${xObject.size}]`;
package/src/index.ts CHANGED
@@ -6,3 +6,4 @@ export * from './functions';
6
6
  export * from './size';
7
7
  export * from './strings';
8
8
  export * from './observable';
9
+ export * from './parsers';
@@ -0,0 +1,34 @@
1
+ import { text } from '../../strings';
2
+ import { Assert } from '../../types';
3
+ import type { PropertyParser } from '../parser.interface';
4
+
5
+ export class ArrayParser<T> implements PropertyParser<T[]> {
6
+ constructor(
7
+ private readonly singleValueParser: PropertyParser<T>,
8
+ private readonly options = { separator: ',' },
9
+ ) {}
10
+
11
+ type(): string {
12
+ return `(array)${this.singleValueParser.type()}`;
13
+ }
14
+
15
+ description(): string {
16
+ return text`
17
+ Extracts array of values. If raw value is a string
18
+ <${this.options.separator}> will used as separator. Each value:
19
+ ${this.singleValueParser.description()}
20
+ `;
21
+ }
22
+
23
+ parse(value: unknown): T[] {
24
+ if (Array.isArray(value)) {
25
+ return value.map((x) => this.singleValueParser.parse(x));
26
+ }
27
+
28
+ Assert.isString(value);
29
+
30
+ return value
31
+ .split(this.options.separator)
32
+ .map((x) => this.singleValueParser.parse(x));
33
+ }
34
+ }
@@ -0,0 +1 @@
1
+ export * from './array.parser';
@@ -0,0 +1,20 @@
1
+ import { Convert } from '../../convert';
2
+ import { text } from '../../strings';
3
+ import type { PropertyParser } from '../parser.interface';
4
+
5
+ export class BoolParser implements PropertyParser<boolean> {
6
+ type(): string {
7
+ return 'boolean';
8
+ }
9
+
10
+ description(): string {
11
+ return text`
12
+ Extracts boolean value. Any boolean value or strings
13
+ 'true' and 'false' will pass.
14
+ `;
15
+ }
16
+
17
+ parse(value: unknown): boolean {
18
+ return Convert.boolean(value);
19
+ }
20
+ }
@@ -0,0 +1,4 @@
1
+ describe('BoolParser', () => {
2
+ it('See tests for Convert.boolean() in @rsdk/common', () =>
3
+ expect(true).toBe(true));
4
+ });
@@ -0,0 +1 @@
1
+ export * from './bool.parser';
@@ -0,0 +1,24 @@
1
+ import { Convert } from '../../convert';
2
+ import type { EnumLike, EnumValue } from '../../enums';
3
+ import { Enum } from '../../enums';
4
+ import { text } from '../../strings';
5
+ import type { PropertyParser } from '../parser.interface';
6
+
7
+ export class EnumParser<E extends EnumValue> implements PropertyParser<E> {
8
+ constructor(private readonly enumeration: EnumLike) {}
9
+
10
+ type(): string {
11
+ return `string|number(enum_value)`;
12
+ }
13
+
14
+ description(): string {
15
+ return text`
16
+ Extracts strings or numbers and ensures it is one of:
17
+ ${Enum.values(this.enumeration).join(', ')}
18
+ `;
19
+ }
20
+
21
+ parse(value: unknown): E {
22
+ return Convert.enum<E>(value, this.enumeration);
23
+ }
24
+ }
@@ -0,0 +1,5 @@
1
+ describe('EnumParser', () => {
2
+ /* eslint-disable @typescript-eslint/no-empty-function */
3
+ it('See tests for Convert.enum() in @rsdk/common', () =>
4
+ expect(true).toBe(true));
5
+ });
@@ -0,0 +1 @@
1
+ export * from './enum.parser';
@@ -0,0 +1,11 @@
1
+ export * from './boolean';
2
+ export * from './enum';
3
+ export * from './int';
4
+ export * from './number';
5
+ export * from './size';
6
+ export * from './string';
7
+ export * from './timespan';
8
+ export * from './url';
9
+ export * from './array';
10
+ export * from './json';
11
+ export * from './parser.interface';
@@ -0,0 +1 @@
1
+ export * from './int.parser';
@@ -0,0 +1,20 @@
1
+ import { Convert } from '../../convert';
2
+ import { text } from '../../strings';
3
+ import type { PropertyParser } from '../parser.interface';
4
+
5
+ export class IntParser implements PropertyParser<number> {
6
+ type(): string {
7
+ return 'number(int)';
8
+ }
9
+
10
+ description(): string {
11
+ return text`
12
+ Extracts valid integer (uses parseInt under the hood).
13
+ Floating point numbers, NaN, Infinity will be rejected.
14
+ `;
15
+ }
16
+
17
+ parse(value: unknown): number {
18
+ return Convert.int(value);
19
+ }
20
+ }
@@ -0,0 +1,5 @@
1
+ describe('IntParser', () => {
2
+ /* eslint-disable @typescript-eslint/no-empty-function */
3
+ it('See tests for Convert.int() in @rsdk/common', () =>
4
+ expect(true).toBe(true));
5
+ });
@@ -0,0 +1 @@
1
+ export * from './json.parser';
@@ -0,0 +1,15 @@
1
+ import type { PropertyParser } from '../parser.interface';
2
+
3
+ export class JsonParser implements PropertyParser<Record<string, any>> {
4
+ description(): string {
5
+ return 'parse string as json';
6
+ }
7
+
8
+ parse(value: string): Record<string, any> {
9
+ return JSON.parse(value);
10
+ }
11
+
12
+ type(): string {
13
+ return 'json';
14
+ }
15
+ }
@@ -0,0 +1 @@
1
+ export * from './number.parser';
@@ -0,0 +1,23 @@
1
+ import { Convert } from '../../convert';
2
+ import { text } from '../../strings';
3
+ import type { PropertyParser } from '../parser.interface';
4
+
5
+ export class NumberParser implements PropertyParser<number> {
6
+ type(): string {
7
+ return 'number';
8
+ }
9
+
10
+ description(): string {
11
+ return text`
12
+ Extracts numeric value (uses parseFloat under the hood).
13
+ NaN and Infinity will be rejected.
14
+
15
+ NOTE: Use correct decimal delimiter according to locale. See
16
+ https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Intl
17
+ `;
18
+ }
19
+
20
+ parse(value: unknown): number {
21
+ return Convert.number(value);
22
+ }
23
+ }
@@ -0,0 +1,5 @@
1
+ describe('NumberParser', () => {
2
+ /* eslint-disable @typescript-eslint/no-empty-function */
3
+ it('See tests for Convert.number() in @rsdk/common', () =>
4
+ expect(true).toBe(true));
5
+ });
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Implement this interface to create more parsers.
3
+ * For example: email, BigInt, guid etc.
4
+ *
5
+ * NOTE: In other modules it is more common to define metadata
6
+ * via decorators. But this case is a bit different.
7
+ *
8
+ * 1. Parser implementations can have constructors with parameters
9
+ * which will affect it's behaviour. It's will be nice to have it
10
+ * in metadata, and decorators won't provide this.
11
+ *
12
+ * 2. Metadata will be extracted via type() and description() methods
13
+ * mostly during decorator factories (such as @Property()) code is
14
+ * executed. Thus output **can** be used in autodoc
15
+ */
16
+ export interface PropertyParser<T = any> {
17
+ type(): string;
18
+
19
+ description(): string;
20
+
21
+ parse(value: unknown): T;
22
+ }
@@ -0,0 +1 @@
1
+ export * from './size.parser';