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

Add support for number values with underscores #693

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. It was fixed though - see #704


<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