Skip to content

Commit 34b9efc

Browse files
yaki3355Yaki Dori
andauthored
fix: infinity value throws (#476)
* serializer.js - asNumber * Infinity.test.js Co-authored-by: Yaki Dori <[email protected]>
1 parent a93bb18 commit 34b9efc

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

serializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ module.exports = class Serializer {
4848

4949
asNumber (i) {
5050
const num = Number(i)
51-
if (Number.isNaN(num)) {
51+
if (Number.isNaN(num) || !Number.isFinite(num)) {
5252
throw new Error(`The value "${i}" cannot be converted to a number.`)
5353
} else {
5454
return '' + num

test/Infinity.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
})

0 commit comments

Comments
 (0)