@wener/utils 1.1.47 → 1.1.49

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 (194) hide show
  1. package/README.md +9 -1
  2. package/lib/asyncs/Promises.js +54 -0
  3. package/lib/asyncs/Promises.js.map +1 -0
  4. package/lib/asyncs/createLazyPromise.js +52 -51
  5. package/lib/asyncs/createLazyPromise.js.map +1 -1
  6. package/lib/asyncs/firstOfAsyncIterator.js.map +1 -1
  7. package/lib/asyncs/isPromise.js +2 -3
  8. package/lib/asyncs/isPromise.js.map +1 -1
  9. package/lib/asyncs/nextOfAsyncIterator.js.map +1 -1
  10. package/lib/asyncs/timeout.js +4 -4
  11. package/lib/asyncs/timeout.js.map +1 -1
  12. package/lib/cn/division/DivisionCode.js +30 -17
  13. package/lib/cn/division/DivisionCode.js.map +1 -1
  14. package/lib/cn/id/{Mod11Checksum.js → Mod11.js} +3 -6
  15. package/lib/cn/id/Mod11.js.map +1 -0
  16. package/lib/cn/id/ResidentIdNumber.js +10 -12
  17. package/lib/cn/id/ResidentIdNumber.js.map +1 -1
  18. package/lib/cn/types.js +3 -0
  19. package/lib/cn/types.js.map +1 -0
  20. package/lib/cn/uscc/{Mod31Checksum.js → Mod31.js} +3 -6
  21. package/lib/cn/uscc/Mod31.js.map +1 -0
  22. package/lib/cn/uscc/USCC.js +6 -9
  23. package/lib/cn/uscc/USCC.js.map +1 -1
  24. package/lib/cn/uscc/isUSCC.js +1 -1
  25. package/lib/cn/uscc/isUSCC.js.map +1 -1
  26. package/lib/crypto/getNodeCrypto.js +14 -10
  27. package/lib/crypto/getNodeCrypto.js.map +1 -1
  28. package/lib/crypto/getRandomValues.js +5 -1
  29. package/lib/crypto/getRandomValues.js.map +1 -1
  30. package/lib/emitter/types.js +3 -0
  31. package/lib/emitter/types.js.map +1 -0
  32. package/lib/fetch/createFetchWith.js.map +1 -1
  33. package/lib/fetch/createFetchWithRetry.js.map +1 -1
  34. package/lib/fetch/http.types.js +4 -0
  35. package/lib/fetch/http.types.js.map +1 -0
  36. package/lib/index.js +6 -3
  37. package/lib/index.js.map +1 -1
  38. package/lib/io/ArrayBuffers.js.map +1 -1
  39. package/lib/io/ByteBuffer.js +235 -36
  40. package/lib/io/ByteBuffer.js.map +1 -1
  41. package/lib/langs/getGlobalStates.js +17 -0
  42. package/lib/langs/getGlobalStates.js.map +1 -0
  43. package/lib/langs/getObjectId.js +18 -0
  44. package/lib/langs/getObjectId.js.map +1 -0
  45. package/lib/langs/isNullish.js +5 -0
  46. package/lib/langs/isNullish.js.map +1 -0
  47. package/lib/logging/slog.js.map +1 -1
  48. package/lib/maths/clamp.js +7 -6
  49. package/lib/maths/clamp.js.map +1 -1
  50. package/lib/maths/random.js.map +1 -1
  51. package/lib/mitt/index.js +77 -0
  52. package/lib/mitt/index.js.map +1 -0
  53. package/lib/objects/computeIfAbsent.js +1 -1
  54. package/lib/objects/computeIfAbsent.js.map +1 -1
  55. package/lib/schema/typebox/typebox.js.map +1 -1
  56. package/lib/server/fetch/createFetchWithProxyByUndici.js.map +1 -1
  57. package/package.json +6 -14
  58. package/src/asyncs/Promises.ts +62 -0
  59. package/src/asyncs/createLazyPromise.test.ts +52 -13
  60. package/src/asyncs/createLazyPromise.ts +66 -61
  61. package/src/asyncs/firstOfAsyncIterator.ts +1 -1
  62. package/src/asyncs/isPromise.ts +3 -3
  63. package/src/asyncs/nextOfAsyncIterator.ts +1 -1
  64. package/src/asyncs/timeout.ts +4 -4
  65. package/src/cn/division/DivisionCode.ts +61 -8
  66. package/src/cn/division/division.test.ts +6 -0
  67. package/src/cn/id/{Mod11Checksum.ts → Mod11.ts} +3 -6
  68. package/src/cn/id/ResidentIdNumber.ts +11 -13
  69. package/src/cn/id/id.test.ts +5 -5
  70. package/src/cn/scripts/gen.test.ts +2 -1
  71. package/src/cn/types.ts +11 -0
  72. package/src/cn/uscc/{Mod31Checksum.ts → Mod31.ts} +3 -7
  73. package/src/cn/uscc/USCC.ts +7 -11
  74. package/src/cn/uscc/isUSCC.ts +1 -1
  75. package/src/cn/uscc/uscc.test.ts +4 -4
  76. package/src/crypto/getNodeCrypto.ts +16 -11
  77. package/src/crypto/getRandomValues.ts +6 -1
  78. package/src/emitter/types.ts +18 -0
  79. package/src/fetch/createFetchWith.ts +1 -1
  80. package/src/fetch/createFetchWithRetry.ts +1 -1
  81. package/src/fetch/http.types.ts +46 -0
  82. package/src/index.ts +7 -3
  83. package/src/io/ArrayBuffers.ts +7 -3
  84. package/src/io/ByteBuffer.test.ts +33 -6
  85. package/src/io/ByteBuffer.ts +272 -41
  86. package/src/langs/getGlobalStates.ts +21 -0
  87. package/src/langs/getObjectId.ts +20 -0
  88. package/src/langs/isNullish.ts +3 -0
  89. package/src/langs/mixin.test.ts +43 -0
  90. package/src/logging/slog.ts +1 -1
  91. package/src/maths/clamp.test.ts +5 -1
  92. package/src/maths/clamp.ts +7 -7
  93. package/src/maths/random.ts +1 -1
  94. package/src/mitt/README.md +1 -0
  95. package/src/mitt/index.ts +114 -0
  96. package/src/objects/computeIfAbsent.ts +3 -2
  97. package/src/objects/merge/merge.test.ts +1 -1
  98. package/src/schema/typebox/typebox.ts +1 -1
  99. package/src/server/fetch/createFetchWithProxyByUndici.ts +2 -2
  100. package/tsconfig.json +4 -1
  101. package/lib/asyncs/sleep.js +0 -3
  102. package/lib/asyncs/sleep.js.map +0 -1
  103. package/lib/cn/id/Mod11Checksum.js.map +0 -1
  104. package/lib/cn/uscc/Mod31Checksum.js.map +0 -1
  105. package/lib/schema/typebox/gen/codegen/common/encoder.js +0 -94
  106. package/lib/schema/typebox/gen/codegen/common/encoder.js.map +0 -1
  107. package/lib/schema/typebox/gen/codegen/common/formatter.js +0 -33
  108. package/lib/schema/typebox/gen/codegen/common/formatter.js.map +0 -1
  109. package/lib/schema/typebox/gen/codegen/common/index.js +0 -29
  110. package/lib/schema/typebox/gen/codegen/common/index.js.map +0 -1
  111. package/lib/schema/typebox/gen/codegen/common/jsdoc.js +0 -117
  112. package/lib/schema/typebox/gen/codegen/common/jsdoc.js.map +0 -1
  113. package/lib/schema/typebox/gen/codegen/expression/compiler.js +0 -364
  114. package/lib/schema/typebox/gen/codegen/expression/compiler.js.map +0 -1
  115. package/lib/schema/typebox/gen/codegen/expression/errors.js +0 -259
  116. package/lib/schema/typebox/gen/codegen/expression/errors.js.map +0 -1
  117. package/lib/schema/typebox/gen/codegen/expression/evaluator.js +0 -254
  118. package/lib/schema/typebox/gen/codegen/expression/evaluator.js.map +0 -1
  119. package/lib/schema/typebox/gen/codegen/expression/expression.js +0 -381
  120. package/lib/schema/typebox/gen/codegen/expression/expression.js.map +0 -1
  121. package/lib/schema/typebox/gen/codegen/expression/index.js +0 -32
  122. package/lib/schema/typebox/gen/codegen/expression/index.js.map +0 -1
  123. package/lib/schema/typebox/gen/codegen/index.js +0 -29
  124. package/lib/schema/typebox/gen/codegen/index.js.map +0 -1
  125. package/lib/schema/typebox/gen/codegen/model/index.js +0 -40
  126. package/lib/schema/typebox/gen/codegen/model/index.js.map +0 -1
  127. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js +0 -260
  128. package/lib/schema/typebox/gen/codegen/model/model-to-arktype.js.map +0 -1
  129. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js +0 -383
  130. package/lib/schema/typebox/gen/codegen/model/model-to-expression.js.map +0 -1
  131. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js +0 -238
  132. package/lib/schema/typebox/gen/codegen/model/model-to-grpc.js.map +0 -1
  133. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js +0 -274
  134. package/lib/schema/typebox/gen/codegen/model/model-to-io-ts.js.map +0 -1
  135. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js +0 -47
  136. package/lib/schema/typebox/gen/codegen/model/model-to-javascript.js.map +0 -1
  137. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js +0 -192
  138. package/lib/schema/typebox/gen/codegen/model/model-to-json-schema.js.map +0 -1
  139. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js +0 -33
  140. package/lib/schema/typebox/gen/codegen/model/model-to-typebox.js.map +0 -1
  141. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js +0 -188
  142. package/lib/schema/typebox/gen/codegen/model/model-to-typescript.js.map +0 -1
  143. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js +0 -239
  144. package/lib/schema/typebox/gen/codegen/model/model-to-valibot.js.map +0 -1
  145. package/lib/schema/typebox/gen/codegen/model/model-to-value.js +0 -43
  146. package/lib/schema/typebox/gen/codegen/model/model-to-value.js.map +0 -1
  147. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js +0 -227
  148. package/lib/schema/typebox/gen/codegen/model/model-to-yrel.js.map +0 -1
  149. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js +0 -225
  150. package/lib/schema/typebox/gen/codegen/model/model-to-yup.js.map +0 -1
  151. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js +0 -248
  152. package/lib/schema/typebox/gen/codegen/model/model-to-zod.js.map +0 -1
  153. package/lib/schema/typebox/gen/codegen/model/model.js +0 -27
  154. package/lib/schema/typebox/gen/codegen/model/model.js.map +0 -1
  155. package/lib/schema/typebox/gen/codegen/typescript/index.js +0 -28
  156. package/lib/schema/typebox/gen/codegen/typescript/index.js.map +0 -1
  157. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js +0 -72
  158. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-model.js.map +0 -1
  159. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js +0 -620
  160. package/lib/schema/typebox/gen/codegen/typescript/typescript-to-typebox.js.map +0 -1
  161. package/lib/schema/typebox/gen/index.js +0 -3
  162. package/lib/schema/typebox/gen/index.js.map +0 -1
  163. package/src/asyncs/sleep.ts +0 -1
  164. package/src/schema/typebox/gen/codegen/common/encoder.ts +0 -99
  165. package/src/schema/typebox/gen/codegen/common/formatter.ts +0 -31
  166. package/src/schema/typebox/gen/codegen/common/index.ts +0 -29
  167. package/src/schema/typebox/gen/codegen/common/jsdoc.ts +0 -93
  168. package/src/schema/typebox/gen/codegen/expression/compiler.ts +0 -377
  169. package/src/schema/typebox/gen/codegen/expression/errors.ts +0 -302
  170. package/src/schema/typebox/gen/codegen/expression/evaluator.ts +0 -268
  171. package/src/schema/typebox/gen/codegen/expression/expression.ts +0 -538
  172. package/src/schema/typebox/gen/codegen/expression/index.ts +0 -32
  173. package/src/schema/typebox/gen/codegen/index.ts +0 -29
  174. package/src/schema/typebox/gen/codegen/model/index.ts +0 -40
  175. package/src/schema/typebox/gen/codegen/model/model-to-arktype.ts +0 -266
  176. package/src/schema/typebox/gen/codegen/model/model-to-expression.ts +0 -378
  177. package/src/schema/typebox/gen/codegen/model/model-to-grpc.ts +0 -244
  178. package/src/schema/typebox/gen/codegen/model/model-to-io-ts.ts +0 -294
  179. package/src/schema/typebox/gen/codegen/model/model-to-javascript.ts +0 -42
  180. package/src/schema/typebox/gen/codegen/model/model-to-json-schema.ts +0 -166
  181. package/src/schema/typebox/gen/codegen/model/model-to-typebox.ts +0 -32
  182. package/src/schema/typebox/gen/codegen/model/model-to-typescript.ts +0 -189
  183. package/src/schema/typebox/gen/codegen/model/model-to-valibot.ts +0 -236
  184. package/src/schema/typebox/gen/codegen/model/model-to-value.ts +0 -42
  185. package/src/schema/typebox/gen/codegen/model/model-to-yrel.ts +0 -232
  186. package/src/schema/typebox/gen/codegen/model/model-to-yup.ts +0 -226
  187. package/src/schema/typebox/gen/codegen/model/model-to-zod.ts +0 -251
  188. package/src/schema/typebox/gen/codegen/model/model.ts +0 -33
  189. package/src/schema/typebox/gen/codegen/typescript/index.ts +0 -28
  190. package/src/schema/typebox/gen/codegen/typescript/typescript-to-model.ts +0 -61
  191. package/src/schema/typebox/gen/codegen/typescript/typescript-to-typebox.ts +0 -647
  192. package/src/schema/typebox/gen/gen.test.ts +0 -12
  193. package/src/schema/typebox/gen/index.ts +0 -1
  194. /package/src/schema/{typebox/gen/README.md → README.md} +0 -0
@@ -1,166 +0,0 @@
1
- /*--------------------------------------------------------------------------
2
-
3
- @sinclair/typebox-codegen
4
-
5
- The MIT License (MIT)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
-
25
- ---------------------------------------------------------------------------*/
26
- import * as Types from '@sinclair/typebox';
27
- import { Formatter } from '../common/index';
28
- import { TypeBoxModel } from './model';
29
-
30
- // --------------------------------------------------------------------------
31
- // ModelToJsonSchema
32
- // --------------------------------------------------------------------------
33
- export namespace ModelToJsonSchema {
34
- function Any(schema: Types.TAny): Types.TSchema {
35
- return schema;
36
- }
37
- function Array(schema: Types.TArray): Types.TSchema {
38
- return schema;
39
- }
40
- function BigInt(schema: Types.TBigInt): Types.TSchema {
41
- return schema;
42
- }
43
- function Boolean(schema: Types.TBoolean): Types.TSchema {
44
- return schema;
45
- }
46
- function Date(schema: Types.TDate): Types.TSchema {
47
- return schema;
48
- }
49
- function Constructor(schema: Types.TConstructor): Types.TSchema {
50
- return schema;
51
- }
52
- function Function(schema: Types.TFunction): Types.TSchema {
53
- const parameters = schema.parameters.map((schema) => Visit(schema));
54
- const returns = Visit(schema.returns);
55
- return { ...schema, parameters, returns };
56
- }
57
- function Integer(schema: Types.TInteger): Types.TSchema {
58
- return schema;
59
- }
60
- function Intersect(schema: Types.TIntersect): Types.TSchema {
61
- const allOf = schema.allOf.map((schema) => Visit(schema));
62
- return { ...schema, allOf };
63
- }
64
- function Literal(schema: Types.TLiteral): Types.TSchema {
65
- return schema;
66
- }
67
- function Never(schema: Types.TNever): Types.TSchema {
68
- return schema;
69
- }
70
- function Null(schema: Types.TNull): Types.TSchema {
71
- return schema;
72
- }
73
- function String(schema: Types.TString): Types.TSchema {
74
- return schema;
75
- }
76
- function Number(schema: Types.TNumber): Types.TSchema {
77
- return schema;
78
- }
79
- function Object(schema: Types.TObject): Types.TSchema {
80
- const properties = globalThis.Object.keys(schema.properties).reduce((acc, key) => {
81
- return { ...acc, [key]: Visit(schema.properties[key]) };
82
- }, {});
83
- return { ...schema, properties };
84
- }
85
- function Promise(schema: Types.TPromise): Types.TSchema {
86
- const item = Visit(schema.item);
87
- return { ...schema, item };
88
- }
89
- function Record(schema: Types.TRecord): Types.TSchema {
90
- const patternProperties = globalThis.Object.keys(schema.patternProperties).reduce((acc, key) => {
91
- return { ...acc, [key]: Visit(schema.patternProperties[key]) };
92
- }, {});
93
- return { ...schema, patternProperties };
94
- }
95
- function Ref(schema: Types.TRef): Types.TSchema {
96
- return schema;
97
- }
98
- function This(schema: Types.TThis): Types.TSchema {
99
- return schema;
100
- }
101
- function Tuple(schema: Types.TTuple): Types.TSchema {
102
- if (schema.items === undefined) return schema;
103
- const items = schema.items.map((schema) => Visit(schema));
104
- return { ...schema, items };
105
- }
106
- function TemplateLiteral(schema: Types.TTemplateLiteral): Types.TSchema {
107
- return schema;
108
- }
109
- function UInt8Array(schema: Types.TUint8Array): Types.TSchema {
110
- return schema;
111
- }
112
- function Undefined(schema: Types.TUndefined): Types.TSchema {
113
- return schema;
114
- }
115
- function Union(schema: Types.TUnion): Types.TSchema {
116
- const anyOf = schema.anyOf.map((schema) => Visit(schema));
117
- return { ...schema, anyOf };
118
- }
119
- function Unknown(schema: Types.TUnknown): Types.TSchema {
120
- return schema;
121
- }
122
- function Void(schema: Types.TVoid) {
123
- return schema;
124
- }
125
- function UnsupportedType(schema: Types.TSchema) {
126
- return schema;
127
- }
128
- function Visit(schema: Types.TSchema): Types.TSchema {
129
- if (Types.TypeGuard.IsAny(schema)) return Any(schema);
130
- if (Types.TypeGuard.IsArray(schema)) return Array(schema);
131
- if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
132
- if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
133
- if (Types.TypeGuard.IsDate(schema)) return Date(schema);
134
- if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
135
- if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
136
- if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
137
- if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
138
- if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
139
- if (Types.TypeGuard.IsNever(schema)) return Never(schema);
140
- if (Types.TypeGuard.IsNull(schema)) return Null(schema);
141
- if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
142
- if (Types.TypeGuard.IsObject(schema)) return Object(schema);
143
- if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
144
- if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
145
- if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
146
- if (Types.TypeGuard.IsString(schema)) return String(schema);
147
- if (Types.TypeGuard.IsTemplateLiteral(schema)) return TemplateLiteral(schema);
148
- if (Types.TypeGuard.IsThis(schema)) return This(schema);
149
- if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
150
- if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
151
- if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
152
- if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
153
- if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
154
- if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
155
- return UnsupportedType(schema);
156
- }
157
- export function Generate(model: TypeBoxModel): string {
158
- const buffer: string[] = [];
159
- for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
160
- const schema = Visit(type);
161
- const encode = JSON.stringify(schema, null, 2);
162
- buffer.push(`export const ${type.$id} = ${encode}`);
163
- }
164
- return Formatter.Format(buffer.join('\n'));
165
- }
166
- }
@@ -1,32 +0,0 @@
1
- /*--------------------------------------------------------------------------
2
-
3
- @sinclair/typebox-codegen
4
-
5
- The MIT License (MIT)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
-
25
- ---------------------------------------------------------------------------*/
26
- import { TypeBoxModel } from './model';
27
-
28
- export namespace ModelToTypeBox {
29
- export function Generate(model: TypeBoxModel) {
30
- return '// todo';
31
- }
32
- }
@@ -1,189 +0,0 @@
1
- /*--------------------------------------------------------------------------
2
-
3
- @sinclair/typebox-codegen
4
-
5
- The MIT License (MIT)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
-
25
- ---------------------------------------------------------------------------*/
26
- import * as Types from '@sinclair/typebox';
27
- import { TypeCompiler } from '@sinclair/typebox/compiler';
28
- import { Formatter } from '../common/formatter';
29
- import { TypeBoxModel } from './model';
30
-
31
- export namespace ModelToTypeScript {
32
- function Any(schema: Types.TAny) {
33
- return 'any';
34
- }
35
- function Array(schema: Types.TArray) {
36
- const items = Visit(schema.items);
37
- return `Array<${items}>`;
38
- }
39
- function Boolean(schema: Types.TBoolean) {
40
- return 'boolean';
41
- }
42
- function BigInt(schema: Types.TBigInt) {
43
- return 'bigint';
44
- }
45
- function Constructor(schema: Types.TConstructor) {
46
- const params = schema.parameters.map((param) => Visit(param)).join(', ');
47
- const returns = Visit(schema.returns);
48
- return `(new (${params}) => ${returns})`;
49
- }
50
- function Date(schema: Types.TDate) {
51
- return 'Date';
52
- }
53
- function Function(schema: Types.TFunction) {
54
- const params = schema.parameters.map((param) => Visit(param)).join(', ');
55
- const returns = Visit(schema.returns);
56
- return `((${params}) => ${returns})`;
57
- }
58
- function Integer(schema: Types.TInteger) {
59
- return 'number';
60
- }
61
- function Intersect(schema: Types.TIntersect) {
62
- return schema.allOf.map((schema) => Visit(schema)).join(' & ');
63
- }
64
- function Literal(schema: Types.TLiteral) {
65
- if (typeof schema.const === 'string') {
66
- return `'${schema.const}'`;
67
- } else {
68
- return `${schema.const}`;
69
- }
70
- }
71
- function Never(schema: Types.TNever) {
72
- return 'never';
73
- }
74
- function Null(schema: Types.TNull) {
75
- return 'null';
76
- }
77
- function String(schema: Types.TString) {
78
- return 'string';
79
- }
80
- function Number(schema: Types.TNumber) {
81
- return 'number';
82
- }
83
- function Object(schema: Types.TObject) {
84
- const properties: string = globalThis.Object.entries(schema.properties)
85
- .map(([key, value]) => {
86
- return `${key}: ${Visit(value)}`;
87
- })
88
- .join(',\n');
89
- return `{\n${properties}\n}`;
90
- }
91
- function Promise(schema: Types.TPromise) {
92
- const item = Visit(schema.item);
93
- return `Promise<${item}>`;
94
- }
95
- function Record(schema: Types.TRecord) {
96
- for (const [key, value] of globalThis.Object.entries(schema.patternProperties)) {
97
- const type = Visit(value);
98
- if (key === '^(0|[1-9][0-9]*)$') {
99
- return `Record<number, ${type}>`;
100
- } else {
101
- return `Record<string, ${type}>`;
102
- }
103
- }
104
- throw Error('TypeBoxToTypeScript: Unreachable');
105
- }
106
- function Ref(schema: Types.TRef) {
107
- return schema.$ref;
108
- }
109
- function This(schema: Types.TThis) {
110
- return schema.$ref;
111
- }
112
- function Tuple(schema: Types.TTuple) {
113
- if (schema.items === undefined) return `[]`;
114
- const items = schema.items.map((schema) => Visit(schema)).join(', ');
115
- return `[${items}]`;
116
- }
117
- function UInt8Array(schema: Types.TUint8Array) {
118
- return `Uint8Array`;
119
- }
120
- function Undefined(schema: Types.TUndefined) {
121
- return `undefined`;
122
- }
123
- function Union(schema: Types.TUnion) {
124
- return schema.anyOf.map((schema) => Visit(schema)).join(' | ');
125
- }
126
- function Unknown(schema: Types.TUnknown) {
127
- return `unknown`;
128
- }
129
- function Void(schema: Types.TVoid) {
130
- return `void`;
131
- }
132
- function Visit(schema: Types.TSchema): string {
133
- if (reference_map.has(schema.$id!)) return schema.$id!;
134
- if (schema.$id !== undefined) reference_map.set(schema.$id, schema);
135
- if (Types.TypeGuard.IsAny(schema)) return Any(schema);
136
- if (Types.TypeGuard.IsArray(schema)) return Array(schema);
137
- if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
138
- if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
139
- if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
140
- if (Types.TypeGuard.IsDate(schema)) return Date(schema);
141
- if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
142
- if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
143
- if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
144
- if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
145
- if (Types.TypeGuard.IsNever(schema)) return Never(schema);
146
- if (Types.TypeGuard.IsNull(schema)) return Null(schema);
147
- if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
148
- if (Types.TypeGuard.IsObject(schema)) return Object(schema);
149
- if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
150
- if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
151
- if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
152
- if (Types.TypeGuard.IsThis(schema)) return This(schema);
153
- if (Types.TypeGuard.IsString(schema)) return String(schema);
154
- if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
155
- if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
156
- if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
157
- if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
158
- if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
159
- if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
160
- return 'unknown';
161
- }
162
- export function GenerateType(model: TypeBoxModel, $id: string) {
163
- reference_map.clear();
164
- const type = model.types.find((type) => type.$id === $id);
165
- if (type === undefined) return `export type ${$id} = unknown`;
166
- return `export type ${type.$id!} = ${Visit(type)}`;
167
- }
168
- const reference_map = new Map<string, Types.TSchema>();
169
- export function Generate(model: TypeBoxModel): string {
170
- reference_map.clear();
171
- const definitions: string[] = [];
172
- for (const type of model.types) {
173
- const definition = `export type ${type.$id!} = ${Visit(type)}`;
174
- const assertion = `export const ${type.$id!} = (() => { ${TypeCompiler.Code(type, model.types, {
175
- language: 'typescript',
176
- })} })();`;
177
- const rewritten = assertion.replaceAll(
178
- `return function check(value: any): boolean`,
179
- `return function check(value: any): value is ${type.$id!}`,
180
- );
181
- definitions.push(`
182
- ${definition}
183
- ${rewritten}
184
- `);
185
- }
186
- const output = [...definitions];
187
- return Formatter.Format(output.join('\n\n'));
188
- }
189
- }
@@ -1,236 +0,0 @@
1
- /*--------------------------------------------------------------------------
2
-
3
- @sinclair/typebox-codegen
4
-
5
- The MIT License (MIT)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
-
25
- ---------------------------------------------------------------------------*/
26
- import * as Types from '@sinclair/typebox';
27
- import { Formatter, PropertyEncoder } from '../common/index';
28
- import { TypeBoxModel } from './model';
29
- import { ModelToTypeScript } from './model-to-typescript';
30
-
31
- // --------------------------------------------------------------------------
32
- // ModelToValibot
33
- // --------------------------------------------------------------------------
34
- export namespace ModelToValibot {
35
- function IsDefined<T = any>(value: unknown): value is T {
36
- return value !== undefined;
37
- }
38
- function Type(type: string, parameter: string | null, constraints: string[]) {
39
- if (constraints.length > 0) {
40
- if (typeof parameter === 'string') {
41
- return `${type}(${parameter}, [${constraints.join(', ')}])`;
42
- } else {
43
- return `${type}([${constraints.join(', ')}])`;
44
- }
45
- } else {
46
- if (typeof parameter === 'string') {
47
- return `${type}(${parameter})`;
48
- } else {
49
- return `${type}()`;
50
- }
51
- }
52
- }
53
- function Any(schema: Types.TAny) {
54
- return Type(`v.any`, null, []);
55
- }
56
- function Array(schema: Types.TArray) {
57
- const items = Visit(schema.items);
58
- const constraints: string[] = [];
59
- if (IsDefined<number>(schema.minItems)) constraints.push(`v.minLength(${schema.minItems})`);
60
- if (IsDefined<number>(schema.maxItems)) constraints.push(`v.maxLength(${schema.maxItems})`);
61
- return Type(`v.array`, items, constraints);
62
- }
63
- function BigInt(schema: Types.TBigInt) {
64
- return Type(`v.bigint`, null, []);
65
- }
66
- function Boolean(schema: Types.TBoolean) {
67
- return Type(`v.boolean`, null, []);
68
- }
69
- function Date(schema: Types.TDate) {
70
- return Type(`v.date`, null, []);
71
- }
72
- function Constructor(schema: Types.TConstructor): string {
73
- return UnsupportedType(schema);
74
- }
75
- function Function(schema: Types.TFunction) {
76
- return UnsupportedType(schema);
77
- }
78
- function Integer(schema: Types.TInteger) {
79
- return Type(`v.number`, null, [`v.integer()`]);
80
- }
81
- function Intersect(schema: Types.TIntersect) {
82
- const inner = schema.allOf.map((inner) => Visit(inner));
83
- return Type(`v.merge`, `[${inner.join(', ')}]`, []);
84
- }
85
- function Literal(schema: Types.TLiteral) {
86
- // prettier-ignore
87
- return typeof schema.const === `string`
88
- ? Type(`v.literal`, `'${schema.const}'`, [])
89
- : Type(`v.literal`, `${schema.const}`, [])
90
- }
91
- function Never(schema: Types.TNever) {
92
- return Type(`v.never`, null, []);
93
- }
94
- function Null(schema: Types.TNull) {
95
- return Type(`v.nullType`, null, []);
96
- }
97
- function String(schema: Types.TString) {
98
- const constraints: string[] = [];
99
- if (IsDefined<number>(schema.maxLength)) constraints.push(`v.maxLength(${schema.maxLength})`);
100
- if (IsDefined<number>(schema.minLength)) constraints.push(`v.minLength(${schema.minLength})`);
101
- return Type(`v.string`, null, constraints);
102
- }
103
- function Number(schema: Types.TNumber) {
104
- const constraints: string[] = [];
105
- if (IsDefined<number>(schema.minimum)) constraints.push(`v.minValue(${schema.minimum})`);
106
- if (IsDefined<number>(schema.maximum)) constraints.push(`v.maxValue(${schema.maximum})`);
107
- if (IsDefined<number>(schema.exclusiveMinimum)) constraints.push(`v.minValue(${schema.exclusiveMinimum + 1})`);
108
- if (IsDefined<number>(schema.exclusiveMaximum)) constraints.push(`v.maxValue(${schema.exclusiveMaximum - 1})`);
109
- return Type('v.number', null, constraints);
110
- }
111
- function Object(schema: Types.TObject) {
112
- // prettier-ignore
113
- const properties = globalThis.Object.entries(schema.properties).map(([key, value]) => {
114
- const optional = Types.TypeGuard.IsOptional(value)
115
- const property = PropertyEncoder.Encode(key)
116
- return optional ? `${property}: v.optional(${Visit(value)})` : `${property}: ${Visit(value)}`
117
- }).join(`,`)
118
- const constraints: string[] = [];
119
- return Type(`v.object`, `{\n${properties}\n}`, constraints);
120
- }
121
- function Promise(schema: Types.TPromise) {
122
- return UnsupportedType(schema);
123
- }
124
- function Record(schema: Types.TRecord) {
125
- for (const [key, value] of globalThis.Object.entries(schema.patternProperties)) {
126
- const type = Visit(value);
127
- if (key === `^(0|[1-9][0-9]*)$`) {
128
- return UnsupportedType(schema);
129
- } else {
130
- return Type(`v.record`, type, []);
131
- }
132
- }
133
- throw Error(`Unreachable`);
134
- }
135
- function Ref(schema: Types.TRef) {
136
- if (!reference_map.has(schema.$ref!)) return UnsupportedType(schema);
137
- return schema.$ref;
138
- }
139
- function This(schema: Types.TThis) {
140
- return UnsupportedType(schema);
141
- }
142
- function Tuple(schema: Types.TTuple) {
143
- if (schema.items === undefined) return `[]`;
144
- const items = schema.items.map((schema) => Visit(schema)).join(`, `);
145
- return Type(`v.tuple`, `[${items}]`, []);
146
- }
147
- function TemplateLiteral(schema: Types.TTemplateLiteral) {
148
- const constaint = Type(`v.regex`, `/${schema.pattern}/`, []);
149
- return Type(`v.string`, null, [constaint]);
150
- }
151
- function UInt8Array(schema: Types.TUint8Array): string {
152
- return UnsupportedType(schema);
153
- }
154
- function Undefined(schema: Types.TUndefined) {
155
- return Type(`v.undefinedType`, null, []);
156
- }
157
- function Union(schema: Types.TUnion) {
158
- const inner = schema.anyOf.map((schema) => Visit(schema)).join(`, `);
159
- return Type(`v.union`, `[${inner}]`, []);
160
- }
161
- function Unknown(schema: Types.TUnknown) {
162
- return Type(`v.unknown`, null, []);
163
- }
164
- function Void(schema: Types.TVoid) {
165
- return Type(`v.voidType`, null, []);
166
- }
167
- function UnsupportedType(schema: Types.TSchema) {
168
- return `v.any(/* unsupported */)`;
169
- }
170
- function Visit(schema: Types.TSchema): string {
171
- if (schema.$id !== undefined) reference_map.set(schema.$id, schema);
172
- if (schema.$id !== undefined && emitted_set.has(schema.$id!)) return schema.$id!;
173
- if (Types.TypeGuard.IsAny(schema)) return Any(schema);
174
- if (Types.TypeGuard.IsArray(schema)) return Array(schema);
175
- if (Types.TypeGuard.IsBigInt(schema)) return BigInt(schema);
176
- if (Types.TypeGuard.IsBoolean(schema)) return Boolean(schema);
177
- if (Types.TypeGuard.IsDate(schema)) return Date(schema);
178
- if (Types.TypeGuard.IsConstructor(schema)) return Constructor(schema);
179
- if (Types.TypeGuard.IsFunction(schema)) return Function(schema);
180
- if (Types.TypeGuard.IsInteger(schema)) return Integer(schema);
181
- if (Types.TypeGuard.IsIntersect(schema)) return Intersect(schema);
182
- if (Types.TypeGuard.IsLiteral(schema)) return Literal(schema);
183
- if (Types.TypeGuard.IsNever(schema)) return Never(schema);
184
- if (Types.TypeGuard.IsNull(schema)) return Null(schema);
185
- if (Types.TypeGuard.IsNumber(schema)) return Number(schema);
186
- if (Types.TypeGuard.IsObject(schema)) return Object(schema);
187
- if (Types.TypeGuard.IsPromise(schema)) return Promise(schema);
188
- if (Types.TypeGuard.IsRecord(schema)) return Record(schema);
189
- if (Types.TypeGuard.IsRef(schema)) return Ref(schema);
190
- if (Types.TypeGuard.IsString(schema)) return String(schema);
191
- if (Types.TypeGuard.IsTemplateLiteral(schema)) return TemplateLiteral(schema);
192
- if (Types.TypeGuard.IsThis(schema)) return This(schema);
193
- if (Types.TypeGuard.IsTuple(schema)) return Tuple(schema);
194
- if (Types.TypeGuard.IsUint8Array(schema)) return UInt8Array(schema);
195
- if (Types.TypeGuard.IsUndefined(schema)) return Undefined(schema);
196
- if (Types.TypeGuard.IsUnion(schema)) return Union(schema);
197
- if (Types.TypeGuard.IsUnknown(schema)) return Unknown(schema);
198
- if (Types.TypeGuard.IsVoid(schema)) return Void(schema);
199
- return UnsupportedType(schema);
200
- }
201
- function Collect(schema: Types.TSchema) {
202
- return [...Visit(schema)].join(``);
203
- }
204
- function GenerateType(model: TypeBoxModel, schema: Types.TSchema, references: Types.TSchema[]) {
205
- const output: string[] = [];
206
- for (const reference of references) {
207
- if (reference.$id === undefined) return UnsupportedType(schema);
208
- reference_map.set(reference.$id, reference);
209
- }
210
- const type = Collect(schema);
211
- if (recursive_set.has(schema.$id!)) {
212
- output.push(`export ${ModelToTypeScript.GenerateType(model, schema.$id!)}`);
213
- output.push(
214
- `export const ${schema.$id || `T`}: v.Output<${schema.$id}> = v.lazy(() => ${Formatter.Format(type)})`,
215
- );
216
- } else {
217
- output.push(`export type ${schema.$id} = v.Output<typeof ${schema.$id}>`);
218
- output.push(`export const ${schema.$id || `T`} = ${Formatter.Format(type)}`);
219
- }
220
- if (schema.$id) emitted_set.add(schema.$id);
221
- return output.join('\n');
222
- }
223
- const reference_map = new Map<string, Types.TSchema>();
224
- const recursive_set = new Set<string>();
225
- const emitted_set = new Set<string>();
226
- export function Generate(model: TypeBoxModel): string {
227
- reference_map.clear();
228
- recursive_set.clear();
229
- emitted_set.clear();
230
- const buffer: string[] = [`import * as v from 'valibot'`, ''];
231
- for (const type of model.types.filter((type) => Types.TypeGuard.IsSchema(type))) {
232
- buffer.push(GenerateType(model, type, model.types));
233
- }
234
- return Formatter.Format(buffer.join('\n'));
235
- }
236
- }
@@ -1,42 +0,0 @@
1
- /*--------------------------------------------------------------------------
2
-
3
- @sinclair/typebox-codegen
4
-
5
- The MIT License (MIT)
6
-
7
- Permission is hereby granted, free of charge, to any person obtaining a copy
8
- of this software and associated documentation files (the "Software"), to deal
9
- in the Software without restriction, including without limitation the rights
10
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- copies of the Software, and to permit persons to whom the Software is
12
- furnished to do so, subject to the following conditions:
13
-
14
- The above copyright notice and this permission notice shall be included in
15
- all copies or substantial portions of the Software.
16
-
17
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
- THE SOFTWARE.
24
-
25
- ---------------------------------------------------------------------------*/
26
- import { Value } from '@sinclair/typebox/value';
27
- import { Formatter } from '../common/formatter';
28
- import { TypeBoxModel } from './model';
29
-
30
- export namespace ModelToValue {
31
- export function Generate(model: TypeBoxModel): string {
32
- const definitions: string[] = [];
33
-
34
- for (const type of model.types) {
35
- definitions.push(`
36
- export const ${type.$id!} = ${JSON.stringify(Value.Create(type, model.types))};
37
- `);
38
- }
39
- const output = [...definitions];
40
- return output.join('\n\n');
41
- }
42
- }