Skip to content

Commit 76ca3d2

Browse files
committed
Automatically set minFractionDigits during parse if input has trailing zeros (#97)
1 parent 6880d1f commit 76ca3d2

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/tags/core.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,13 @@ export default failsafe.concat([
7777
identify: value => typeof value === 'number',
7878
default: true,
7979
tag: 'tag:yaml.org,2002:float',
80-
test: /^[-+]?(?:0|[1-9][0-9]*)\.[0-9]*$/,
81-
resolve: str => parseFloat(str),
80+
test: /^[-+]?(?:0|[1-9][0-9]*)\.([0-9]*)$/,
81+
resolve(str, frac) {
82+
const node = new Scalar(parseFloat(str))
83+
if (frac && frac[frac.length - 1] === '0')
84+
node.minFractionDigits = frac.length
85+
return node
86+
},
8287
stringify: stringifyNumber
8388
}
8489
])

src/tags/yaml-1.1/index.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,15 @@ export default failsafe.concat(
102102
identify: value => typeof value === 'number',
103103
default: true,
104104
tag: 'tag:yaml.org,2002:float',
105-
test: /^[-+]?([0-9][0-9_]*)?\.[0-9_]*$/,
106-
resolve: str => parseFloat(str.replace(/_/g, '')),
105+
test: /^[-+]?(?:[0-9][0-9_]*)?\.([0-9_]*)$/,
106+
resolve(str, frac) {
107+
const node = new Scalar(parseFloat(str.replace(/_/g, '')))
108+
if (frac) {
109+
const f = frac.replace(/_/g, '')
110+
if (f[f.length - 1] === '0') node.minFractionDigits = f.length
111+
}
112+
return node
113+
},
107114
stringify: stringifyNumber
108115
}
109116
],

tests/doc/parse.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe('tags', () => {
7171

7272
test('eemeli/yaml#97', () => {
7373
const doc = YAML.parseDocument('foo: !!float 3.0')
74-
expect(String(doc)).toBe('foo: !!float 3\n')
74+
expect(String(doc)).toBe('foo: !!float 3.0\n')
7575
})
7676
})
7777

@@ -84,6 +84,7 @@ describe('number types', () => {
8484
- 123_456
8585
- 3.1e+2
8686
- 5.1_2_3E-1
87+
- 4.02
8788
- 4.20`
8889
const doc = YAML.parseDocument(src, { version: '1.1' })
8990
expect(doc.contents.items).toMatchObject([
@@ -93,10 +94,13 @@ describe('number types', () => {
9394
{ value: 123456 },
9495
{ value: 310, format: 'EXP' },
9596
{ value: 0.5123, format: 'EXP' },
96-
{ value: 4.2 }
97+
{ value: 4.02 },
98+
{ value: 4.2, minFractionDigits: 2 }
9799
])
98100
expect(doc.contents.items[3]).not.toHaveProperty('format')
99101
expect(doc.contents.items[6]).not.toHaveProperty('format')
102+
expect(doc.contents.items[6]).not.toHaveProperty('minFractionDigits')
103+
expect(doc.contents.items[7]).not.toHaveProperty('format')
100104
})
101105

102106
test('Version 1.2', () => {
@@ -106,6 +110,7 @@ describe('number types', () => {
106110
- 123456
107111
- 3.1e+2
108112
- 5.123E-1
113+
- 4.02
109114
- 4.20`
110115
const doc = YAML.parseDocument(src, { version: '1.2' })
111116
expect(doc.contents.items).toMatchObject([
@@ -114,10 +119,13 @@ describe('number types', () => {
114119
{ value: 123456 },
115120
{ value: 310, format: 'EXP' },
116121
{ value: 0.5123, format: 'EXP' },
117-
{ value: 4.2 }
122+
{ value: 4.02 },
123+
{ value: 4.2, minFractionDigits: 2 }
118124
])
119125
expect(doc.contents.items[2]).not.toHaveProperty('format')
120126
expect(doc.contents.items[5]).not.toHaveProperty('format')
127+
expect(doc.contents.items[5]).not.toHaveProperty('minFractionDigits')
128+
expect(doc.contents.items[6]).not.toHaveProperty('format')
121129
})
122130
})
123131

0 commit comments

Comments
 (0)