Skip to content

Commit

Permalink
fix: Do not parse -.NaN or +.nan as NaN (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Jun 2, 2024
1 parent 69f3517 commit edc623d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/schema/core/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const floatNaN: ScalarTag = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^(?:[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN))$/,
test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
resolve: str =>
str.slice(-3).toLowerCase() === 'nan'
? NaN
Expand Down
2 changes: 1 addition & 1 deletion src/schema/yaml-1.1/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const floatNaN: ScalarTag = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^[-+]?\.(?:inf|Inf|INF|nan|NaN|NAN)$/,
test: /^(?:[-+]?\.(?:inf|Inf|INF)|\.nan|\.NaN|\.NAN)$/,
resolve: (str: string) =>
str.slice(-3).toLowerCase() === 'nan'
? NaN
Expand Down
18 changes: 12 additions & 6 deletions tests/doc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,22 @@ option: TruE\n`)
const src = `canonical: 6.8523015e+5
fixed: 685230.15
negative infinity: -.inf
not a number: .NaN`
not a number: .NaN
not a NaN: -.NaN`

const doc = parseDocument(src)
expect(doc.toJS()).toMatchObject({
canonical: 685230.15,
fixed: 685230.15,
'negative infinity': Number.NEGATIVE_INFINITY,
'not a number': NaN
'not a number': NaN,
'not a NaN': '-.NaN'
})
expect(String(doc)).toBe(`canonical: 6.8523015e+5
fixed: 685230.15
negative infinity: -.inf
not a number: .nan\n`)
not a number: .nan
not a NaN: -.NaN\n`)
})

test('!!int', () => {
Expand Down Expand Up @@ -546,7 +549,8 @@ exponential: 685.230_15e+03
fixed: 685_230.15
sexagesimal: 190:20:30.15
negative infinity: -.inf
not a number: .NaN`
not a number: .NaN
not a NaN: -.NaN`

const doc = parseDocument(src)
expect(doc.toJS()).toMatchObject({
Expand All @@ -555,7 +559,8 @@ not a number: .NaN`
fixed: 685230.15,
sexagesimal: 685230.15,
'negative infinity': Number.NEGATIVE_INFINITY,
'not a number': NaN
'not a number': NaN,
'not a NaN': '-.NaN'
})
expect(String(doc)).toBe(`%YAML 1.1
---
Expand All @@ -564,7 +569,8 @@ exponential: 6.8523015e+5
fixed: 685230.15
sexagesimal: 190:20:30.15
negative infinity: -.inf
not a number: .nan\n`)
not a number: .nan
not a NaN: -.NaN\n`)
})

test('!!int', () => {
Expand Down

0 comments on commit edc623d

Please sign in to comment.