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

Skip to content

Commit a3ad946

Browse files
committed
Add support for number values with underscores
- 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 #646
1 parent b73bef7 commit a3ad946

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

docs/reference/actions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ The list of supported modifier keys is shown below.
131131

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

134-
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:
134+
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:
135135

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

docs/reference/values.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ export default class extends Controller {
5353

5454
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.
5555

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

6464
## Properties and Attributes
6565

src/core/value_properties.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ const readers: { [type: string]: Reader } = {
248248
},
249249

250250
number(value: string): number {
251-
return Number(value)
251+
return Number(value.replace(/_/g, ""))
252252
},
253253

254254
object(value: string): object {

src/tests/modules/core/value_tests.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ export default class ValueTests extends ControllerTestCase(ValueController) {
4646
this.controller.numericValue = "" as any
4747
this.assert.equal(this.controller.numericValue, 0)
4848
this.assert.equal(this.get("numeric-value"), "")
49+
50+
// Number values should support Numeric separators
51+
this.set("numeric-value", "7_150")
52+
this.assert.equal(this.controller.numericValue, 7150)
53+
54+
// Number values should be written simply, without Numeric separators
55+
this.controller.numericValue = 10500
56+
this.assert.deepEqual(this.get("numeric-value"), "10500")
4957
}
5058

5159
"test boolean values"() {

0 commit comments

Comments
 (0)