question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

OptionController value coercing number to string

See original GitHub issue

Inside OptionController by using the HTML DOM option object and specifically the inherited setAttribute method any value in a list of possible options that should be a number will subsequently be converted into a string by the native implementation of setAttribute method.

https://github.com/dataarts/dat.gui/blob/8cd1b4d2ae0982b63105deedac320004373af99b/src/dat/controllers/OptionController.js#L54

Happens when wanting to have multiple numbers as options when using add with args that are of type: add(target: Object, propName:string, items:number[]): GUIController;

https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
MacroMancommented, Mar 29, 2019

The values of a select input will always be of type string. That’s the HTML spec, and so cannot remain a number.

You could subscribe to the onchange event and coerce them back using Number or parseInt.

0reactions
neillrobsoncommented, Apr 29, 2019

I was also stumped for a little while on how to elegantly solve this issue. The following function worked for me:

function toNumber(value) {
    this.object[this.property] = Number(value);
}

gui.add(obj, "name", { Zero: 0, One: 1 }).onChange(toNumber);

I do think that it would be good to mention this constraint (and perhaps provide alternative solutions such as the above function) somewhere in the documentation.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type Coercion with DataWeave - MuleSoft Documentation
First, you coerce a number value into String type to the specified output format that uses the Java DecimalFormat pattern (##) and also...
Read more >
What's the best way to convert a number to a string in ...
pfx the question asks how to convert numeric values to a string. Providing examples of non-numeric values (e.g. null , undefined ) that...
Read more >
Conversion rules | BigQuery - Google Cloud
Note: String literals do not coerce to numeric types. ... If the parameter value cannot be coerced successfully to the target type, an...
Read more >
VALUE - Google Docs Editors Help
Converts a string in any of the date, time or number formats that Google Sheets understands into a number. Sample Usage VALUE("123") VALUE("7/20/1969") ......
Read more >
Type coercion - MDN Web Docs Glossary
Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found