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.
<button type="button" data-controller="wait" data-wait-delay-value="7_000">Delay for 7 seconds</button>
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.
return Number(value.replace('_','')
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
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
Numberdoes not support underscores in the provided string (note - neither does parseInt)