fast-json-stringify 5.0.0 → 5.0.3

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.
@@ -13,5 +13,3 @@ on:
13
13
  jobs:
14
14
  test:
15
15
  uses: fastify/workflows/.github/workflows/plugins-ci.yml@v3
16
- with:
17
- auto-merge-exclude: 'chalk'
@@ -2,8 +2,7 @@
2
2
 
3
3
  const { spawn } = require('child_process')
4
4
 
5
- const chalk = require('chalk')
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
- const result = await inquirer.prompt([{
19
+ console.log(message)
20
+ const result = await cliSelect({
17
21
  type: 'list',
18
22
  name: 'branch',
19
- choices: branches,
20
- loop: false,
21
- pageSize: 20,
22
- message
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(chalk.grey(`Checking out "${branch}"`))
30
+ console.log(`${greyColor}Checking out "${branch}"${resetColor}`)
29
31
  await git.checkout(branch)
30
32
 
31
- console.log(chalk.grey(`Execute "${command}"`))
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(chalk.green(message))
79
+ console.log(`${greenColor}${message}${resetColor}`)
78
80
  } else if (roundedPercent < -PERCENT_THRESHOLD) {
79
- console.log(chalk.red(message))
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(chalk.gray(`Back to ${currentBranch.name} ${currentBranch.commit}`))
115
+ console.log(`${greyColor}Back to ${currentBranch.name} ${currentBranch.commit}${resetColor}`)
114
116
  })()
package/index.js CHANGED
@@ -102,10 +102,10 @@ function build (schema, options) {
102
102
  ajvInstance.addSchema(schema, rootSchemaId)
103
103
 
104
104
  if (options.schema) {
105
- options.schema = clone(options.schema)
105
+ const externalSchemas = clone(options.schema)
106
106
 
107
- for (const key of Object.keys(options.schema)) {
108
- const externalSchema = options.schema[key]
107
+ for (const key of Object.keys(externalSchemas)) {
108
+ const externalSchema = externalSchemas[key]
109
109
  isValidSchema(externalSchema, key)
110
110
  extendDateTimeType(externalSchema)
111
111
 
@@ -297,7 +297,7 @@ function addPatternProperties (location) {
297
297
  }
298
298
 
299
299
  function additionalProperty (location) {
300
- let ap = location.schema.additionalProperties
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 (ap.$ref) {
314
- apLocation = resolveRef(location, ap.$ref)
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]]')
@@ -523,8 +523,9 @@ function addIfThenElse (location) {
523
523
 
524
524
  if (thenSchema.if && thenSchema.then) {
525
525
  code += addIfThenElse(thenLocation)
526
+ } else {
527
+ code += buildInnerObject(thenLocation)
526
528
  }
527
- code += buildInnerObject(thenLocation)
528
529
  code += `
529
530
  }
530
531
  `
@@ -538,8 +539,9 @@ function addIfThenElse (location) {
538
539
 
539
540
  if (elseSchema.if && elseSchema.then) {
540
541
  code += addIfThenElse(elseLocation)
542
+ } else {
543
+ code += buildInnerObject(elseLocation)
541
544
  }
542
- code += buildInnerObject(elseLocation)
543
545
  code += `
544
546
  }
545
547
  `
@@ -582,15 +584,13 @@ function buildObject (location) {
582
584
  var addComma = false
583
585
  `
584
586
 
585
- let rCode
586
587
  if (schema.if && schema.then) {
587
- rCode = addIfThenElse(location)
588
+ functionCode += addIfThenElse(location)
588
589
  } else {
589
- rCode = buildInnerObject(location)
590
+ functionCode += buildInnerObject(location)
590
591
  }
591
592
 
592
- // Removes the comma if is the last element of the string (in case there are not properties)
593
- functionCode += `${rCode}
593
+ functionCode += `
594
594
  json += '}'
595
595
  return json
596
596
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "5.0.0",
3
+ "version": "5.0.3",
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
- "chalk": "^4.1.2",
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
+ })
package/test/ref.test.js CHANGED
@@ -1,5 +1,7 @@
1
1
  'use strict'
2
2
 
3
+ const clone = require('rfdc')({ proto: true })
4
+
3
5
  const test = require('tap').test
4
6
  const build = require('..')
5
7
 
@@ -1822,3 +1824,70 @@ test('ref with same id in properties', (t) => {
1822
1824
  t.equal(output, '{"_id":"foo","image":{"_id":"bar","name":"hello","owner":"baz"}}')
1823
1825
  })
1824
1826
  })
1827
+
1828
+ test('Should not modify external schemas', (t) => {
1829
+ t.plan(2)
1830
+
1831
+ const externalSchema = {
1832
+ uuid: {
1833
+ format: 'uuid',
1834
+ $id: 'UUID',
1835
+ type: 'string'
1836
+ },
1837
+ Entity: {
1838
+ $id: 'Entity',
1839
+ type: 'object',
1840
+ properties: {
1841
+ id: { $ref: 'UUID' },
1842
+ id2: { $ref: 'UUID' }
1843
+ }
1844
+ }
1845
+ }
1846
+
1847
+ const options = { schema: externalSchema }
1848
+ const optionsClone = clone(options)
1849
+
1850
+ const stringify = build({ $ref: 'Entity' }, options)
1851
+
1852
+ const data = { id: 'a4e4c954-9f5f-443a-aa65-74d95732249a' }
1853
+ const output = stringify(data)
1854
+
1855
+ t.equal(output, JSON.stringify(data))
1856
+ t.same(options, optionsClone)
1857
+ })
1858
+
1859
+ test('input schema is not mutated', (t) => {
1860
+ t.plan(3)
1861
+
1862
+ const schema = {
1863
+ title: 'object with $ref',
1864
+ type: 'object',
1865
+ definitions: {
1866
+ def: { type: 'string' }
1867
+ },
1868
+ properties: {
1869
+ obj: {
1870
+ $ref: '#/definitions/def'
1871
+ }
1872
+ }
1873
+ }
1874
+
1875
+ const clonedSchema = JSON.parse(JSON.stringify(schema))
1876
+
1877
+ const object = {
1878
+ obj: 'test'
1879
+ }
1880
+
1881
+ const stringify = build(schema)
1882
+ const output = stringify(object)
1883
+
1884
+ try {
1885
+ JSON.parse(output)
1886
+ t.pass()
1887
+ } catch (e) {
1888
+ t.fail()
1889
+ }
1890
+
1891
+ t.equal(output, '{"obj":"test"}')
1892
+ t.same(schema, clonedSchema)
1893
+ })