Skip to content

Commit

Permalink
optimize string version
Browse files Browse the repository at this point in the history
Signed-off-by: francesco <[email protected]>
  • Loading branch information
cesco69 authored Apr 12, 2024
1 parent 70861ef commit ee2f4da
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ const SINGLE_TICK = /'/g

let largeArraySize = 2e4
let largeArrayMechanism = 'default'
let addCommand = 'json +='
let addStartCommand = 'json += '
let addEndCommand = ''

const validRoundingMethods = [
'floor',
Expand Down Expand Up @@ -102,7 +103,8 @@ function build (schema, options) {
}

if (context?.options?.enableStream) {
addCommand = 'json.push'
addStartCommand = 'json.push('
addEndCommand = ')'
}

if (options.schema) {
Expand Down Expand Up @@ -297,7 +299,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
code += `
if (/${propertyKey.replace(/\\*\//g, '\\/')}/.test(key)) {
${addComma}
${addCommand}(serializer.asString(key) + ':')
${addStartCommand}serializer.asString(key) + ':'${addEndCommand}
${buildValue(context, propertyLocation, 'value')}
continue
}
Expand All @@ -312,13 +314,13 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma) {
if (additionalPropertiesSchema === true) {
code += `
${addComma}
${addCommand}(serializer.asString(key) + ':' + JSON.stringify(value))
${addStartCommand}serializer.asString(key) + ':' + JSON.stringify(value)${addEndCommand}
`
} else {
const propertyLocation = location.getPropertyLocation('additionalProperties')
code += `
${addComma}
${addCommand}(serializer.asString(key) + ':')
${addStartCommand}serializer.asString(key) + ':'${addEndCommand}
${buildValue(context, propertyLocation, 'value')}
`
}
Expand Down Expand Up @@ -354,12 +356,12 @@ function buildInnerObject (context, location) {
}
}

code += `${addCommand}('{')\n`
code += `${addStartCommand}'{'${addEndCommand}\n`

let addComma = ''
if (!hasRequiredProperties) {
code += 'let addComma = false\n'
addComma = `!addComma && (addComma = true) || (${addCommand}(','))`
addComma = `!addComma && (addComma = true) || (${addStartCommand}','${addEndCommand})`
}

for (const key of propertiesKeys) {
Expand All @@ -376,15 +378,15 @@ function buildInnerObject (context, location) {
value = obj[${sanitizedKey}]
if (value !== undefined) {
${addComma}
${addCommand}(${JSON.stringify(sanitizedKey + ':')})
${addStartCommand}${JSON.stringify(sanitizedKey + ':')}${addEndCommand}
// buildValue: ${JSON.stringify(location.schema)}
${buildValue(context, propertyLocation, 'value')}
}`

if (defaultValue !== undefined) {
code += ` else {
${addComma}
${addCommand}(${JSON.stringify(sanitizedKey + ':' + JSON.stringify(defaultValue))})
${addStartCommand}${JSON.stringify(sanitizedKey + ':' + JSON.stringify(defaultValue))}${addEndCommand}
}
`
} else if (isRequired) {
Expand All @@ -397,7 +399,7 @@ function buildInnerObject (context, location) {
}

if (hasRequiredProperties) {
addComma = `${addCommand}(',')`
addComma = `${addStartCommand}','${addEndCommand}`
}
}

Expand All @@ -406,7 +408,7 @@ function buildInnerObject (context, location) {
}

code += `
${addCommand}('}')
${addStartCommand}'}'${addEndCommand}
`
return code
}
Expand Down Expand Up @@ -497,7 +499,7 @@ function buildObject (context, location) {
// ${schemaRef}
function ${functionName} (input) {
const obj = ${toJSON('input')}
${!nullable ? `if (obj === null) return ${addCommand}('{}')` : ''}
${!nullable ? `if (obj === null) return ${addStartCommand}'{}'${addEndCommand}` : ''}
${buildInnerObject(context, location)}
}
Expand Down Expand Up @@ -538,7 +540,7 @@ function buildArray (context, location) {

const nullable = schema.nullable === true
functionCode += `
${!nullable ? `if (obj === null) return ${addCommand}('[]')` : ''}
${!nullable ? `if (obj === null) return ${addStartCommand}'[]'${addEndCommand}` : ''}
if (!Array.isArray(obj)) {
throw new TypeError(\`The value of '${schemaRef}' does not match schema definition.\`)
}
Expand All @@ -554,15 +556,15 @@ function buildArray (context, location) {
}

if (largeArrayMechanism === 'json-stringify') {
functionCode += `if (arrayLength && arrayLength >= ${largeArraySize}) return ${addCommand}(JSON.stringify(obj))\n`
functionCode += `if (arrayLength && arrayLength >= ${largeArraySize}) return ${addStartCommand}JSON.stringify(obj)${addEndCommand}\n`
}

functionCode += `
let value\n
`

if (Array.isArray(itemsSchema)) {
functionCode += `${addCommand}('[')\n`
functionCode += `${addStartCommand}'['${addEndCommand}\n`
for (let i = 0; i < itemsSchema.length; i++) {
const item = itemsSchema[i]
functionCode += `value = obj[${i}]`
Expand All @@ -572,7 +574,7 @@ function buildArray (context, location) {
if (${buildArrayTypeCondition(item.type, `[${i}]`)}) {
${tmpRes}
if (${i} < arrayLength - 1) {
${addCommand}(',')
${addStartCommand}','${addEndCommand}
}
} else {
throw new Error(\`Item at ${i} does not match schema definition.\`)
Expand All @@ -584,24 +586,24 @@ function buildArray (context, location) {
if (schema.additionalItems) {
functionCode += `
for (let i = ${itemsSchema.length}; i < arrayLength; i++) {
${addCommand}(JSON.stringify(obj[i]))
${addStartCommand}JSON.stringify(obj[i])${addEndCommand}
if (i < arrayLength - 1) {
${addCommand}(',')
${addStartCommand}','${addEndCommand}
}
}`
}
} else {
const code = buildValue(context, itemsLocation, 'obj[i]')
functionCode += `${addCommand}('[')
functionCode += `${addStartCommand}'['${addEndCommand}
for (let i = 0; i < arrayLength; i++) {
${code}
if (i < arrayLength - 1) {
${addCommand}(',')
${addStartCommand}','${addEndCommand}
}
}`
}

functionCode += `${addCommand}(']')
functionCode += `${addStartCommand}']'${addEndCommand}
}`

context.functions.push(functionCode)
Expand Down Expand Up @@ -729,40 +731,40 @@ function buildSingleTypeSerializer (context, location, input) {

switch (schema.type) {
case 'null':
return `${addCommand}('null')`
return `${addStartCommand}'null'${addEndCommand}`
case 'string': {
if (schema.format === 'date-time') {
return `${addCommand}(serializer.asDateTime(${input}))`
return `${addStartCommand}serializer.asDateTime(${input})${addEndCommand}`
} else if (schema.format === 'date') {
return `${addCommand}(serializer.asDate(${input}))`
return `${addStartCommand}serializer.asDate(${input})${addEndCommand}`
} else if (schema.format === 'time') {
return `${addCommand}(serializer.asTime(${input}))`
return `${addStartCommand}serializer.asTime(${input})${addEndCommand}`
} else if (schema.format === 'unsafe') {
return `${addCommand}(serializer.asUnsafeString(${input}))`
return `${addStartCommand}serializer.asUnsafeString(${input})${addEndCommand}`
} else {
return `
if (typeof ${input} !== 'string') {
if (${input} === null) {
${addCommand}('""')
${addStartCommand}'""'${addEndCommand}
} else if (${input} instanceof Date) {
${addCommand}('"' + ${input}.toISOString() + '"')
${addStartCommand}'"' + ${input}.toISOString() + '"'${addEndCommand}
} else if (${input} instanceof RegExp) {
${addCommand}(serializer.asString(${input}.source))
${addStartCommand}serializer.asString(${input}.source)${addEndCommand}
} else {
${addCommand}(serializer.asString(${input}.toString()))
${addStartCommand}serializer.asString(${input}.toString())${addEndCommand}
}
} else {
${addCommand}(serializer.asString(${input}))
${addStartCommand}serializer.asString(${input})${addEndCommand}
}
`
}
}
case 'integer':
return `${addCommand}(serializer.asInteger(${input}))`
return `${addStartCommand}serializer.asInteger(${input})${addEndCommand}`
case 'number':
return `${addCommand}(serializer.asNumber(${input}))`
return `${addStartCommand}serializer.asNumber(${input})${addEndCommand}`
case 'boolean':
return `${addCommand}(serializer.asBoolean(${input}))`
return `${addStartCommand}serializer.asBoolean(${input})${addEndCommand}`
case 'object': {
const funcName = buildObject(context, location)
return `${funcName}(${input},json)`
Expand All @@ -772,7 +774,7 @@ function buildSingleTypeSerializer (context, location, input) {
return `${funcName}(${input},json)`
}
case undefined:
return `${addCommand}(JSON.stringify(${input}))`
return `${addStartCommand}JSON.stringify(${input})${addEndCommand}`
default:
throw new Error(`${schema.type} unsupported`)
}
Expand All @@ -789,12 +791,12 @@ function buildConstSerializer (location, input) {
if (hasNullType) {
code += `
if (${input} === null) {
${addCommand}('null')
${addStartCommand}'null'${addEndCommand}
} else {
`
}

code += `${addCommand}('${JSON.stringify(schema.const).replace(SINGLE_TICK, "\\'")}')`
code += `${addStartCommand}'${JSON.stringify(schema.const).replace(SINGLE_TICK, "\\'")}'${addEndCommand}`

if (hasNullType) {
code += `
Expand Down Expand Up @@ -962,7 +964,7 @@ function buildValue (context, location, input) {
let schema = location.schema

if (typeof schema === 'boolean') {
return `${addCommand}(JSON.stringify(${input}))`
return `${addStartCommand}JSON.stringify(${input})${addEndCommand}`
}

if (schema.$ref) {
Expand Down Expand Up @@ -996,7 +998,7 @@ function buildValue (context, location, input) {
if (nullable) {
code += `
if (${input} === null) {
${addCommand}('null')
${addStartCommand}'null'${addEndCommand}
} else {
`
}
Expand Down

0 comments on commit ee2f4da

Please sign in to comment.