fast-json-stringify 5.0.1 → 5.0.2

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
@@ -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.1",
3
+ "version": "5.0.2",
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
@@ -48,7 +48,7 @@ module.exports = class Serializer {
48
48
 
49
49
  asNumber (i) {
50
50
  const num = Number(i)
51
- if (Number.isNaN(num)) {
51
+ if (Number.isNaN(num) || !Number.isFinite(num)) {
52
52
  throw new Error(`The value "${i}" cannot be converted to a number.`)
53
53
  } else {
54
54
  return '' + num
@@ -0,0 +1,39 @@
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, Math.E, Number.EPSILON, Number.MAX_SAFE_INTEGER, Number.MAX_VALUE,
8
+ Number.MIN_SAFE_INTEGER, Number.MIN_VALUE]
9
+
10
+ t.plan(values.length)
11
+
12
+ const schema = {
13
+ type: 'number'
14
+ }
15
+
16
+ const stringify = build(schema)
17
+
18
+ values.forEach(v => t.equal(stringify(v), JSON.stringify(v)))
19
+ })
20
+
21
+ test('Infinite numbers', t => {
22
+ const values = [Infinity, -Infinity]
23
+
24
+ t.plan(values.length)
25
+
26
+ const schema = {
27
+ type: 'number'
28
+ }
29
+
30
+ const stringify = build(schema)
31
+
32
+ values.forEach(v => {
33
+ try {
34
+ stringify(v)
35
+ } catch (err) {
36
+ t.equal(err.message, `The value "${v}" cannot be converted to a number.`)
37
+ }
38
+ })
39
+ })
@@ -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
+ })