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

Feature request - support numeric separators in number value attributes #646

Closed
lb- opened this issue Jan 26, 2023 · 1 comment · Fixed by #693
Closed

Feature request - support numeric separators in number value attributes #646

lb- opened this issue Jan 26, 2023 · 1 comment · Fixed by #693

Comments

@lb-
Copy link
Contributor

lb- commented Jan 26, 2023

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)

  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

@lb-
Copy link
Contributor Author

lb- commented Jan 26, 2023

If there is an appetite for this feature, I'm happy to put together a PR for it.

lb- pushed a commit to lb-/stimulus that referenced this issue Jun 20, 2023
lb- pushed a commit to lb-/stimulus that referenced this issue Jun 20, 2023
lb- added a commit to lb-/stimulus that referenced this issue Jun 21, 2023
lb- added a commit to lb-/stimulus that referenced this issue Jun 21, 2023
@dhh dhh closed this as completed in #693 Jun 24, 2023
dhh pushed a commit that referenced this issue Jun 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

1 participant