@@ -409,9 +409,13 @@ const stringSerializerMap = {
409
409
time : 'serializer.asTime.bind(serializer)'
410
410
}
411
411
412
- function getStringSerializer ( format ) {
413
- return stringSerializerMap [ format ] ||
414
- 'serializer.asString.bind(serializer)'
412
+ function getStringSerializer ( format , nullable ) {
413
+ switch ( format ) {
414
+ case 'date-time' : return nullable ? 'serializer.asDatetimeNullable.bind(serializer)' : 'serializer.asDatetime.bind(serializer)'
415
+ case 'date' : return nullable ? 'serializer.asDateNullable.bind(serializer)' : 'serializer.asDate.bind(serializer)'
416
+ case 'time' : return nullable ? 'serializer.asTimeNullable.bind(serializer)' : 'serializer.asTime.bind(serializer)'
417
+ default : return nullable ? 'serializer.asStringNullable.bind(serializer)' : 'serializer.asString.bind(serializer)'
418
+ }
415
419
}
416
420
417
421
function getTestSerializer ( format ) {
@@ -1025,43 +1029,30 @@ function buildArray (location, code, name, key = null) {
1025
1029
`
1026
1030
}
1027
1031
1028
- code += `
1029
- var l = obj.length
1030
- if (l && l >= ${ largeArraySize } ) {`
1031
-
1032
- const concatSnippet = `
1032
+ code += 'const arrayLength = obj.length\n'
1033
+ if ( largeArrayMechanism !== 'default' ) {
1034
+ if ( largeArrayMechanism === 'json-stringify' ) {
1035
+ code += `if (arrayLength && arrayLength >= ${ largeArraySize } ) return JSON.stringify(obj)\n`
1036
+ } else {
1037
+ throw new Error ( `Unsupported large array mechanism ${ largeArrayMechanism } ` )
1033
1038
}
1039
+ }
1034
1040
1035
- var jsonOutput= ''
1036
- for (var i = 0; i < l; i++) {
1037
- var json = ''
1041
+ code += `
1042
+ let jsonOutput= ''
1043
+ for (let i = 0; i < arrayLength; i++) {
1044
+ let json = ''
1038
1045
${ result . code }
1039
1046
jsonOutput += json
1040
1047
1041
- if (json.length > 0 && i < l - 1) {
1048
+ if (json.length > 0 && i < arrayLength - 1) {
1042
1049
jsonOutput += ','
1043
1050
}
1044
1051
}
1045
1052
return \`[\${jsonOutput}]\`
1046
1053
}`
1047
1054
1048
- switch ( largeArrayMechanism ) {
1049
- case 'default' :
1050
- code += `
1051
- return \`[\${obj.map(${ result . mapFnName } ).join(',')}]\``
1052
- break
1053
-
1054
- case 'json-stringify' :
1055
- code += `
1056
- return JSON.stringify(obj)`
1057
- break
1058
-
1059
- default :
1060
- throw new Error ( `Unsupported large array mechanism ${ largeArrayMechanism } ` )
1061
- }
1062
-
1063
1055
code += `
1064
- ${ concatSnippet }
1065
1056
${ result . laterCode }
1066
1057
`
1067
1058
@@ -1144,9 +1135,6 @@ function asFuncName (str) {
1144
1135
}
1145
1136
1146
1137
function nested ( laterCode , name , key , location , subKey , isArray ) {
1147
- let code = ''
1148
- let funcName
1149
-
1150
1138
subKey = subKey || ''
1151
1139
1152
1140
let schema = location . schema
@@ -1167,47 +1155,44 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1167
1155
1168
1156
const accessor = isArray ? key : `[${ JSON . stringify ( key ) } ]`
1169
1157
1158
+ let code = ''
1159
+ let funcName
1160
+
1170
1161
switch ( type ) {
1171
1162
case 'null' :
1172
- funcName = '$asNull'
1173
1163
code += `
1174
1164
json += serializer.asNull()
1175
1165
`
1176
1166
break
1177
1167
case 'string' : {
1178
- funcName = '$asString'
1179
- const stringSerializer = getStringSerializer ( schema . format )
1180
- code += nullable ? `json += obj${ accessor } === null ? null : ${ stringSerializer } (obj${ accessor } )` : `json += ${ stringSerializer } (obj${ accessor } )`
1168
+ funcName = getStringSerializer ( schema . format , nullable )
1169
+ code += `json += ${ funcName } (obj${ accessor } )`
1181
1170
break
1182
1171
}
1183
1172
case 'integer' :
1184
- funcName = '$ asInteger'
1185
- code += nullable ? `json += obj ${ accessor } === null ? null : serializer.asInteger(obj ${ accessor } )` : `json += serializer.asInteger (obj${ accessor } )`
1173
+ funcName = nullable ? 'serializer.asIntegerNullable.bind(serializer)' : 'serializer. asInteger.bind(serializer) '
1174
+ code += `json += ${ funcName } (obj${ accessor } )`
1186
1175
break
1187
1176
case 'number' :
1188
- funcName = '$ asNumber'
1189
- code += nullable ? `json += obj ${ accessor } === null ? null : serializer.asNumber(obj ${ accessor } )` : `json += serializer.asNumber (obj${ accessor } )`
1177
+ funcName = nullable ? 'serializer.asNumberNullable.bind(serializer)' : 'serializer. asNumber.bind(serializer) '
1178
+ code += `json += ${ funcName } (obj${ accessor } )`
1190
1179
break
1191
1180
case 'boolean' :
1192
- funcName = '$ asBoolean'
1193
- code += nullable ? `json += obj ${ accessor } === null ? null : serializer.asBoolean(obj ${ accessor } )` : `json += serializer.asBoolean (obj${ accessor } )`
1181
+ funcName = nullable ? 'serializer.asBooleanNullable.bind(serializer)' : 'serializer. asBoolean.bind(serializer) '
1182
+ code += `json += ${ funcName } (obj${ accessor } )`
1194
1183
break
1195
1184
case 'object' :
1196
1185
funcName = asFuncName ( name + key + subKey )
1197
1186
laterCode = buildObject ( location , laterCode , funcName )
1198
- code += `
1199
- json += ${ funcName } (obj${ accessor } )
1200
- `
1187
+ code += `json += ${ funcName } (obj${ accessor } )`
1201
1188
break
1202
1189
case 'array' :
1203
1190
funcName = asFuncName ( '$arr' + name + key + subKey ) // eslint-disable-line
1204
1191
laterCode = buildArray ( location , laterCode , funcName , key )
1205
- code += `
1206
- json += ${ funcName } (obj${ accessor } )
1207
- `
1192
+ code += `json += ${ funcName } (obj${ accessor } )`
1208
1193
break
1209
1194
case undefined :
1210
- funcName = '$ asNull'
1195
+ funcName = 'serializer. asNull.bind(serializer) '
1211
1196
if ( 'anyOf' in schema ) {
1212
1197
// beware: dereferenceOfRefs has side effects and changes schema.anyOf
1213
1198
const anyOfLocations = dereferenceOfRefs ( location , 'anyOf' )
@@ -1344,11 +1329,7 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1344
1329
}
1345
1330
}
1346
1331
1347
- return {
1348
- code,
1349
- laterCode,
1350
- mapFnName : funcName
1351
- }
1332
+ return { code, laterCode }
1352
1333
}
1353
1334
1354
1335
function isEmpty ( schema ) {
0 commit comments