diff --git a/index.js b/index.js index 2390c28a..42863552 100644 --- a/index.js +++ b/index.js @@ -15,6 +15,22 @@ const SINGLE_TICK = /'/g let largeArraySize = 2e4 let largeArrayMechanism = 'default' +const serializerFns = ` +let { + asString, + asInteger, + asNumber, + asBoolean, + asDateTime, + asDate, + asTime, + asUnsafeString +} = serializer + +asInteger = asInteger.bind(serializer) + +` + const validRoundingMethods = [ 'floor', 'ceil', @@ -141,6 +157,7 @@ function build (schema, options) { const code = buildValue(context, location, 'input') let contextFunctionCode = ` + ${serializerFns} const JSON_STR_BEGIN_OBJECT = '{' const JSON_STR_END_OBJECT = '}' const JSON_STR_BEGIN_ARRAY = '[' @@ -292,7 +309,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) { code += ` if (/${propertyKey.replace(/\\*\//g, '\\/')}/.test(key)) { ${addComma} - json += serializer.asString(key) + JSON_STR_COLONS + json += asString(key) + JSON_STR_COLONS ${buildValue(context, propertyLocation, 'value')} continue } @@ -307,13 +324,13 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) { if (additionalPropertiesSchema === true) { code += ` ${addComma} - json += serializer.asString(key) + JSON_STR_COLONS + JSON.stringify(value) + json += asString(key) + JSON_STR_COLONS + JSON.stringify(value) ` } else { const propertyLocation = location.getPropertyLocation('additionalProperties') code += ` ${addComma} - json += serializer.asString(key) + JSON_STR_COLONS + json += asString(key) + JSON_STR_COLONS ${buildValue(context, propertyLocation, 'value')} ` } @@ -729,13 +746,13 @@ function buildSingleTypeSerializer (context, location, input) { return 'json += JSON_STR_NULL' case 'string': { if (schema.format === 'date-time') { - return `json += serializer.asDateTime(${input})` + return `json += asDateTime(${input})` } else if (schema.format === 'date') { - return `json += serializer.asDate(${input})` + return `json += asDate(${input})` } else if (schema.format === 'time') { - return `json += serializer.asTime(${input})` + return `json += asTime(${input})` } else if (schema.format === 'unsafe') { - return `json += serializer.asUnsafeString(${input})` + return `json += asUnsafeString(${input})` } else { return ` if (typeof ${input} !== 'string') { @@ -744,22 +761,22 @@ function buildSingleTypeSerializer (context, location, input) { } else if (${input} instanceof Date) { json += JSON_STR_QUOTE + ${input}.toISOString() + JSON_STR_QUOTE } else if (${input} instanceof RegExp) { - json += serializer.asString(${input}.source) + json += asString(${input}.source) } else { - json += serializer.asString(${input}.toString()) + json += asString(${input}.toString()) } } else { - json += serializer.asString(${input}) + json += asString(${input}) } ` } } case 'integer': - return `json += serializer.asInteger(${input})` + return `json += asInteger(${input})` case 'number': - return `json += serializer.asNumber(${input})` + return `json += asNumber(${input})` case 'boolean': - return `json += serializer.asBoolean(${input})` + return `json += asBoolean(${input})` case 'object': { const funcName = buildObject(context, location) return `json += ${funcName}(${input})`