-
-
Notifications
You must be signed in to change notification settings - Fork 35.6k
doc: explain edge case when assigning port to url #19645
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
Changes from 1 commit
d719151
2cc61c1
257eae0
e402ff4
64863ee
aedadce
a55b432
0d39ca6
92919bc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -317,6 +317,15 @@ console.log(myURL.port); | |
| myURL.port = 1e10; | ||
| console.log(myURL.port); | ||
| // Prints 1234 | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this empty line seems redundant. |
||
| // Out-of-range numbers, which are converted to exponential | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is especially confusing given the previous example which says unconditionally that out-of-range numbers are ignored. Are we sure this behavior isn't simply a bug that needs fixing, rather than a defined behavior that needs documenting? It's hard to imagine anyone actually intentionally using this behavior.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree with both points. I didn't have time to go through the actual spec, however I was told it is indeed the case in: Obviously adding a range-check and some tests is a very straightforward solution for the unexpected behavior, but I'm not at all sure this is not a spec bug. In the meantime though, the documentation should perhaps be changed as it does not reflect the actual behavior expressed in node. |
||
| // notation via .toString(), will behave as strings with leading zeroes. | ||
| // This means that the port will be assigned the integer part of the coefficient, | ||
| // assuming the number is normalized (for example, 0.9e10 => 9e9). | ||
| // See https://www.ecma-international.org/ecma-262/6.0/#sec-tostring-applied-to-the-number-type for more information | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd omit the spec link. I also think "will behave as strings with leading zeroes" might be more confusing than clarifying.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed, and made the entire thing arguably clearer |
||
| myURL.port = 4.567e21; | ||
| console.log(myURL.port); | ||
| // Prints 4 (because the coefficient is 4.567) | ||
| ``` | ||
|
|
||
| The port value may be set as either a number or as a String containing a number | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it isn't super clear that
(1e10).toString()is'10000000000'-- might be good to clarify that.It got me thinking that it might be good to just describe the actual algorithm for assigning a number to
portin the prose (ll. 322-329): the number is first stringified usingString(num), and then the leading characters of the resulting string that are digits are then converted back to a number. (Note: in cases where the default representation of the number is an exponential, the parts after the decimal point or theecharacter are discarded.) Only then is the range checked.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've rephrased the whole thing, adopting most of your advice, and it is much clearer now in my opinion.