fast-json-stringify 2.7.7 → 2.7.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,7 @@ jobs:
22
22
  - uses: actions/checkout@v2.3.4
23
23
 
24
24
  - name: Use Node.js
25
- uses: actions/setup-node@v2.1.5
25
+ uses: actions/setup-node@v2.3.0
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
28
 
@@ -38,6 +38,6 @@ jobs:
38
38
  needs: test
39
39
  runs-on: ubuntu-latest
40
40
  steps:
41
- - uses: fastify/github-action-merge-dependabot@v2.1.1
41
+ - uses: fastify/github-action-merge-dependabot@v2.2.0
42
42
  with:
43
43
  github-token: ${{ secrets.GITHUB_TOKEN }}
package/index.js CHANGED
@@ -1023,9 +1023,11 @@ function buildArray (location, code, name, key = null) {
1023
1023
 
1024
1024
  code += `
1025
1025
  var l = obj.length
1026
- var w = l - 1
1026
+
1027
1027
  for (var i = 0; i < l; i++) {
1028
- if (i > 0) {
1028
+ var jsonLastChar = json[json.length - 1]
1029
+
1030
+ if (i > 0 && jsonLastChar !== '[' && jsonLastChar !== ',') {
1029
1031
  json += ','
1030
1032
  }
1031
1033
  ${result.code}
@@ -1214,9 +1216,12 @@ function nested (laterCode, name, key, location, subKey, isArray) {
1214
1216
  `
1215
1217
  laterCode = nestedResult.laterCode
1216
1218
  })
1217
- code += `
1218
- else json+= null
1219
- `
1219
+
1220
+ if (!isArray) {
1221
+ code += `
1222
+ else json+= null
1223
+ `
1224
+ }
1220
1225
  } else if (isEmpty(schema)) {
1221
1226
  code += `
1222
1227
  json += JSON.stringify(obj${accessor})
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "2.7.7",
3
+ "version": "2.7.8",
4
4
  "description": "Stringify your JSON at max speed",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -401,3 +401,105 @@ test('oneOf object with field of type string with format or null', (t) => {
401
401
  prop: toStringify
402
402
  }), `{"prop":"${toStringify.toISOString()}"}`)
403
403
  })
404
+
405
+ test('one array item match oneOf types', (t) => {
406
+ t.plan(1)
407
+
408
+ const schema = {
409
+ type: 'object',
410
+ additionalProperties: false,
411
+ required: ['data'],
412
+ properties: {
413
+ data: {
414
+ type: 'array',
415
+ minItems: 1,
416
+ items: {
417
+ oneOf: [
418
+ {
419
+ type: 'string'
420
+ },
421
+ {
422
+ type: 'number'
423
+ }
424
+ ]
425
+ }
426
+ }
427
+ }
428
+ }
429
+
430
+ const stringify = build(schema)
431
+
432
+ const responseWithMappedType = stringify({
433
+ data: [false, 'foo']
434
+ })
435
+
436
+ t.equal('{"data":["foo"]}', responseWithMappedType)
437
+ })
438
+
439
+ test('some array items match oneOf types', (t) => {
440
+ t.plan(1)
441
+
442
+ const schema = {
443
+ type: 'object',
444
+ additionalProperties: false,
445
+ required: ['data'],
446
+ properties: {
447
+ data: {
448
+ type: 'array',
449
+ minItems: 1,
450
+ items: {
451
+ oneOf: [
452
+ {
453
+ type: 'string'
454
+ },
455
+ {
456
+ type: 'number'
457
+ }
458
+ ]
459
+ }
460
+ }
461
+ }
462
+ }
463
+
464
+ const stringify = build(schema)
465
+
466
+ const responseWithMappedTypes = stringify({
467
+ data: [false, 'foo', true, 5]
468
+ })
469
+
470
+ t.equal('{"data":["foo",5]}', responseWithMappedTypes)
471
+ })
472
+
473
+ test('all array items does not match oneOf types', (t) => {
474
+ t.plan(1)
475
+
476
+ const schema = {
477
+ type: 'object',
478
+ additionalProperties: false,
479
+ required: ['data'],
480
+ properties: {
481
+ data: {
482
+ type: 'array',
483
+ minItems: 1,
484
+ items: {
485
+ oneOf: [
486
+ {
487
+ type: 'string'
488
+ },
489
+ {
490
+ type: 'number'
491
+ }
492
+ ]
493
+ }
494
+ }
495
+ }
496
+ }
497
+
498
+ const stringify = build(schema)
499
+
500
+ const emptyResponse = stringify({
501
+ data: [null, false, true, undefined, [], {}]
502
+ })
503
+
504
+ t.equal('{"data":[]}', emptyResponse)
505
+ })