feat: add Num.fromString static method to parse decimal strings into numbers

Implement a new `Num.fromString(value)` method that attempts to parse a string as a decimal literal and returns the corresponding `Num` instance, or `null` if parsing fails. The implementation uses `strtod` for conversion, validates the argument is a string at runtime, and includes tests for valid numbers, edge cases like leading/trailing whitespace and partial suffix parsing, non-number inputs returning null, and a runtime error when a non-string argument is passed. Also fixes a missing semicolon in the `toString` native registration and removes the hex literal TODO from the number literals test.
This commit is contained in:
Bob Nystrom
2015-02-25 14:53:12 +00:00
5 changed files with 40 additions and 2 deletions
+7
View File
@@ -90,3 +90,10 @@ It is a runtime error if `other` is not a number.
### **...**(other) operator
**TODO**
### Num.**fromString**(value)
Attempts to parse `value` as a decimal literal and return it as an instance of
`Num`. If the number cannot be parsed `null` will be returned.
It is a runtime error if `value` is not a string.