Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

@lb-

Description

@lb-

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions