@@ -3,15 +3,47 @@ import type { TAnySchema } from '@sinclair/typebox'
3
3
const Kind = Symbol . for ( 'TypeBox.Kind' )
4
4
const OptionalKind = Symbol . for ( 'TypeBox.Optional' )
5
5
6
- const isSpecialProperty = ( name : string ) : boolean => / ( \ | - | \t | \n ) / . test ( name )
6
+ const isSpecialProperty = ( name : string ) => / ( \ | - | \t | \n ) / . test ( name )
7
7
8
- const joinProperty = ( v1 : string , v2 : string ) : string => {
8
+ const joinProperty = ( v1 : string , v2 : string ) => {
9
9
if ( isSpecialProperty ( v2 ) ) return `${ v1 } ["${ v2 } "]`
10
10
11
11
return `${ v1 } .${ v2 } `
12
12
}
13
13
14
- const encodeProperty = ( v : string ) : string => `"${ v } "`
14
+ const encodeProperty = ( v : string ) => `"${ v } "`
15
+
16
+ const isInteger = ( schema : TAnySchema ) => {
17
+ if ( ! schema . anyOf || ( Kind in schema && schema [ Kind ] !== 'Union' ) )
18
+ return false
19
+
20
+ let hasIntegerFormat = false
21
+ let hasNumberType = false
22
+
23
+ for ( const type of schema . anyOf ) {
24
+ if ( type . type === 'null' || type . type === 'undefined' ) {
25
+ continue
26
+ }
27
+
28
+ if (
29
+ ! hasIntegerFormat &&
30
+ type . type === 'string' &&
31
+ type . format === 'integer'
32
+ ) {
33
+ hasIntegerFormat = true
34
+ continue
35
+ }
36
+
37
+ if ( ! hasNumberType && type . type === 'number' ) {
38
+ hasNumberType = true
39
+ continue
40
+ }
41
+
42
+ return false
43
+ }
44
+
45
+ return hasIntegerFormat && hasNumberType
46
+ }
15
47
16
48
const getMetadata = ( schema : TAnySchema ) => {
17
49
let isNullable = false
@@ -148,16 +180,16 @@ const accelerate = (
148
180
149
181
switch ( schema . type ) {
150
182
case 'string' :
151
- if ( isNullable || isUndefinable )
152
- v = `\${${ nullableCondition } ?null:\`\\"\${${ property } }\\"\`}`
183
+ if ( nullableCondition )
184
+ v = `\${${ nullableCondition } ?${ schema . default !== undefined ? `'" ${ schema . default } "'` : `' null'` } :\`\\"\${${ property } }\\"\`}`
153
185
else v = `\"\${${ property } }\"`
154
186
break
155
187
156
188
case 'number' :
157
189
case 'boolean' :
158
- case 'integer' :
159
190
case 'bigint' :
160
- if ( nullableCondition ) v = `\${${ property } ??null}`
191
+ if ( nullableCondition )
192
+ v = `\${${ property } ??${ schema . default !== undefined ? schema . default : `'null'` } }`
161
193
else v = `\${${ property } }`
162
194
break
163
195
@@ -169,7 +201,7 @@ const accelerate = (
169
201
break
170
202
171
203
case 'object' :
172
- if ( nullableCondition ) v += `\${${ nullableCondition } ?null:\``
204
+ if ( nullableCondition ) v += `\${${ nullableCondition } ?" null" :\``
173
205
174
206
v += '{'
175
207
@@ -182,7 +214,7 @@ const accelerate = (
182
214
183
215
let init = true
184
216
let hasOptional = false
185
- let op = `op[ ${ instruction . optional } ] `
217
+ let op = `op${ instruction . optional } `
186
218
187
219
for ( const key in schema . properties )
188
220
if ( OptionalKind in schema . properties [ key ] ) {
@@ -210,8 +242,16 @@ const accelerate = (
210
242
211
243
const comma = `\${${ op } ?',':(${ op } =true)&&''}`
212
244
245
+ let defaultValue = schema . properties [ key ] . default
246
+ if ( defaultValue !== undefined ) {
247
+ if ( typeof defaultValue === 'string' )
248
+ defaultValue = `"${ defaultValue } "`
249
+
250
+ defaultValue = `\`${ comma } ${ k } :${ defaultValue } \``
251
+ } else defaultValue = '""'
252
+
213
253
v += isOptional
214
- ? `\${(${ name } !== undefined?\`${ comma } ${ k } :${ p } \`:'' )}`
254
+ ? `\${(${ name } === undefined?${ defaultValue } : \`${ comma } ${ k } :${ p } \`)}`
215
255
: hasOptional
216
256
? `${ ! init ? ',' : `\${(${ op } =true)&&""}` } ${ k } :${ p } `
217
257
: `${ ! init ? ',' : '' } ${ k } :${ p } `
@@ -232,7 +272,7 @@ const accelerate = (
232
272
233
273
if ( schema . items . type === 'string' ) {
234
274
if ( nullableCondition )
235
- v += `\${${ nullableCondition } ?null:${ property } .length?\`["$\{${ property } .join('",\"')}"]\`:"[]"}`
275
+ v += `\${${ nullableCondition } ?" null" :${ property } .length?\`["$\{${ property } .join('",\"')}"]\`:"[]"}`
236
276
else
237
277
v += `\${${ property } .length?\`["$\{${ property } .join('",\"')}"]\`:"[]"}`
238
278
@@ -242,18 +282,18 @@ const accelerate = (
242
282
if (
243
283
schema . items . type === 'number' ||
244
284
schema . items . type === 'boolean' ||
245
- schema . items . type === 'integer ' ||
246
- schema . items . type === 'bigint'
285
+ schema . items . type === 'bigint ' ||
286
+ isInteger ( schema . items )
247
287
) {
248
288
if ( nullableCondition )
249
- v += `\${${ nullableCondition } ?null:${ property } .length?\`[$\{${ property } .join(',')}]\`:"[]"`
289
+ v += `\${${ nullableCondition } ?'" null"' :${ property } .length?\`[$\{${ property } .join(',')}]\`:"[]"`
250
290
else
251
291
v += `\${${ property } .length?\`[$\{${ property } .join(',')}]\`:"[]"}`
252
292
253
293
break
254
294
}
255
295
256
- if ( isNullable || isUndefinable ) v += `\${!${ property } ?null:\``
296
+ if ( isNullable || isUndefinable ) v += `\${!${ property } ?'" null"' :\``
257
297
258
298
if ( ! isRoot ) v += `\${(()=>{`
259
299
@@ -276,14 +316,22 @@ const accelerate = (
276
316
default :
277
317
if ( isDateType ( schema ) ) {
278
318
if ( isNullable || isUndefinable )
279
- v = `\${${ nullableCondition } ?null:typeof ${ property } ==="object"?\`\"\${${ property } .toISOString()}\"\`:${ property } }`
319
+ v = `\${${ nullableCondition } ?${ schema . default !== undefined ? `'" ${ schema . default } "'` : "' null'" } :typeof ${ property } ==="object"?\`\"\${${ property } .toISOString()}\"\`:${ property } }`
280
320
else {
281
321
v = `\${typeof ${ property } ==="object"?\`\"\${${ property } .toISOString()}\"\`:${ property } }`
282
322
}
283
323
284
324
break
285
325
}
286
326
327
+ if ( isInteger ( schema ) ) {
328
+ if ( nullableCondition )
329
+ v = `\${${ property } ??${ schema . default !== undefined ? schema . default : `'null'` } }`
330
+ else v = `\${${ property } }`
331
+
332
+ break
333
+ }
334
+
287
335
v = `$\{JSON.stringify(${ property } )}`
288
336
289
337
break
@@ -296,16 +344,27 @@ const accelerate = (
296
344
297
345
let setup = ''
298
346
299
- if ( instruction . optional )
300
- setup += `let op=new Array(${ instruction . optional } )\n`
347
+ if ( instruction . optional ) {
348
+ setup += 'let '
349
+
350
+ for ( let i = 0 ; i < instruction . optional ; i ++ ) {
351
+ if ( i !== 0 ) setup += ','
352
+ setup += `op${ i } =false`
353
+ }
354
+
355
+ setup += '\n'
356
+ }
357
+
358
+ if ( instruction . properties . length ) {
359
+ setup += 'const '
360
+
361
+ for ( let i = 0 ; i < instruction . properties . length ; i ++ ) {
362
+ if ( i !== 0 ) setup += ','
363
+ setup += `s${ i } =${ instruction . properties [ i ] } `
364
+ }
301
365
302
- for ( let i = 0 ; i < instruction . properties . length ; i ++ ) {
303
- const key = `s${ i } `
304
- if ( i === 0 ) setup += 'const '
305
- else setup += ','
306
- setup += `${ key } =${ instruction . properties [ i ] } `
366
+ setup += '\n'
307
367
}
308
- if ( instruction . properties . length ) setup += '\n'
309
368
310
369
if ( isArray ) return setup + '\n' + v
311
370
0 commit comments