Skip to content

Commit 5fde9b6

Browse files
authored
perf: avoid other addComma
Signed-off-by: francesco <[email protected]>
1 parent 4f884f2 commit 5fde9b6

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ function buildInnerObject (context, location) {
369369
code += 'let json = JSON_STR_BEGIN_OBJECT\n'
370370

371371
let addComma = ''
372-
if (!hasRequiredProperties && (propertiesKeys.length > 1 || (schema.patternProperties || schema.additionalProperties))) {
372+
const hasComma = !hasRequiredProperties && (propertiesKeys.length > 1 || (schema.patternProperties || schema.additionalProperties))
373+
if (hasComma) {
373374
code += 'let addComma = false\n'
374375
addComma = '!addComma && (addComma = true) || (json += JSON_STR_COMMA)'
375376
}
@@ -387,14 +388,14 @@ function buildInnerObject (context, location) {
387388
code += `
388389
value = obj[${sanitizedKey}]
389390
if (value !== undefined) {
390-
${addComma}
391+
${i > 0 ? addComma : (hasComma ? 'addComma = true' : '')}
391392
json += ${JSON.stringify(sanitizedKey + ':')}
392393
${buildValue(context, propertyLocation, 'value')}
393394
}`
394395

395396
if (defaultValue !== undefined) {
396397
code += ` else {
397-
${addComma}
398+
${i > 0 ? addComma : (hasComma ? 'addComma = true' : '')}
398399
json += ${JSON.stringify(sanitizedKey + ':' + JSON.stringify(defaultValue))}
399400
}
400401
`

0 commit comments

Comments
 (0)