@vorplex/core 0.0.8 → 0.0.11

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 (187) hide show
  1. package/dist/consts/mime-type.const.d.ts +456 -0
  2. package/dist/consts/mime-type.const.js +456 -0
  3. package/dist/consts/unit.const.d.ts +22 -0
  4. package/dist/consts/unit.const.js +22 -0
  5. package/dist/functions/debounce.function.d.ts +1 -0
  6. package/dist/functions/debounce.function.js +12 -0
  7. package/dist/functions/is-node-environment.function.d.ts +1 -0
  8. package/dist/functions/is-node-environment.function.js +3 -0
  9. package/dist/functions/parse-path-selector.function.d.ts +2 -0
  10. package/dist/functions/parse-path-selector.function.js +17 -0
  11. package/dist/functions/parse-url.function.d.ts +12 -0
  12. package/dist/functions/parse-url.function.js +25 -0
  13. package/dist/index.d.ts +92 -0
  14. package/dist/index.js +93 -0
  15. package/dist/interfaces/group.interface.d.ts +4 -0
  16. package/dist/interfaces/group.interface.js +0 -0
  17. package/dist/interfaces/key-value.interface.d.ts +4 -0
  18. package/dist/interfaces/key-value.interface.js +0 -0
  19. package/dist/modules/api/socket.model.d.ts +26 -0
  20. package/dist/modules/api/socket.model.js +66 -0
  21. package/dist/modules/api/web-client.model.d.ts +16 -0
  22. package/dist/modules/api/web-client.model.js +34 -0
  23. package/dist/modules/array/array.util.d.ts +17 -0
  24. package/dist/modules/array/array.util.js +105 -0
  25. package/dist/modules/changes/changes.util.d.ts +45 -0
  26. package/dist/modules/changes/changes.util.js +330 -0
  27. package/dist/modules/color/color-data.interface.d.ts +10 -0
  28. package/dist/modules/color/color-data.interface.js +0 -0
  29. package/dist/modules/color/color-formats.const.d.ts +9 -0
  30. package/dist/modules/color/color-formats.const.js +8 -0
  31. package/dist/modules/color/color.type.d.ts +4 -0
  32. package/dist/modules/color/color.type.js +0 -0
  33. package/dist/modules/color/color.util.d.ts +41 -0
  34. package/dist/modules/color/color.util.js +327 -0
  35. package/dist/modules/color/colors.const.d.ts +151 -0
  36. package/dist/modules/color/colors.const.js +150 -0
  37. package/dist/modules/color/hsl.interface.d.ts +6 -0
  38. package/dist/modules/color/hsl.interface.js +0 -0
  39. package/dist/modules/color/hsv.interface.d.ts +6 -0
  40. package/dist/modules/color/hsv.interface.js +0 -0
  41. package/dist/modules/color/rgb.interface.d.ts +6 -0
  42. package/dist/modules/color/rgb.interface.js +0 -0
  43. package/dist/modules/compression/compression.util.d.ts +4 -0
  44. package/dist/modules/compression/compression.util.js +52 -0
  45. package/dist/modules/date/date.util.d.ts +37 -0
  46. package/dist/modules/date/date.util.js +84 -0
  47. package/dist/modules/enum/enum.util.d.ts +17 -0
  48. package/dist/modules/enum/enum.util.js +69 -0
  49. package/dist/modules/hash/hash.util.d.ts +3 -0
  50. package/dist/modules/hash/hash.util.js +9 -0
  51. package/dist/modules/id/id.util.d.ts +7 -0
  52. package/dist/modules/id/id.util.js +33 -0
  53. package/dist/modules/injector/decorators/inject-token.decorator.d.ts +14 -0
  54. package/dist/modules/injector/decorators/inject-token.decorator.js +12 -0
  55. package/dist/modules/injector/decorators/injectable.decorator.d.ts +9 -0
  56. package/dist/modules/injector/decorators/injectable.decorator.js +7 -0
  57. package/dist/modules/injector/injector.model.d.ts +25 -0
  58. package/dist/modules/injector/injector.model.js +152 -0
  59. package/dist/modules/injector/provider-scopes.enum.d.ts +5 -0
  60. package/dist/modules/injector/provider-scopes.enum.js +6 -0
  61. package/dist/modules/injector/provider.interface.d.ts +10 -0
  62. package/dist/modules/injector/provider.interface.js +0 -0
  63. package/dist/modules/json-filter/json-filter.function.d.ts +41 -0
  64. package/dist/modules/json-filter/json-filter.function.js +75 -0
  65. package/dist/modules/logging/console-logger.model.d.ts +36 -0
  66. package/dist/modules/logging/console-logger.model.js +44 -0
  67. package/dist/modules/logging/logger.model.d.ts +6 -0
  68. package/dist/modules/logging/logger.model.js +7 -0
  69. package/dist/modules/logging/task.d.ts +38 -0
  70. package/dist/modules/logging/task.js +133 -0
  71. package/dist/modules/math/line.d.ts +12 -0
  72. package/dist/modules/math/line.js +50 -0
  73. package/dist/modules/math/point.d.ts +34 -0
  74. package/dist/modules/math/point.js +71 -0
  75. package/dist/modules/math/polygon.d.ts +45 -0
  76. package/dist/modules/math/polygon.js +273 -0
  77. package/dist/modules/math/rect.d.ts +6 -0
  78. package/dist/modules/math/rect.js +0 -0
  79. package/dist/modules/math/size.d.ts +4 -0
  80. package/dist/modules/math/size.js +0 -0
  81. package/dist/modules/number/number.util.d.ts +9 -0
  82. package/dist/modules/number/number.util.js +27 -0
  83. package/dist/modules/object/object.util.d.ts +7 -0
  84. package/dist/modules/object/object.util.js +63 -0
  85. package/dist/modules/path/path.util.d.ts +11 -0
  86. package/dist/modules/path/path.util.js +59 -0
  87. package/dist/modules/random/random.util.d.ts +17 -0
  88. package/dist/modules/random/random.util.js +57 -0
  89. package/dist/modules/readable-stream/readable-stream.util.d.ts +3 -0
  90. package/dist/modules/readable-stream/readable-stream.util.js +26 -0
  91. package/dist/modules/reflection/interfaces/parameter.interface.d.ts +4 -0
  92. package/dist/modules/reflection/interfaces/parameter.interface.js +0 -0
  93. package/dist/modules/reflection/models/attribute.model.d.ts +12 -0
  94. package/dist/modules/reflection/models/attribute.model.js +17 -0
  95. package/dist/modules/reflection/models/class-attribute.model.d.ts +4 -0
  96. package/dist/modules/reflection/models/class-attribute.model.js +6 -0
  97. package/dist/modules/reflection/models/method-attribute.model.d.ts +5 -0
  98. package/dist/modules/reflection/models/method-attribute.model.js +8 -0
  99. package/dist/modules/reflection/models/parameter-attribute.model.d.ts +7 -0
  100. package/dist/modules/reflection/models/parameter-attribute.model.js +21 -0
  101. package/dist/modules/reflection/models/property-attribute.model.d.ts +5 -0
  102. package/dist/modules/reflection/models/property-attribute.model.js +8 -0
  103. package/dist/modules/reflection/models/property-proxy-attribute.model.d.ts +15 -0
  104. package/dist/modules/reflection/models/property-proxy-attribute.model.js +19 -0
  105. package/dist/modules/reflection/types/constructor.type.d.ts +2 -0
  106. package/dist/modules/reflection/types/constructor.type.js +0 -0
  107. package/dist/modules/reflection/types/instance.type.d.ts +7 -0
  108. package/dist/modules/reflection/types/instance.type.js +0 -0
  109. package/dist/modules/reflection/types/type.type.d.ts +2 -0
  110. package/dist/modules/reflection/types/type.type.js +0 -0
  111. package/dist/modules/reflection/utils/decorator.util.d.ts +70 -0
  112. package/dist/modules/reflection/utils/decorator.util.js +135 -0
  113. package/dist/modules/reflection/utils/reflection.util.d.ts +140 -0
  114. package/dist/modules/reflection/utils/reflection.util.js +311 -0
  115. package/dist/modules/router/router.util.d.ts +33 -0
  116. package/dist/modules/router/router.util.js +59 -0
  117. package/dist/modules/state/adaptors/array/array-adaptor.util.d.ts +25 -0
  118. package/dist/modules/state/adaptors/array/array-adaptor.util.js +97 -0
  119. package/dist/modules/state/adaptors/entity/entity-adaptor.util.d.ts +35 -0
  120. package/dist/modules/state/adaptors/entity/entity-adaptor.util.js +158 -0
  121. package/dist/modules/state/adaptors/entity/entity-map.type.d.ts +4 -0
  122. package/dist/modules/state/adaptors/entity/entity-map.type.js +0 -0
  123. package/dist/modules/state/adaptors/entity/entity.interface.d.ts +3 -0
  124. package/dist/modules/state/adaptors/entity/entity.interface.js +0 -0
  125. package/dist/modules/state/state.model.d.ts +46 -0
  126. package/dist/modules/state/state.model.js +129 -0
  127. package/dist/modules/state/update.type.d.ts +2 -0
  128. package/dist/modules/state/update.type.js +0 -0
  129. package/dist/modules/string/string.util.d.ts +11 -0
  130. package/dist/modules/string/string.util.js +42 -0
  131. package/dist/modules/subscribable/subscribable.model.d.ts +10 -0
  132. package/dist/modules/subscribable/subscribable.model.js +34 -0
  133. package/dist/modules/subscribable/subscription.interface.d.ts +3 -0
  134. package/dist/modules/subscribable/subscription.interface.js +0 -0
  135. package/dist/modules/tson/error.d.ts +8 -0
  136. package/dist/modules/tson/error.js +13 -0
  137. package/dist/modules/tson/schema.d.ts +10 -0
  138. package/dist/modules/tson/schema.js +0 -0
  139. package/dist/modules/tson/schemas/any.d.ts +14 -0
  140. package/dist/modules/tson/schemas/any.js +27 -0
  141. package/dist/modules/tson/schemas/array.d.ts +21 -0
  142. package/dist/modules/tson/schemas/array.js +80 -0
  143. package/dist/modules/tson/schemas/boolean.d.ts +14 -0
  144. package/dist/modules/tson/schemas/boolean.js +38 -0
  145. package/dist/modules/tson/schemas/enum.d.ts +15 -0
  146. package/dist/modules/tson/schemas/enum.js +43 -0
  147. package/dist/modules/tson/schemas/number.d.ts +20 -0
  148. package/dist/modules/tson/schemas/number.js +66 -0
  149. package/dist/modules/tson/schemas/object.d.ts +33 -0
  150. package/dist/modules/tson/schemas/object.js +88 -0
  151. package/dist/modules/tson/schemas/schema-base.d.ts +12 -0
  152. package/dist/modules/tson/schemas/schema-base.js +14 -0
  153. package/dist/modules/tson/schemas/string.d.ts +19 -0
  154. package/dist/modules/tson/schemas/string.js +60 -0
  155. package/dist/modules/tson/schemas/union.d.ts +16 -0
  156. package/dist/modules/tson/schemas/union.js +56 -0
  157. package/dist/modules/tson/tson.d.ts +41 -0
  158. package/dist/modules/tson/tson.js +138 -0
  159. package/dist/modules/tson/type.d.ts +53 -0
  160. package/dist/modules/tson/type.js +0 -0
  161. package/dist/modules/value/value.util.d.ts +18 -0
  162. package/dist/modules/value/value.util.js +180 -0
  163. package/dist/types/awaitable.type.d.ts +1 -0
  164. package/dist/types/awaitable.type.js +0 -0
  165. package/dist/types/camel-to-kebab.type.d.ts +1 -0
  166. package/dist/types/camel-to-kebab.type.js +0 -0
  167. package/dist/types/has-key.type.d.ts +3 -0
  168. package/dist/types/has-key.type.js +0 -0
  169. package/dist/types/is-union.type.d.ts +1 -0
  170. package/dist/types/is-union.type.js +0 -0
  171. package/dist/types/keys-of-type.type.d.ts +5 -0
  172. package/dist/types/keys-of-type.type.js +0 -0
  173. package/dist/types/keys-with-fix.type.d.ts +3 -0
  174. package/dist/types/keys-with-fix.type.js +0 -0
  175. package/dist/types/optional-keys.type.d.ts +6 -0
  176. package/dist/types/optional-keys.type.js +0 -0
  177. package/dist/types/partial.type.d.ts +6 -0
  178. package/dist/types/partial.type.js +0 -0
  179. package/dist/types/predicate.type.d.ts +1 -0
  180. package/dist/types/predicate.type.js +0 -0
  181. package/dist/types/recursive-readonly.type.d.ts +3 -0
  182. package/dist/types/recursive-readonly.type.js +0 -0
  183. package/dist/types/recursive-value.type.d.ts +3 -0
  184. package/dist/types/recursive-value.type.js +0 -0
  185. package/dist/types/selector.type.d.ts +1 -0
  186. package/dist/types/selector.type.js +0 -0
  187. package/package.json +1 -1
@@ -0,0 +1,38 @@
1
+ import { TsonError } from '../error';
2
+ import { TsonSchemaBase } from './schema-base';
3
+ export class TsonBoolean extends TsonSchemaBase {
4
+ definition;
5
+ constructor(definition = {
6
+ type: 'boolean',
7
+ }) {
8
+ super();
9
+ this.definition = definition;
10
+ }
11
+ default(value) {
12
+ return new TsonBoolean({
13
+ ...this.definition,
14
+ default: value,
15
+ });
16
+ }
17
+ getDefault() {
18
+ return this.definition.default ?? false;
19
+ }
20
+ accepts(definition) {
21
+ if (definition == null)
22
+ return this.definition.default !== undefined;
23
+ if (definition.type === 'any')
24
+ return true;
25
+ if (definition.type !== 'boolean')
26
+ return false;
27
+ if (this.definition.default === undefined && definition.default !== undefined)
28
+ return false;
29
+ return true;
30
+ }
31
+ parse(value) {
32
+ if (this.parseDefault(value))
33
+ return this.definition.default;
34
+ if (value != null && typeof value !== 'boolean')
35
+ throw new TsonError('Boolean expected', value, this);
36
+ return value;
37
+ }
38
+ }
@@ -0,0 +1,15 @@
1
+ import type { TsonDefinition } from '../schema';
2
+ import { type TsonDefinitionBase, TsonSchemaBase } from './schema-base';
3
+ export interface TsonEnumDefinition<T extends string | number = any> extends TsonDefinitionBase {
4
+ readonly type: 'enum';
5
+ default?: T;
6
+ flags: T[];
7
+ }
8
+ export declare class TsonEnum<T extends string | number = any> extends TsonSchemaBase<T> {
9
+ definition: TsonEnumDefinition<T>;
10
+ constructor(definition?: TsonEnumDefinition<T>);
11
+ default(value: any): TsonEnum;
12
+ getDefault(): T;
13
+ accepts(definition: TsonDefinition | null | undefined): boolean;
14
+ parse(value: any): T;
15
+ }
@@ -0,0 +1,43 @@
1
+ import { TsonError } from '../error';
2
+ import { TsonSchemaBase } from './schema-base';
3
+ export class TsonEnum extends TsonSchemaBase {
4
+ definition;
5
+ constructor(definition = {
6
+ type: 'enum',
7
+ flags: [],
8
+ }) {
9
+ super();
10
+ this.definition = definition;
11
+ }
12
+ default(value) {
13
+ return new TsonEnum({
14
+ ...this.definition,
15
+ default: value,
16
+ });
17
+ }
18
+ getDefault() {
19
+ return this.definition.default ?? this.definition.flags[0];
20
+ }
21
+ accepts(definition) {
22
+ if (definition.type === 'any')
23
+ return true;
24
+ if (definition == null && this.definition.default !== undefined)
25
+ return true;
26
+ if (this.definition.default === undefined && definition.default !== undefined)
27
+ return false;
28
+ if (definition.type !== 'enum')
29
+ return false;
30
+ return this.definition.flags.every((flag) => definition.flags.includes(flag));
31
+ }
32
+ parse(value) {
33
+ if (this.parseDefault(value))
34
+ return this.definition.default;
35
+ if (value != null && typeof this.definition.flags[0] === 'string' && typeof value !== 'string')
36
+ throw new TsonError('String expected', value, this);
37
+ if (value != null && typeof this.definition.flags[0] === 'number' && typeof value !== 'number')
38
+ throw new TsonError('Number expected', value, this);
39
+ if (!this.definition.flags.includes(value))
40
+ throw new TsonError('Invalid value', value, this);
41
+ return value;
42
+ }
43
+ }
@@ -0,0 +1,20 @@
1
+ import type { TsonDefinition } from '../schema';
2
+ import { type TsonDefinitionBase, TsonSchemaBase } from './schema-base';
3
+ export interface TsonNumberDefinition extends TsonDefinitionBase {
4
+ readonly type: 'number';
5
+ min?: number;
6
+ max?: number;
7
+ default?: number;
8
+ integer?: boolean;
9
+ }
10
+ export declare class TsonNumber extends TsonSchemaBase<number> {
11
+ definition: TsonNumberDefinition;
12
+ constructor(definition?: TsonNumberDefinition);
13
+ default(value: any): TsonNumber;
14
+ isInteger(): TsonNumber;
15
+ min(value: number): TsonNumber;
16
+ max(value: number): TsonNumber;
17
+ getDefault(): number;
18
+ accepts(definition: TsonDefinition | null | undefined): boolean;
19
+ parse(value: any): number;
20
+ }
@@ -0,0 +1,66 @@
1
+ import { TsonError } from '../error';
2
+ import { TsonSchemaBase } from './schema-base';
3
+ export class TsonNumber extends TsonSchemaBase {
4
+ definition;
5
+ constructor(definition = {
6
+ type: 'number',
7
+ }) {
8
+ super();
9
+ this.definition = definition;
10
+ }
11
+ default(value) {
12
+ return new TsonNumber({
13
+ ...this.definition,
14
+ default: value,
15
+ });
16
+ }
17
+ isInteger() {
18
+ return new TsonNumber({
19
+ ...this.definition,
20
+ integer: true,
21
+ });
22
+ }
23
+ min(value) {
24
+ return new TsonNumber({
25
+ ...this.definition,
26
+ min: value,
27
+ });
28
+ }
29
+ max(value) {
30
+ return new TsonNumber({
31
+ ...this.definition,
32
+ max: value,
33
+ });
34
+ }
35
+ getDefault() {
36
+ return this.definition.default ?? 0;
37
+ }
38
+ accepts(definition) {
39
+ if (definition == null && this.definition.default !== undefined)
40
+ return true;
41
+ if (definition.type === 'any')
42
+ return true;
43
+ if (this.definition.default === undefined && definition.default !== undefined)
44
+ return false;
45
+ if (definition.type !== 'number')
46
+ return false;
47
+ if (this.definition.min != null && (definition.min == null || definition.min < this.definition.min))
48
+ return false;
49
+ if (this.definition.max != null && (definition.max == null || definition.max > this.definition.max))
50
+ return false;
51
+ if (this.definition.integer != null && this.definition.integer !== definition.integer)
52
+ return false;
53
+ return true;
54
+ }
55
+ parse(value) {
56
+ if (this.parseDefault(value))
57
+ return this.definition.default;
58
+ if (value != null && typeof value !== 'number')
59
+ throw new TsonError('Number expected', value, this);
60
+ if (this.definition.min != null && value < this.definition.min)
61
+ throw new TsonError(`Number should be greater than ${this.definition.min}`, value, this);
62
+ if (this.definition.max != null && value > this.definition.max)
63
+ throw new TsonError(`Number should be less than ${this.definition.max}`, value, this);
64
+ return value;
65
+ }
66
+ }
@@ -0,0 +1,33 @@
1
+ import type { Type } from '../../reflection/types/type.type';
2
+ import type { TsonDefinition } from '../schema';
3
+ import type { TsonType, TypeTson } from '../type';
4
+ import { type TsonDefinitionBase, TsonSchemaBase } from './schema-base';
5
+ export interface TsonObjectDefinition<T extends Record<string, any> = Record<string, any>> extends TsonDefinitionBase {
6
+ readonly type: 'object';
7
+ property?: TsonDefinition;
8
+ properties?: {
9
+ [key in keyof T]: TypeTson<T[key]>;
10
+ };
11
+ prototype?: any;
12
+ default?: T;
13
+ }
14
+ export declare class TsonObject<T extends {
15
+ [key: string]: TsonDefinition;
16
+ } = {}> extends TsonSchemaBase<{
17
+ [P in keyof T]: TsonType<T[P]>;
18
+ }> {
19
+ definition: TsonObjectDefinition<{
20
+ [P in keyof T]: TsonType<T[P]>;
21
+ }>;
22
+ constructor(definition?: TsonObjectDefinition<{
23
+ [P in keyof T]: TsonType<T[P]>;
24
+ }>);
25
+ getDefault(): {
26
+ [P in keyof T]: TsonType<T[P]>;
27
+ };
28
+ prototype(type: Type): TsonObject<T>;
29
+ accepts(definition: TsonDefinition | null | undefined): boolean;
30
+ parse(value: any): {
31
+ [P in keyof T]: TsonType<T[P]>;
32
+ };
33
+ }
@@ -0,0 +1,88 @@
1
+ import { $Reflection } from '../../reflection/utils/reflection.util';
2
+ import { TsonError } from '../error';
3
+ import { $Tson } from '../tson';
4
+ import { TsonSchemaBase } from './schema-base';
5
+ export class TsonObject extends TsonSchemaBase {
6
+ definition;
7
+ constructor(definition = { type: 'object' }) {
8
+ super();
9
+ this.definition = definition;
10
+ }
11
+ getDefault() {
12
+ if (this.definition.default !== undefined)
13
+ return this.definition.default;
14
+ if (this.definition.property)
15
+ return {};
16
+ if (!this.definition.properties)
17
+ return {};
18
+ const result = {};
19
+ for (const property in this.definition.properties) {
20
+ result[property] = $Tson.parse(this.definition.properties[property]).getDefault();
21
+ }
22
+ return result;
23
+ }
24
+ prototype(type) {
25
+ this.definition.prototype = type.prototype;
26
+ return this;
27
+ }
28
+ accepts(definition) {
29
+ if (definition == null && this.definition.default !== undefined)
30
+ return true;
31
+ if (definition.type === 'any')
32
+ return true;
33
+ if (this.definition.default === undefined && definition.default !== undefined)
34
+ return false;
35
+ if (definition.type !== 'object')
36
+ return false;
37
+ if (this.definition.prototype != null && !$Reflection.extends(definition.prototype, this.definition.prototype))
38
+ return false;
39
+ if (this.definition.property) {
40
+ if (!definition.property)
41
+ return false;
42
+ if (!$Tson.parse(this.definition.property).accepts(definition.property))
43
+ return false;
44
+ }
45
+ if (this.definition.properties) {
46
+ for (const property in this.definition.properties) {
47
+ if (!$Tson.parse(this.definition.properties[property]).accepts(definition.properties?.[property]))
48
+ return false;
49
+ }
50
+ }
51
+ return true;
52
+ }
53
+ parse(value) {
54
+ if (this.parseDefault(value))
55
+ return this.definition.default;
56
+ if (value != null && typeof value !== 'object')
57
+ throw new TsonError('Object expected', value, this);
58
+ if (this.definition.property) {
59
+ for (const property in value) {
60
+ try {
61
+ value[property] = $Tson.parse(this.definition.property).parse(value[property]);
62
+ }
63
+ catch (error) {
64
+ if (error instanceof TsonError) {
65
+ error.path = `.${property}${error.path}`;
66
+ }
67
+ throw error;
68
+ }
69
+ }
70
+ }
71
+ if (this.definition.properties) {
72
+ for (const property in this.definition.properties) {
73
+ try {
74
+ value[property] = $Tson.parse(this.definition.properties[property]).parse(value[property]);
75
+ }
76
+ catch (error) {
77
+ if (error instanceof TsonError) {
78
+ error.path = `.${property}${error.path}`;
79
+ }
80
+ throw error;
81
+ }
82
+ }
83
+ }
84
+ if (this.definition.prototype)
85
+ value = Object.setPrototypeOf(value, this.definition.prototype);
86
+ return value;
87
+ }
88
+ }
@@ -0,0 +1,12 @@
1
+ export interface TsonDefinitionBase<T = any> {
2
+ readonly type: string;
3
+ default?: T;
4
+ description?: string;
5
+ }
6
+ export declare abstract class TsonSchemaBase<T = any> {
7
+ definition: TsonDefinitionBase<T>;
8
+ protected parseDefault(value: any): boolean;
9
+ abstract accepts(tson: TsonDefinitionBase | null | undefined): boolean;
10
+ abstract parse(value: any): T;
11
+ abstract getDefault(): T;
12
+ }
@@ -0,0 +1,14 @@
1
+ import { $String } from '../../string/string.util';
2
+ import { TsonError } from '../error';
3
+ export class TsonSchemaBase {
4
+ definition;
5
+ parseDefault(value) {
6
+ if (value == null) {
7
+ if (this.definition.default === undefined)
8
+ throw new TsonError(`${$String.upperCaseFirst(this.definition.type)} required`, value, this);
9
+ else
10
+ return true;
11
+ }
12
+ return false;
13
+ }
14
+ }
@@ -0,0 +1,19 @@
1
+ import type { TsonDefinition } from '../schema';
2
+ import { type TsonDefinitionBase, TsonSchemaBase } from './schema-base';
3
+ export interface TsonStringDefinition extends TsonDefinitionBase {
4
+ readonly type: 'string';
5
+ default?: string;
6
+ min?: number;
7
+ max?: number;
8
+ match?: string;
9
+ }
10
+ export declare class TsonString extends TsonSchemaBase<string> {
11
+ definition: TsonStringDefinition;
12
+ constructor(definition?: TsonStringDefinition);
13
+ match(regex: string): TsonString;
14
+ minLength(length: number): TsonString;
15
+ maxLength(length: number): TsonString;
16
+ getDefault(): string;
17
+ accepts(definition: TsonDefinition | null | undefined): boolean;
18
+ parse(value: any): string;
19
+ }
@@ -0,0 +1,60 @@
1
+ import { TsonError } from '../error';
2
+ import { TsonSchemaBase } from './schema-base';
3
+ export class TsonString extends TsonSchemaBase {
4
+ definition;
5
+ constructor(definition = { type: 'string' }) {
6
+ super();
7
+ this.definition = definition;
8
+ }
9
+ match(regex) {
10
+ return new TsonString({
11
+ ...this.definition,
12
+ match: regex,
13
+ });
14
+ }
15
+ minLength(length) {
16
+ return new TsonString({
17
+ ...this.definition,
18
+ min: length,
19
+ });
20
+ }
21
+ maxLength(length) {
22
+ return new TsonString({
23
+ ...this.definition,
24
+ max: length,
25
+ });
26
+ }
27
+ getDefault() {
28
+ return this.definition.default ?? '';
29
+ }
30
+ accepts(definition) {
31
+ if (definition == null && this.definition.default !== undefined)
32
+ return true;
33
+ if (definition.type === 'any')
34
+ return true;
35
+ if (this.definition.default === undefined && definition.default !== undefined)
36
+ return false;
37
+ if (definition.type !== 'string')
38
+ return false;
39
+ if (this.definition.max != null && (definition.max == null || definition.max > this.definition.max))
40
+ return false;
41
+ if (this.definition.min != null && (definition.min == null || definition.min < this.definition.min))
42
+ return false;
43
+ if (this.definition.match != null && this.definition.match !== definition.match)
44
+ return false;
45
+ return true;
46
+ }
47
+ parse(value) {
48
+ if (this.parseDefault(value))
49
+ return this.definition.default;
50
+ if (value != null && typeof value !== 'string')
51
+ throw new TsonError('String expected', value, this);
52
+ if (this.definition.min != null && value.length < this.definition.min)
53
+ throw new TsonError(`Value min length of ${this.definition.min} expected`, value, this);
54
+ if (this.definition.max != null && value.length > this.definition.max)
55
+ throw new TsonError(`Value max length of ${this.definition.max} expected`, value, this);
56
+ if (this.definition.match != null && !new RegExp(this.definition.match).exec(value))
57
+ throw new TsonError(`Invalid value`, value, this);
58
+ return value;
59
+ }
60
+ }
@@ -0,0 +1,16 @@
1
+ import type { TsonDefinition } from '../schema';
2
+ import type { TypeTson } from '../type';
3
+ import { type TsonDefinitionBase, TsonSchemaBase } from './schema-base';
4
+ export interface TsonUnionDefinition<T extends readonly any[] = any> extends TsonDefinitionBase<T> {
5
+ type: 'union';
6
+ default?: undefined;
7
+ union: readonly TypeTson<T>[];
8
+ }
9
+ export declare class TsonUnion<T = any> extends TsonSchemaBase<T> {
10
+ definition: TsonUnionDefinition;
11
+ constructor(definition?: TsonUnionDefinition);
12
+ default(value: any): TsonUnion;
13
+ getDefault(): undefined;
14
+ accepts(definition: TsonDefinition | null | undefined): boolean;
15
+ parse(value: any): any;
16
+ }
@@ -0,0 +1,56 @@
1
+ import { TsonError } from '../error';
2
+ import { $Tson } from '../tson';
3
+ import { TsonSchemaBase } from './schema-base';
4
+ export class TsonUnion extends TsonSchemaBase {
5
+ definition;
6
+ constructor(definition = {
7
+ type: 'union',
8
+ union: [],
9
+ }) {
10
+ super();
11
+ this.definition = definition;
12
+ }
13
+ default(value) {
14
+ return new TsonUnion({
15
+ ...this.definition,
16
+ default: value,
17
+ });
18
+ }
19
+ getDefault() {
20
+ return this.definition.default;
21
+ }
22
+ accepts(definition) {
23
+ if (definition == null && this.definition.default !== undefined)
24
+ return true;
25
+ if (definition.type === 'any')
26
+ return true;
27
+ if (definition.type !== 'union') {
28
+ for (const union of this.definition.union) {
29
+ if ($Tson.parse(union).accepts(definition))
30
+ return true;
31
+ }
32
+ return false;
33
+ }
34
+ else {
35
+ for (const union2 of definition.union) {
36
+ if (!this.definition.union.some((union) => $Tson.parse(union).accepts(union2)))
37
+ return false;
38
+ }
39
+ return true;
40
+ }
41
+ }
42
+ parse(value) {
43
+ if (this.parseDefault(value))
44
+ return this.definition.default;
45
+ for (const type of this.definition.union) {
46
+ try {
47
+ return $Tson.parse(type).parse(value);
48
+ }
49
+ catch (error) {
50
+ if (!(error instanceof TsonError))
51
+ throw error;
52
+ }
53
+ }
54
+ throw new TsonError(`Union type mismatch`, value, this);
55
+ }
56
+ }
@@ -0,0 +1,41 @@
1
+ import type { TsonDefinition, TsonSchema } from './schema';
2
+ import { type TsonAnyDefinition } from './schemas/any';
3
+ import { type TsonArrayDefinition } from './schemas/array';
4
+ import { type TsonBooleanDefinition } from './schemas/boolean';
5
+ import { type TsonEnumDefinition } from './schemas/enum';
6
+ import { type TsonNumberDefinition } from './schemas/number';
7
+ import { type TsonObjectDefinition } from './schemas/object';
8
+ import { type TsonStringDefinition } from './schemas/string';
9
+ import { type TsonUnionDefinition } from './schemas/union';
10
+ export declare class $Tson {
11
+ static string<T extends Omit<TsonStringDefinition, 'type'>>(definition?: T): T & Pick<TsonStringDefinition, 'type'>;
12
+ static number<T extends Omit<TsonNumberDefinition, 'type'>>(definition?: T): T & Pick<TsonNumberDefinition, 'type'>;
13
+ static boolean<T extends Omit<TsonBooleanDefinition, 'type'>>(definition?: T): T & Pick<TsonBooleanDefinition, 'type'>;
14
+ static array<T extends Omit<TsonArrayDefinition, 'type'>>(definition?: T): T & Pick<TsonArrayDefinition, 'type'>;
15
+ static object<P extends Record<string, TsonDefinition>>(definition: {
16
+ properties: P;
17
+ } & Omit<TsonObjectDefinition<P>, 'type' | 'properties'>): {
18
+ readonly type: 'object';
19
+ properties: P;
20
+ };
21
+ static object<P extends TsonDefinition>(definition: {
22
+ property: P;
23
+ } & Omit<TsonObjectDefinition<P>, 'type' | 'property'>): {
24
+ readonly type: 'object';
25
+ property: P;
26
+ };
27
+ static object<T>(definition?: Omit<TsonObjectDefinition<T>, 'type'>): TsonObjectDefinition<T>;
28
+ static any(definition?: Omit<TsonAnyDefinition, 'type'>): TsonAnyDefinition;
29
+ static enum<T extends string | number = any>(definition?: Omit<TsonEnumDefinition<T>, 'type'>): TsonEnumDefinition<T>;
30
+ static const<T extends string | number>(...flags: T[]): TsonEnumDefinition<T>;
31
+ static union<T extends readonly TsonDefinition[]>(definition: {
32
+ union: T;
33
+ } & Omit<TsonUnionDefinition<T>, 'union' | 'type'>): {
34
+ type: 'union';
35
+ union: T;
36
+ };
37
+ static fromEnum<T extends object>(flags: T): TsonEnumDefinition<T[keyof T] extends string ? T[keyof T] & string : T[keyof T] & number>;
38
+ static parse(definition: TsonDefinition): TsonSchema;
39
+ static generateTypeScriptDefinition(definition: TsonDefinition): string;
40
+ static generate(value: any): TsonDefinition;
41
+ }
@@ -0,0 +1,138 @@
1
+ import { $Enum } from '../enum/enum.util';
2
+ import { $Reflection } from '../reflection/utils/reflection.util';
3
+ import { $String } from '../string/string.util';
4
+ import { TsonAny } from './schemas/any';
5
+ import { TsonArray } from './schemas/array';
6
+ import { TsonBoolean } from './schemas/boolean';
7
+ import { TsonEnum } from './schemas/enum';
8
+ import { TsonNumber } from './schemas/number';
9
+ import { TsonObject } from './schemas/object';
10
+ import { TsonString } from './schemas/string';
11
+ import { TsonUnion } from './schemas/union';
12
+ export class $Tson {
13
+ static string(definition) {
14
+ return { type: 'string', ...(definition ?? {}) };
15
+ }
16
+ static number(definition) {
17
+ return { type: 'number', ...(definition ?? {}) };
18
+ }
19
+ static boolean(definition) {
20
+ return { type: 'boolean', ...(definition ?? {}) };
21
+ }
22
+ static array(definition) {
23
+ return { type: 'array', ...(definition ?? {}) };
24
+ }
25
+ static object(definition) {
26
+ return { type: 'object', ...(definition ?? {}) };
27
+ }
28
+ static any(definition) {
29
+ return { type: 'any', ...(definition ?? {}) };
30
+ }
31
+ static enum(definition) {
32
+ return { type: 'enum', flags: [], ...(definition ?? {}) };
33
+ }
34
+ static const(...flags) {
35
+ return {
36
+ type: 'enum',
37
+ flags
38
+ };
39
+ }
40
+ static union(definition) {
41
+ return {
42
+ type: 'union',
43
+ union: [],
44
+ ...(definition ?? {}),
45
+ };
46
+ }
47
+ static fromEnum(flags) {
48
+ return {
49
+ type: 'enum',
50
+ flags: $Enum.getValues(flags),
51
+ };
52
+ }
53
+ static parse(definition) {
54
+ switch (definition.type) {
55
+ case 'any':
56
+ return new TsonAny(definition);
57
+ case 'string':
58
+ return new TsonString(definition);
59
+ case 'number':
60
+ return new TsonNumber(definition);
61
+ case 'boolean':
62
+ return new TsonBoolean(definition);
63
+ case 'object':
64
+ return new TsonObject(definition);
65
+ case 'array':
66
+ return new TsonArray(definition);
67
+ case 'enum':
68
+ return new TsonEnum(definition);
69
+ case 'union':
70
+ return new TsonUnion(definition);
71
+ }
72
+ }
73
+ static generateTypeScriptDefinition(definition) {
74
+ if (definition == null)
75
+ return 'any';
76
+ switch (definition.type) {
77
+ case 'any':
78
+ return 'any';
79
+ case 'string':
80
+ return 'string';
81
+ case 'number':
82
+ return 'number';
83
+ case 'boolean':
84
+ return 'boolean';
85
+ case 'object':
86
+ if (definition.property) {
87
+ let result = '';
88
+ if (definition.description)
89
+ result += `// ${definition.property.description}\n`;
90
+ result += `[key: string]: ${this.generateTypeScriptDefinition(definition.property)}`;
91
+ return `{\n${$String.indent(result, 4)}\n}`;
92
+ }
93
+ else if (definition.properties) {
94
+ const result = Object
95
+ .entries(definition.properties)
96
+ .map(([property, value]) => {
97
+ let result = '';
98
+ if (value.description)
99
+ result += `// ${value.description}\n`;
100
+ result += `'${property}'${'default' in value ? '?' : ''}: ${this.generateTypeScriptDefinition(value)}`;
101
+ return result;
102
+ })
103
+ .join(',\n');
104
+ return `{\n${$String.indent(result, 4)}\n}`;
105
+ }
106
+ else
107
+ return 'Record<string, any>';
108
+ case 'array':
109
+ return `${this.generateTypeScriptDefinition(definition.itemDefinition)}[]`;
110
+ case 'enum':
111
+ return `${definition.flags.map(flag => typeof flag === 'string' ? `'${flag}'` : flag).join(' | ')}`;
112
+ case 'union':
113
+ return `${definition.union.map(definition => this.generateTypeScriptDefinition(definition)).join(' | ')}`;
114
+ }
115
+ }
116
+ static generate(value) {
117
+ if (typeof value === 'string')
118
+ return $Tson.string();
119
+ if (typeof value === 'number')
120
+ return $Tson.number();
121
+ if (typeof value === 'boolean')
122
+ return $Tson.boolean();
123
+ if ($Enum.isEnum(value))
124
+ return $Tson.enum(value);
125
+ if ($Reflection.isArray(value))
126
+ return $Tson.array({
127
+ itemDefinition: $Tson.generate(value[0]) ?? $Tson.any(),
128
+ });
129
+ if ($Reflection.isObject(value))
130
+ return $Tson.object({
131
+ properties: Object.keys(value).reduce((properties, key) => ({
132
+ ...properties,
133
+ [key]: $Tson.generate(value[key]),
134
+ }), {}),
135
+ });
136
+ return $Tson.any();
137
+ }
138
+ }