@@ -11,6 +11,7 @@ const Serializer = require('./lib/serializer')
11
11
const Validator = require ( './lib/validator' )
12
12
const RefResolver = require ( './lib/ref-resolver' )
13
13
const Location = require ( './lib/location' )
14
+ const optimize = require ( './lib/optimize' )
14
15
15
16
let largeArraySize = 2e4
16
17
let largeArrayMechanism = 'default'
@@ -27,6 +28,18 @@ const validLargeArrayMechanisms = [
27
28
'json-stringify'
28
29
]
29
30
31
+ const serializerFns = `
32
+ const {
33
+ asString,
34
+ asInteger,
35
+ asNumber,
36
+ asBoolean,
37
+ asDateTime,
38
+ asDate,
39
+ asTime,
40
+ } = serializer
41
+ `
42
+
30
43
const addComma = '!addComma && (addComma = true) || (json += \',\')'
31
44
32
45
function isValidSchema ( schema , name ) {
@@ -119,21 +132,8 @@ function build (schema, options) {
119
132
const location = new Location ( schema , context . rootSchemaId )
120
133
const code = buildValue ( context , location , 'input' )
121
134
122
- let contextFunctionCode
123
-
124
- // If we have only the invocation of the 'anonymous0' function, we would
125
- // basically just wrap the 'anonymous0' function in the 'main' function and
126
- // and the overhead of the intermediate variable 'json'. We can avoid the
127
- // wrapping and the unnecessary memory allocation by aliasing 'anonymous0' to
128
- // 'main'
129
- if ( code === 'json += anonymous0(input)' ) {
130
- contextFunctionCode = `
131
- ${ context . functions . join ( '\n' ) }
132
- const main = anonymous0
133
- return main
134
- `
135
- } else {
136
- contextFunctionCode = `
135
+ let contextFunctionCode = `
136
+ ${ serializerFns }
137
137
function main (input) {
138
138
let json = ''
139
139
${ code }
@@ -142,7 +142,8 @@ function build (schema, options) {
142
142
${ context . functions . join ( '\n' ) }
143
143
return main
144
144
`
145
- }
145
+
146
+ contextFunctionCode = optimize ( contextFunctionCode )
146
147
147
148
const serializer = new Serializer ( options )
148
149
const validator = new Validator ( options . ajv )
@@ -263,7 +264,7 @@ function buildExtraObjectPropertiesSerializer (context, location) {
263
264
code += `
264
265
if (/${ propertyKey . replace ( / \\ * \/ / g, '\\/' ) } /.test(key)) {
265
266
${ addComma }
266
- json += serializer. asString(key) + ':'
267
+ json += asString(key) + ':'
267
268
${ buildValue ( context , propertyLocation , 'value' ) }
268
269
continue
269
270
}
@@ -278,13 +279,13 @@ function buildExtraObjectPropertiesSerializer (context, location) {
278
279
if ( additionalPropertiesSchema === true ) {
279
280
code += `
280
281
${ addComma }
281
- json += serializer. asString(key) + ':' + JSON.stringify(value)
282
+ json += asString(key) + ':' + JSON.stringify(value)
282
283
`
283
284
} else {
284
285
const propertyLocation = location . getPropertyLocation ( 'additionalProperties' )
285
286
code += `
286
287
${ addComma }
287
- json += serializer. asString(key) + ':'
288
+ json += asString(key) + ':'
288
289
${ buildValue ( context , propertyLocation , 'value' ) }
289
290
`
290
291
}
@@ -743,21 +744,21 @@ function buildSingleTypeSerializer (context, location, input) {
743
744
return 'json += \'null\''
744
745
case 'string' : {
745
746
if ( schema . format === 'date-time' ) {
746
- return `json += serializer. asDateTime(${ input } )`
747
+ return `json += asDateTime(${ input } )`
747
748
} else if ( schema . format === 'date' ) {
748
- return `json += serializer. asDate(${ input } )`
749
+ return `json += asDate(${ input } )`
749
750
} else if ( schema . format === 'time' ) {
750
- return `json += serializer. asTime(${ input } )`
751
+ return `json += asTime(${ input } )`
751
752
} else {
752
- return `json += serializer. asString(${ input } )`
753
+ return `json += asString(${ input } )`
753
754
}
754
755
}
755
756
case 'integer' :
756
- return `json += serializer. asInteger(${ input } )`
757
+ return `json += asInteger(${ input } )`
757
758
case 'number' :
758
- return `json += serializer. asNumber(${ input } )`
759
+ return `json += asNumber(${ input } )`
759
760
case 'boolean' :
760
- return `json += serializer. asBoolean(${ input } )`
761
+ return `json += asBoolean(${ input } )`
761
762
case 'object' : {
762
763
const funcName = buildObject ( context , location )
763
764
return `json += ${ funcName } (${ input } )`
0 commit comments