Skip to content

Commit

Permalink
Add Number.parseFloat (#668)
Browse files Browse the repository at this point in the history
Co-authored-by: Peli de Halleux <pelikhan@users.noreply.github.com>
  • Loading branch information
PiotrKasperski and pelikhan authored Dec 4, 2023
1 parent 4ef1920 commit b91d71d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
23 changes: 23 additions & 0 deletions packages/runtime/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,29 @@ describe("number", () => {
checkNot(false)
checkNot([NaN])
})
test("parseFloat", () => {
const check = (v: unknown) => expect(Number.parseFloat(v)).toBe(3.14)
const checkNaN = (v: unknown) =>
expect(isNaN(Number.parseFloat(v))).toBe(isNaN(NaN))

const checkInfinity = (v: unknown) =>
expect(Number.parseFloat(v)).toBe(Infinity)

const checkMinusInfinity = (v: unknown) =>
expect(Number.parseFloat(v)).toBe(-Infinity)
check(3.14)
check("3.14")
check(" 3.14 ")
check("314e-2")
check("0.0314E+2")
check("3.14some non-digit characters")
checkNaN("FF2")
checkNaN("NaN")
checkInfinity("1.7976931348623159e+308")
checkMinusInfinity("-1.7976931348623159e+308")
checkInfinity("Infinity")
checkMinusInfinity("-Infinity")
})
test("isFinite", () => {
const check = (v: unknown) => expect(Number.isFinite(v)).toBe(true)
const checkNot = (v: unknown) => expect(Number.isFinite(v)).toBe(false)
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime/src/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ export class Number {
return isNaN(number as number)
}
/**
* Returns tru if the passed value is finite number, false otherwise.
* Returns floating point number parsed from the given string, or NaN when the first non-whitespace character cannot be converted to a number.
* @param string The value to parse, coerced to a string. Leading whitespace in this argument is ignored.
*/
static parseFloat(string: unknown): number {
return parseFloat(string as string)
}
/** Returns tru if the passed value is finite number, false otherwise.
* @param number A numeric value
*/
static isFinite(number: unknown): boolean {
Expand Down

0 comments on commit b91d71d

Please sign in to comment.