Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

float is not parsed properly #97

Closed
imagecmos opened this issue Mar 11, 2019 · 6 comments
Closed

float is not parsed properly #97

imagecmos opened this issue Mar 11, 2019 · 6 comments

Comments

@imagecmos
Copy link

my local version is 1.4.0
the case like this
YAML.parse("foo: !!float 3.0")
YAML.parse("foo: 3.0")
the result always is
{ foo: 3 }
the right result is not { foo: 3.0 }?

@eemeli
Copy link
Owner

eemeli commented Mar 11, 2019

Could you clarify how you think that the JS representation of the value 3.0 should differ from its representation of the value 3?

@imagecmos
Copy link
Author

Could you clarify how you think that the JS representation of the value 3.0 should differ from its representation of the value 3?

we have a web page, users can edit their config then push it to target path
so we want the content of the config is the same as what users input

@eemeli
Copy link
Owner

eemeli commented Mar 11, 2019

The answer I was intending to give here was to use YAML.parseDocument() rather than YAML.parse(), but it turns out that the !!float tag is lost even then. That probably shouldn't happen. I'll test a bit more, and possibly tweak the number stringifier to keep the tag.

@eemeli
Copy link
Owner

eemeli commented Mar 12, 2019

Actually, I'll need to refactor the tag object a bit to accommodate this.

@eemeli
Copy link
Owner

eemeli commented Mar 14, 2019

With the most recent change, we get this:

const doc = YAML.parseDocument('foo: !!float 3.0') 
String(doc) === 'foo: !!float 3\n'

Effectively, I've made it so that explicit tags in the source correspond to explicit tags in the AST nodes, both when parsing as well as stringifying.

I'm considering what -- if anything -- to do about the .0 part. For instance, if you first parsed 3.0 as a float and then updated its value to 30, would you expect it to be stringified as 30 or as 30.0? Similarly, if you parsed 3.10 and updated its value to 30, how should that be stringified?

@eemeli
Copy link
Owner

eemeli commented Mar 28, 2019

Closing this as fixed. When a non-scientific float is now parsed, if its value has 0 as the last fraction digit, it sets a property minFractionDigits for the resolved node, which is then used during stringification. So if you update a value that was parsed from 3.0 to 30, it'll be stringified as 30.0:

const doc = YAML.parseDocument('foo: !!float 3.0\nbar: 4.20e3')
// In order to keep the explicit tag & minFractionDigits we need to fetch and
// modify the original node, rather than using set() to overwrite its value.
const float = doc.get('foo', true)
float.value = 30
String(doc)
// foo: !!float 30.0
// bar: 4.2e+3

Setting minFractionDigits on a scalar node with a Number value will control the string output, provided that its format is empty and its tag is either empty, or set to 'tag:yaml.org,2002:float'.

Exponential values now keep that form, but their fraction digits aren't remembered. This is due to needing to use Number#toExponential() to stringify them, as Intl.NumberFormat doesn't yet support them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants