@@ -540,12 +540,13 @@ function buildArray (context, location) {
540
540
}
541
541
542
542
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`
544
544
}
545
545
546
546
functionCode += `
547
+ const arrayEnd = arrayLength - 1
547
548
let value
548
- let jsonOutput = ''
549
+ let json = ''
549
550
`
550
551
551
552
if ( Array . isArray ( itemsSchema ) ) {
@@ -556,11 +557,9 @@ function buildArray (context, location) {
556
557
functionCode += `
557
558
if (${ i } < arrayLength) {
558
559
if (${ buildArrayTypeCondition ( item . type , `[${ i } ]` ) } ) {
559
- let json = ''
560
560
${ tmpRes }
561
- jsonOutput += json
562
- if (${ i } < arrayLength - 1) {
563
- jsonOutput += ','
561
+ if (${ i } < arrayEnd) {
562
+ json += ','
564
563
}
565
564
} else {
566
565
throw new Error(\`Item at ${ i } does not match schema definition.\`)
@@ -572,27 +571,25 @@ function buildArray (context, location) {
572
571
if ( schema . additionalItems ) {
573
572
functionCode += `
574
573
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 += ','
578
577
}
579
578
}`
580
579
}
581
580
} else {
582
581
const code = buildValue ( context , itemsLocation , 'obj[i]' )
583
582
functionCode += `
584
583
for (let i = 0; i < arrayLength; i++) {
585
- let json = ''
586
584
${ code }
587
- jsonOutput += json
588
- if (i < arrayLength - 1) {
589
- jsonOutput += ','
585
+ if (i < arrayEnd) {
586
+ json += ','
590
587
}
591
588
}`
592
589
}
593
590
594
591
functionCode += `
595
- return \`[\${jsonOutput }]\`
592
+ return \`[\${json }]\`
596
593
}`
597
594
598
595
context . functions . push ( functionCode )
0 commit comments