Skip to content

Commit

Permalink
Fix parsing of floats like 00.4 in core schema (Fixes #144, again)
Browse files Browse the repository at this point in the history
  • Loading branch information
eemeli committed Mar 21, 2020
1 parent 61b45d4 commit 3a23187
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/tags/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const expObj = {
default: true,
tag: 'tag:yaml.org,2002:float',
format: 'EXP',
test: /^[-+]?(?:\.[0-9]+|(?:0|[1-9][0-9]*)(\.[0-9]*)?)[eE][-+]?[0-9]+$/,
test: /^[-+]?(?:\.[0-9]+|[0-9]+(?:\.[0-9]*)?)[eE][-+]?[0-9]+$/,
resolve: str => parseFloat(str),
stringify: ({ value }) => Number(value).toExponential()
}
Expand All @@ -82,7 +82,7 @@ export const floatObj = {
identify: value => typeof value === 'number',
default: true,
tag: 'tag:yaml.org,2002:float',
test: /^[-+]?(?:\.([0-9]+)|(?:0|[1-9][0-9]*)\.([0-9]*))$/,
test: /^[-+]?(?:\.([0-9]+)|[0-9]+\.([0-9]*))$/,
resolve(str, frac1, frac2) {
const frac = frac1 || frac2
const node = new Scalar(parseFloat(str))
Expand Down
12 changes: 8 additions & 4 deletions tests/doc/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ describe('number types', () => {
- 5.1_2_3E-1
- 4.02
- 4.20
- .42`
- .42
- 00.4`
const doc = YAML.parseDocument(src, { version: '1.1' })
expect(doc.contents.items).toMatchObject([
{ value: 10, format: 'BIN' },
Expand All @@ -97,7 +98,8 @@ describe('number types', () => {
{ value: 0.5123, format: 'EXP' },
{ value: 4.02 },
{ value: 4.2, minFractionDigits: 2 },
{ value: 0.42 }
{ value: 0.42 },
{ value: 0.4 }
])
expect(doc.contents.items[3]).not.toHaveProperty('format')
expect(doc.contents.items[6]).not.toHaveProperty('format')
Expand All @@ -114,7 +116,8 @@ describe('number types', () => {
- 5.123E-1
- 4.02
- 4.20
- .42`
- .42
- 00.4`
const doc = YAML.parseDocument(src, { version: '1.2' })
expect(doc.contents.items).toMatchObject([
{ value: 83, format: 'OCT' },
Expand All @@ -124,7 +127,8 @@ describe('number types', () => {
{ value: 0.5123, format: 'EXP' },
{ value: 4.02 },
{ value: 4.2, minFractionDigits: 2 },
{ value: 0.42 }
{ value: 0.42 },
{ value: 0.4 }
])
expect(doc.contents.items[2]).not.toHaveProperty('format')
expect(doc.contents.items[5]).not.toHaveProperty('format')
Expand Down

0 comments on commit 3a23187

Please sign in to comment.