@@ -540,12 +540,13 @@ function buildArray (context, location) {
540540 }
541541
542542 if ( largeArrayMechanism === 'json-stringify' ) {
543- functionCode += `if (arrayLength && arrayLength >= ${ largeArraySize } ) return JSON.stringify(obj)\n`
543+ functionCode += `if (arrayLength >= ${ largeArraySize } ) return JSON.stringify(obj)\n`
544544 }
545545
546546 functionCode += `
547+ const arrayEnd = arrayLength - 1
547548 let value
548- let jsonOutput = ''
549+ let json = ''
549550 `
550551
551552 if ( Array . isArray ( itemsSchema ) ) {
@@ -556,11 +557,9 @@ function buildArray (context, location) {
556557 functionCode += `
557558 if (${ i } < arrayLength) {
558559 if (${ buildArrayTypeCondition ( item . type , `[${ i } ]` ) } ) {
559- let json = ''
560560 ${ tmpRes }
561- jsonOutput += json
562- if (${ i } < arrayLength - 1) {
563- jsonOutput += ','
561+ if (${ i } < arrayEnd) {
562+ json += ','
564563 }
565564 } else {
566565 throw new Error(\`Item at ${ i } does not match schema definition.\`)
@@ -572,27 +571,25 @@ function buildArray (context, location) {
572571 if ( schema . additionalItems ) {
573572 functionCode += `
574573 for (let i = ${ itemsSchema . length } ; i < arrayLength; i++) {
575- jsonOutput += JSON.stringify(obj[i])
576- if (i < arrayLength - 1 ) {
577- jsonOutput += ','
574+ json += JSON.stringify(obj[i])
575+ if (i < arrayEnd ) {
576+ json += ','
578577 }
579578 }`
580579 }
581580 } else {
582581 const code = buildValue ( context , itemsLocation , 'obj[i]' )
583582 functionCode += `
584583 for (let i = 0; i < arrayLength; i++) {
585- let json = ''
586584 ${ code }
587- jsonOutput += json
588- if (i < arrayLength - 1) {
589- jsonOutput += ','
585+ if (i < arrayEnd) {
586+ json += ','
590587 }
591588 }`
592589 }
593590
594591 functionCode += `
595- return \`[\${jsonOutput }]\`
592+ return \`[\${json }]\`
596593 }`
597594
598595 context . functions . push ( functionCode )
0 commit comments