-
Notifications
You must be signed in to change notification settings - Fork 426
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
Feature request - support numeric separators in number value attributes #646
Comments
If there is an appetite for this feature, I'm happy to put together a PR for it. |
5 tasks
lb-
pushed a commit
to lb-/stimulus
that referenced
this issue
Jun 20, 2023
- Aligns with JavaScript numeric separators - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#numeric_separators - Example `data-wait-delay-value="7_000"` - Closes hotwired#646
lb-
pushed a commit
to lb-/stimulus
that referenced
this issue
Jun 20, 2023
- Aligns with JavaScript numeric separators - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#numeric_separators - Example `data-wait-delay-value="7_000"` - Closes hotwired#646
lb-
added a commit
to lb-/stimulus
that referenced
this issue
Jun 21, 2023
- Aligns with JavaScript numeric separators - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#numeric_separators - Example `data-wait-delay-value="7_000"` - Closes hotwired#646
lb-
added a commit
to lb-/stimulus
that referenced
this issue
Jun 21, 2023
- Aligns with JavaScript numeric separators - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#numeric_separators - Example `data-wait-delay-value="7_500"` - Closes hotwired#646
dhh
pushed a commit
that referenced
this issue
Jun 24, 2023
- Aligns with JavaScript numeric separators - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#numeric_separators - Example `data-wait-delay-value="7_500"` - Closes #646
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Summary
The ability to use an underscore as a separator within number values (e.g.
200_000
) is now part of the ECMA script spec and is supported in most browsers.It would be great to see this supported in number values when used within Stimulus value attributes.
For example, when wanting to support milliseconds but also ensure that the value is readable you could do the following.
Proposed implementation
The simplest way to resolve this would be to blanket remove underscore characters when parsing the number (example below). However, this is not explicitly aligned with the specification, but a regex could be written to be 100% aligned.
stimulus/src/core/value_properties.ts
Line 226 in 8656407
The documentation would also need to be updated to reflect this support and explain the parsing logic. https://stimulus.hotwired.dev/reference/values#types
Additional notes
While this is just syntactic sugar, it is backwards compatible and will help make long numbers more readable at a glance.
When numbers are being updated by the controller itself, I assume there would be no attempt to re-apply the original formatting. This would only benefit manually written numbers in the html on first load / template responses.
Links
Number
does not support underscores in the provided string (note - neither does parseInt)The text was updated successfully, but these errors were encountered: