@@ -140,21 +140,33 @@ function build (schema, options) {
140
140
const location = new Location ( schema , context . rootSchemaId )
141
141
const code = buildValue ( context , location , 'input' )
142
142
143
- let contextFunctionCode
143
+ let contextFunctionCode = `
144
+ const JSON_STR_BEGIN_OBJECT = '{'
145
+ const JSON_STR_END_OBJECT = '}'
146
+ const JSON_STR_BEGIN_ARRAY = '['
147
+ const JSON_STR_END_ARRAY = ']'
148
+ const JSON_STR_COMMA = ','
149
+ const JSON_STR_COLONS = ':'
150
+ const JSON_STR_QUOTE = '"'
151
+ const JSON_STR_EMPTY_OBJECT = JSON_STR_BEGIN_OBJECT + JSON_STR_END_OBJECT
152
+ const JSON_STR_EMPTY_ARRAY = JSON_STR_BEGIN_ARRAY + JSON_STR_END_ARRAY
153
+ const JSON_STR_EMPTY_STRING = JSON_STR_QUOTE + JSON_STR_QUOTE
154
+ const JSON_STR_NULL = 'null'
155
+ `
144
156
145
157
// If we have only the invocation of the 'anonymous0' function, we would
146
158
// basically just wrap the 'anonymous0' function in the 'main' function and
147
159
// and the overhead of the intermediate variable 'json'. We can avoid the
148
160
// wrapping and the unnecessary memory allocation by aliasing 'anonymous0' to
149
161
// 'main'
150
162
if ( code === 'json += anonymous0(input)' ) {
151
- contextFunctionCode = `
163
+ contextFunctionCode + = `
152
164
${ context . functions . join ( '\n' ) }
153
165
const main = anonymous0
154
166
return main
155
167
`
156
168
} else {
157
- contextFunctionCode = `
169
+ contextFunctionCode + = `
158
170
function main (input) {
159
171
let json = ''
160
172
${ code }
@@ -284,7 +296,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
284
296
code += `
285
297
if (/${ propertyKey . replace ( / \\ * \/ / g, '\\/' ) } /.test(key)) {
286
298
${ addComma }
287
- json += serializer.asString(key) + ':'
299
+ json += serializer.asString(key) + JSON_STR_COLONS
288
300
${ buildValue ( context , propertyLocation , 'value' ) }
289
301
continue
290
302
}
@@ -299,13 +311,13 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
299
311
if ( additionalPropertiesSchema === true ) {
300
312
code += `
301
313
${ addComma }
302
- json += serializer.asString(key) + ':' + JSON.stringify(value)
314
+ json += serializer.asString(key) + JSON_STR_COLONS + JSON.stringify(value)
303
315
`
304
316
} else {
305
317
const propertyLocation = location . getPropertyLocation ( 'additionalProperties' )
306
318
code += `
307
319
${ addComma }
308
- json += serializer.asString(key) + ':'
320
+ json += serializer.asString(key) + JSON_STR_COLONS
309
321
${ buildValue ( context , propertyLocation , 'value' ) }
310
322
`
311
323
}
@@ -341,12 +353,12 @@ function buildInnerObject (context, location) {
341
353
}
342
354
}
343
355
344
- code += 'let json = \'{\' \n'
356
+ code += 'let json = JSON_STR_BEGIN_OBJECT \n'
345
357
346
358
let addComma = ''
347
359
if ( ! hasRequiredProperties ) {
348
360
code += 'let addComma = false\n'
349
- addComma = '!addComma && (addComma = true) || (json += \',\' )'
361
+ addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA )'
350
362
}
351
363
352
364
for ( const key of propertiesKeys ) {
@@ -392,7 +404,7 @@ function buildInnerObject (context, location) {
392
404
}
393
405
394
406
code += `
395
- return json + '}'
407
+ return json + JSON_STR_END_OBJECT
396
408
`
397
409
return code
398
410
}
@@ -483,7 +495,7 @@ function buildObject (context, location) {
483
495
// ${ schemaRef }
484
496
function ${ functionName } (input) {
485
497
const obj = ${ toJSON ( 'input' ) }
486
- ${ ! nullable ? 'if (obj === null) return \'{}\' ' : '' }
498
+ ${ ! nullable ? 'if (obj === null) return JSON_STR_EMPTY_OBJECT ' : '' }
487
499
488
500
${ buildInnerObject ( context , location ) }
489
501
}
@@ -524,7 +536,7 @@ function buildArray (context, location) {
524
536
525
537
const nullable = schema . nullable === true
526
538
functionCode += `
527
- ${ ! nullable ? 'if (obj === null) return \'[]\' ' : '' }
539
+ ${ ! nullable ? 'if (obj === null) return JSON_STR_EMPTY_ARRAY ' : '' }
528
540
if (!Array.isArray(obj)) {
529
541
throw new TypeError(\`The value of '${ schemaRef } ' does not match schema definition.\`)
530
542
}
@@ -559,7 +571,7 @@ function buildArray (context, location) {
559
571
if (${ buildArrayTypeCondition ( item . type , `[${ i } ]` ) } ) {
560
572
${ tmpRes }
561
573
if (${ i } < arrayEnd) {
562
- json += ','
574
+ json += JSON_STR_COMMA
563
575
}
564
576
} else {
565
577
throw new Error(\`Item at ${ i } does not match schema definition.\`)
@@ -573,7 +585,7 @@ function buildArray (context, location) {
573
585
for (let i = ${ itemsSchema . length } ; i < arrayLength; i++) {
574
586
json += JSON.stringify(obj[i])
575
587
if (i < arrayEnd) {
576
- json += ','
588
+ json += JSON_STR_COMMA
577
589
}
578
590
}`
579
591
}
@@ -583,13 +595,13 @@ function buildArray (context, location) {
583
595
for (let i = 0; i < arrayLength; i++) {
584
596
${ code }
585
597
if (i < arrayEnd) {
586
- json += ','
598
+ json += JSON_STR_COMMA
587
599
}
588
600
}`
589
601
}
590
602
591
603
functionCode += `
592
- return \`[\${ json}]\`
604
+ return JSON_STR_BEGIN_ARRAY + json + JSON_STR_END_ARRAY
593
605
}`
594
606
595
607
context . functions . push ( functionCode )
@@ -717,7 +729,7 @@ function buildSingleTypeSerializer (context, location, input) {
717
729
718
730
switch ( schema . type ) {
719
731
case 'null' :
720
- return 'json += \'null\' '
732
+ return 'json += JSON_STR_NULL '
721
733
case 'string' : {
722
734
if ( schema . format === 'date-time' ) {
723
735
return `json += serializer.asDateTime(${ input } )`
@@ -731,9 +743,9 @@ function buildSingleTypeSerializer (context, location, input) {
731
743
return `
732
744
if (typeof ${ input } !== 'string') {
733
745
if (${ input } === null) {
734
- json += '""'
746
+ json += JSON_STR_EMPTY_STRING
735
747
} else if (${ input } instanceof Date) {
736
- json += '"' + ${ input } .toISOString() + '"'
748
+ json += JSON_STR_QUOTE + ${ input } .toISOString() + JSON_STR_QUOTE
737
749
} else if (${ input } instanceof RegExp) {
738
750
json += serializer.asString(${ input } .source)
739
751
} else {
@@ -777,7 +789,7 @@ function buildConstSerializer (location, input) {
777
789
if ( hasNullType ) {
778
790
code += `
779
791
if (${ input } === null) {
780
- json += 'null'
792
+ json += JSON_STR_NULL
781
793
} else {
782
794
`
783
795
}
@@ -984,7 +996,7 @@ function buildValue (context, location, input) {
984
996
if ( nullable ) {
985
997
code += `
986
998
if (${ input } === null) {
987
- json += 'null'
999
+ json += JSON_STR_NULL
988
1000
} else {
989
1001
`
990
1002
}
0 commit comments