fast-json-stringify 5.0.1 → 5.0.4
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.
- package/.github/workflows/ci.yml +0 -2
- package/benchmark/bench-cmp-branch.js +16 -14
- package/index.js +15 -11
- package/package.json +2 -3
- package/serializer.js +3 -1
- package/test/if-then-else.test.js +35 -0
- package/test/infinity.test.js +55 -0
- package/test/issue-479.test.js +57 -0
package/.github/workflows/ci.yml
CHANGED
|
@@ -2,8 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
const { spawn } = require('child_process')
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
const inquirer = require('inquirer')
|
|
5
|
+
const cliSelect = require('cli-select')
|
|
7
6
|
const simpleGit = require('simple-git')
|
|
8
7
|
|
|
9
8
|
const git = simpleGit(process.cwd())
|
|
@@ -11,24 +10,27 @@ const git = simpleGit(process.cwd())
|
|
|
11
10
|
const COMMAND = 'npm run bench'
|
|
12
11
|
const DEFAULT_BRANCH = 'master'
|
|
13
12
|
const PERCENT_THRESHOLD = 5
|
|
13
|
+
const greyColor = '\x1b[30m'
|
|
14
|
+
const redColor = '\x1b[31m'
|
|
15
|
+
const greenColor = '\x1b[32m'
|
|
16
|
+
const resetColor = '\x1b[0m'
|
|
14
17
|
|
|
15
18
|
async function selectBranchName (message, branches) {
|
|
16
|
-
|
|
19
|
+
console.log(message)
|
|
20
|
+
const result = await cliSelect({
|
|
17
21
|
type: 'list',
|
|
18
22
|
name: 'branch',
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
}])
|
|
24
|
-
return result.branch
|
|
23
|
+
values: branches
|
|
24
|
+
})
|
|
25
|
+
console.log(result.value)
|
|
26
|
+
return result.value
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
async function executeCommandOnBranch (command, branch) {
|
|
28
|
-
console.log(
|
|
30
|
+
console.log(`${greyColor}Checking out "${branch}"${resetColor}`)
|
|
29
31
|
await git.checkout(branch)
|
|
30
32
|
|
|
31
|
-
console.log(
|
|
33
|
+
console.log(`${greyColor}Execute "${command}"${resetColor}`)
|
|
32
34
|
const childProcess = spawn(command, { stdio: 'pipe', shell: true })
|
|
33
35
|
|
|
34
36
|
let result = ''
|
|
@@ -74,9 +76,9 @@ function compareResults (featureBranch, mainBranch) {
|
|
|
74
76
|
const message = alignedName + percentString.padStart(7, '.')
|
|
75
77
|
|
|
76
78
|
if (roundedPercent > PERCENT_THRESHOLD) {
|
|
77
|
-
console.log(
|
|
79
|
+
console.log(`${greenColor}${message}${resetColor}`)
|
|
78
80
|
} else if (roundedPercent < -PERCENT_THRESHOLD) {
|
|
79
|
-
console.log(
|
|
81
|
+
console.log(`${redColor}${message}${resetColor}`)
|
|
80
82
|
} else {
|
|
81
83
|
console.log(message)
|
|
82
84
|
}
|
|
@@ -110,5 +112,5 @@ function compareResults (featureBranch, mainBranch) {
|
|
|
110
112
|
await git.checkout(currentBranch.commit)
|
|
111
113
|
await git.checkout(currentBranch.name)
|
|
112
114
|
|
|
113
|
-
console.log(
|
|
115
|
+
console.log(`${greyColor}Back to ${currentBranch.name} ${currentBranch.commit}${resetColor}`)
|
|
114
116
|
})()
|
package/index.js
CHANGED
|
@@ -297,7 +297,7 @@ function addPatternProperties (location) {
|
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
function additionalProperty (location) {
|
|
300
|
-
|
|
300
|
+
const ap = location.schema.additionalProperties
|
|
301
301
|
let code = ''
|
|
302
302
|
if (ap === true) {
|
|
303
303
|
code += `
|
|
@@ -309,10 +309,10 @@ function additionalProperty (location) {
|
|
|
309
309
|
|
|
310
310
|
return code
|
|
311
311
|
}
|
|
312
|
+
|
|
312
313
|
let apLocation = mergeLocation(location, 'additionalProperties')
|
|
313
|
-
if (
|
|
314
|
-
apLocation = resolveRef(
|
|
315
|
-
ap = apLocation.schema
|
|
314
|
+
if (apLocation.schema.$ref) {
|
|
315
|
+
apLocation = resolveRef(apLocation, apLocation.schema.$ref)
|
|
316
316
|
}
|
|
317
317
|
|
|
318
318
|
const valueCode = buildValue(apLocation, 'obj[keys[i]]')
|
|
@@ -489,6 +489,10 @@ function mergeAllOfSchema (location, schema, mergedSchema) {
|
|
|
489
489
|
}
|
|
490
490
|
}
|
|
491
491
|
delete mergedSchema.allOf
|
|
492
|
+
|
|
493
|
+
mergedSchema.$id = `merged_${randomUUID()}`
|
|
494
|
+
ajvInstance.addSchema(mergedSchema)
|
|
495
|
+
location.schemaId = mergedSchema.$id
|
|
492
496
|
}
|
|
493
497
|
|
|
494
498
|
function buildInnerObject (location) {
|
|
@@ -523,8 +527,9 @@ function addIfThenElse (location) {
|
|
|
523
527
|
|
|
524
528
|
if (thenSchema.if && thenSchema.then) {
|
|
525
529
|
code += addIfThenElse(thenLocation)
|
|
530
|
+
} else {
|
|
531
|
+
code += buildInnerObject(thenLocation)
|
|
526
532
|
}
|
|
527
|
-
code += buildInnerObject(thenLocation)
|
|
528
533
|
code += `
|
|
529
534
|
}
|
|
530
535
|
`
|
|
@@ -538,8 +543,9 @@ function addIfThenElse (location) {
|
|
|
538
543
|
|
|
539
544
|
if (elseSchema.if && elseSchema.then) {
|
|
540
545
|
code += addIfThenElse(elseLocation)
|
|
546
|
+
} else {
|
|
547
|
+
code += buildInnerObject(elseLocation)
|
|
541
548
|
}
|
|
542
|
-
code += buildInnerObject(elseLocation)
|
|
543
549
|
code += `
|
|
544
550
|
}
|
|
545
551
|
`
|
|
@@ -582,15 +588,13 @@ function buildObject (location) {
|
|
|
582
588
|
var addComma = false
|
|
583
589
|
`
|
|
584
590
|
|
|
585
|
-
let rCode
|
|
586
591
|
if (schema.if && schema.then) {
|
|
587
|
-
|
|
592
|
+
functionCode += addIfThenElse(location)
|
|
588
593
|
} else {
|
|
589
|
-
|
|
594
|
+
functionCode += buildInnerObject(location)
|
|
590
595
|
}
|
|
591
596
|
|
|
592
|
-
|
|
593
|
-
functionCode += `${rCode}
|
|
597
|
+
functionCode += `
|
|
594
598
|
json += '}'
|
|
595
599
|
return json
|
|
596
600
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fast-json-stringify",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.4",
|
|
4
4
|
"description": "Stringify your JSON at max speed",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -35,9 +35,8 @@
|
|
|
35
35
|
"devDependencies": {
|
|
36
36
|
"@sinclair/typebox": "^0.23.3",
|
|
37
37
|
"benchmark": "^2.1.4",
|
|
38
|
-
"
|
|
38
|
+
"cli-select": "^1.1.2",
|
|
39
39
|
"compile-json-stringify": "^0.1.2",
|
|
40
|
-
"inquirer": "^9.0.0",
|
|
41
40
|
"is-my-json-valid": "^2.20.0",
|
|
42
41
|
"luxon": "^2.4.0",
|
|
43
42
|
"pre-commit": "^1.2.2",
|
package/serializer.js
CHANGED
|
@@ -34,7 +34,7 @@ module.exports = class Serializer {
|
|
|
34
34
|
} else {
|
|
35
35
|
/* eslint no-undef: "off" */
|
|
36
36
|
const integer = this.parseInteger(i)
|
|
37
|
-
if (Number.isNaN(integer)) {
|
|
37
|
+
if (Number.isNaN(integer) || !Number.isFinite(integer)) {
|
|
38
38
|
throw new Error(`The value "${i}" cannot be converted to an integer.`)
|
|
39
39
|
} else {
|
|
40
40
|
return '' + integer
|
|
@@ -50,6 +50,8 @@ module.exports = class Serializer {
|
|
|
50
50
|
const num = Number(i)
|
|
51
51
|
if (Number.isNaN(num)) {
|
|
52
52
|
throw new Error(`The value "${i}" cannot be converted to a number.`)
|
|
53
|
+
} else if (!Number.isFinite(num)) {
|
|
54
|
+
return null
|
|
53
55
|
} else {
|
|
54
56
|
return '' + num
|
|
55
57
|
}
|
|
@@ -333,3 +333,38 @@ t.test('if-then-else', t => {
|
|
|
333
333
|
|
|
334
334
|
t.end()
|
|
335
335
|
})
|
|
336
|
+
|
|
337
|
+
t.test('nested if/then', t => {
|
|
338
|
+
t.plan(2)
|
|
339
|
+
|
|
340
|
+
const schema = {
|
|
341
|
+
type: 'object',
|
|
342
|
+
properties: { a: { type: 'string' } },
|
|
343
|
+
if: {
|
|
344
|
+
type: 'object',
|
|
345
|
+
properties: { foo: { type: 'string' } }
|
|
346
|
+
},
|
|
347
|
+
then: {
|
|
348
|
+
properties: { bar: { type: 'string' } },
|
|
349
|
+
if: {
|
|
350
|
+
type: 'object',
|
|
351
|
+
properties: { foo1: { type: 'string' } }
|
|
352
|
+
},
|
|
353
|
+
then: {
|
|
354
|
+
properties: { bar1: { type: 'string' } }
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const stringify = build(schema)
|
|
360
|
+
|
|
361
|
+
t.equal(
|
|
362
|
+
stringify({ a: 'A', foo: 'foo', bar: 'bar' }),
|
|
363
|
+
JSON.stringify({ a: 'A', bar: 'bar' })
|
|
364
|
+
)
|
|
365
|
+
|
|
366
|
+
t.equal(
|
|
367
|
+
stringify({ a: 'A', foo: 'foo', bar: 'bar', foo1: 'foo1', bar1: 'bar1' }),
|
|
368
|
+
JSON.stringify({ a: 'A', bar: 'bar', bar1: 'bar1' })
|
|
369
|
+
)
|
|
370
|
+
})
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const test = require('tap').test
|
|
4
|
+
const build = require('..')
|
|
5
|
+
|
|
6
|
+
test('Finite numbers', t => {
|
|
7
|
+
const values = [-5, 0, -0, 1.33, 99, 100.0,
|
|
8
|
+
Math.E, Number.EPSILON,
|
|
9
|
+
Number.MAX_SAFE_INTEGER, Number.MAX_VALUE,
|
|
10
|
+
Number.MIN_SAFE_INTEGER, Number.MIN_VALUE]
|
|
11
|
+
|
|
12
|
+
t.plan(values.length)
|
|
13
|
+
|
|
14
|
+
const schema = {
|
|
15
|
+
type: 'number'
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const stringify = build(schema)
|
|
19
|
+
|
|
20
|
+
values.forEach(v => t.equal(stringify(v), JSON.stringify(v)))
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test('Infinite integers', t => {
|
|
24
|
+
const values = [Infinity, -Infinity]
|
|
25
|
+
|
|
26
|
+
t.plan(values.length)
|
|
27
|
+
|
|
28
|
+
const schema = {
|
|
29
|
+
type: 'integer'
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const stringify = build(schema)
|
|
33
|
+
|
|
34
|
+
values.forEach(v => {
|
|
35
|
+
try {
|
|
36
|
+
stringify(v)
|
|
37
|
+
} catch (err) {
|
|
38
|
+
t.equal(err.message, `The value "${v}" cannot be converted to an integer.`)
|
|
39
|
+
}
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
test('Infinite numbers', t => {
|
|
44
|
+
const values = [Infinity, -Infinity]
|
|
45
|
+
|
|
46
|
+
t.plan(values.length)
|
|
47
|
+
|
|
48
|
+
const schema = {
|
|
49
|
+
type: 'number'
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const stringify = build(schema)
|
|
53
|
+
|
|
54
|
+
values.forEach(v => t.equal(stringify(v), JSON.stringify(v)))
|
|
55
|
+
})
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const build = require('..')
|
|
5
|
+
|
|
6
|
+
test('should validate anyOf after allOf merge', (t) => {
|
|
7
|
+
t.plan(1)
|
|
8
|
+
|
|
9
|
+
const schema = {
|
|
10
|
+
$id: 'schema',
|
|
11
|
+
type: 'object',
|
|
12
|
+
allOf: [
|
|
13
|
+
{
|
|
14
|
+
$id: 'base',
|
|
15
|
+
type: 'object',
|
|
16
|
+
properties: {
|
|
17
|
+
name: {
|
|
18
|
+
type: 'string'
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
required: [
|
|
22
|
+
'name'
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
$id: 'inner_schema',
|
|
27
|
+
type: 'object',
|
|
28
|
+
properties: {
|
|
29
|
+
union: {
|
|
30
|
+
$id: '#id',
|
|
31
|
+
anyOf: [
|
|
32
|
+
{
|
|
33
|
+
|
|
34
|
+
$id: 'guid',
|
|
35
|
+
type: 'string'
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
|
|
39
|
+
$id: 'email',
|
|
40
|
+
type: 'string'
|
|
41
|
+
}
|
|
42
|
+
]
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
required: [
|
|
46
|
+
'union'
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const stringify = build(schema)
|
|
53
|
+
|
|
54
|
+
t.equal(
|
|
55
|
+
stringify({ name: 'foo', union: 'a8f1cc50-5530-5c62-9109-5ba9589a6ae1' }),
|
|
56
|
+
'{"name":"foo","union":"a8f1cc50-5530-5c62-9109-5ba9589a6ae1"}')
|
|
57
|
+
})
|