fast-json-stringify 5.0.2 → 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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-json-stringify",
3
- "version": "5.0.2",
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",
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
@@ -48,8 +48,10 @@ module.exports = class Serializer {
48
48
 
49
49
  asNumber (i) {
50
50
  const num = Number(i)
51
- if (Number.isNaN(num) || !Number.isFinite(num)) {
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
  }
@@ -4,7 +4,9 @@ const test = require('tap').test
4
4
  const build = require('..')
5
5
 
6
6
  test('Finite numbers', t => {
7
- const values = [-5, 0, -0, 1.33, Math.E, Number.EPSILON, Number.MAX_SAFE_INTEGER, Number.MAX_VALUE,
7
+ const values = [-5, 0, -0, 1.33, 99, 100.0,
8
+ Math.E, Number.EPSILON,
9
+ Number.MAX_SAFE_INTEGER, Number.MAX_VALUE,
8
10
  Number.MIN_SAFE_INTEGER, Number.MIN_VALUE]
9
11
 
10
12
  t.plan(values.length)
@@ -18,13 +20,13 @@ test('Finite numbers', t => {
18
20
  values.forEach(v => t.equal(stringify(v), JSON.stringify(v)))
19
21
  })
20
22
 
21
- test('Infinite numbers', t => {
23
+ test('Infinite integers', t => {
22
24
  const values = [Infinity, -Infinity]
23
25
 
24
26
  t.plan(values.length)
25
27
 
26
28
  const schema = {
27
- type: 'number'
29
+ type: 'integer'
28
30
  }
29
31
 
30
32
  const stringify = build(schema)
@@ -33,7 +35,21 @@ test('Infinite numbers', t => {
33
35
  try {
34
36
  stringify(v)
35
37
  } catch (err) {
36
- t.equal(err.message, `The value "${v}" cannot be converted to a number.`)
38
+ t.equal(err.message, `The value "${v}" cannot be converted to an integer.`)
37
39
  }
38
40
  })
39
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
+ })