Skip to content

Commit

Permalink
Add support for number values with underscores (#693)
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
lb- authored Jun 24, 2023
1 parent 7974f63 commit 4ef73b0
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/reference/actions.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ The list of supported modifier keys is shown below.

Sometimes a controller needs to listen for events dispatched on the global `window` or `document` objects.

You can append `@window` or `@document` to the event name (along with any filter modifier) in an action descriptor to install the event listener on `window` or `document`, respectively, as in the following example:
You can append `@window` or `@document` to the event name (along with any filter modifer) in an action descriptor to install the event listener on `window` or `document`, respectively, as in the following example:

<meta data-controller="callout" data-callout-text-value="resize@window">

Expand Down
14 changes: 7 additions & 7 deletions docs/reference/values.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ export default class extends Controller {

A value's type is one of `Array`, `Boolean`, `Number`, `Object`, or `String`. The type determines how the value is transcoded between JavaScript and HTML.

Type | Encoded as… | Decoded as…
---- | ----------- | -----------
Array | `JSON.stringify(array)` | `JSON.parse(value)`
Boolean | `boolean.toString()` | `!(value == "0" \|\| value == "false")`
Number | `number.toString()` | `Number(value)`
Object | `JSON.stringify(object)` | `JSON.parse(value)`
String | Itself | Itself
| Type | Encoded as… | Decoded as… |
| ------- | ------------------------ | --------------------------------------- |
| Array | `JSON.stringify(array)` | `JSON.parse(value)` |
| Boolean | `boolean.toString()` | `!(value == "0" \|\| value == "false")` |
| Number | `number.toString()` | `Number(value.replace(/_/g, ""))` |
| Object | `JSON.stringify(object)` | `JSON.parse(value)` |
| String | Itself | Itself |

## Properties and Attributes

Expand Down
2 changes: 1 addition & 1 deletion src/core/value_properties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ const readers: { [type: string]: Reader } = {
},

number(value: string): number {
return Number(value)
return Number(value.replace(/_/g, ""))
},

object(value: string): object {
Expand Down
8 changes: 8 additions & 0 deletions src/tests/modules/core/value_tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ export default class ValueTests extends ControllerTestCase(ValueController) {
this.controller.numericValue = "" as any
this.assert.equal(this.controller.numericValue, 0)
this.assert.equal(this.get("numeric-value"), "")

// Number values should support Numeric separators
this.set("numeric-value", "7_150")
this.assert.equal(this.controller.numericValue, 7150)

// Number values should be written simply, without Numeric separators
this.controller.numericValue = 10500
this.assert.deepEqual(this.get("numeric-value"), "10500")
}

"test boolean values"() {
Expand Down

0 comments on commit 4ef73b0

Please sign in to comment.