diff --git a/tests/built-ins/Number/parseFloat.js b/tests/built-ins/Number/parseFloat.js index 840954f2..d56e9b41 100644 --- a/tests/built-ins/Number/parseFloat.js +++ b/tests/built-ins/Number/parseFloat.js @@ -24,4 +24,11 @@ describe("Number.parseFloat", () => { test("returns NaN for non-parseable strings", () => { expect(Number.isNaN(Number.parseFloat("abc"))).toBe(true); }); + + test("parses Infinity", () => { + expect(Number.parseFloat("Infinity")).toBe(Infinity); + expect(Number.parseFloat("+Infinity")).toBe(Infinity); + expect(Number.parseFloat("-Infinity")).toBe(-Infinity); + expect(Number.parseFloat(" Infinity ")).toBe(Infinity); + }); }); diff --git a/tests/built-ins/Number/parseInt.js b/tests/built-ins/Number/parseInt.js index f05bf7b8..02d18171 100644 --- a/tests/built-ins/Number/parseInt.js +++ b/tests/built-ins/Number/parseInt.js @@ -56,4 +56,10 @@ describe("Number.parseInt", () => { test("leading plus sign", () => { expect(Number.parseInt("+42")).toBe(42); }); + + test("Infinity returns NaN", () => { + expect(Number.isNaN(Number.parseInt("Infinity"))).toBe(true); + expect(Number.isNaN(Number.parseInt("-Infinity"))).toBe(true); + expect(Number.isNaN(Number.parseInt("+Infinity"))).toBe(true); + }); }); diff --git a/units/Goccia.Builtins.GlobalNumber.pas b/units/Goccia.Builtins.GlobalNumber.pas index 28c18d4b..8db5d155 100644 --- a/units/Goccia.Builtins.GlobalNumber.pas +++ b/units/Goccia.Builtins.GlobalNumber.pas @@ -222,9 +222,19 @@ function TGocciaGlobalNumber.NumberParseFloat(const AArgs: TGocciaArgumentsColle Inc(I); end; + // Step 3b: Check for "Infinity" literal (StrDecimalLiteral :: Infinity) + if Copy(InputStr, I, 8) = 'Infinity' then + begin + if Sign = -1 then + Result := TGocciaNumberLiteralValue.NegativeInfinityValue + else + Result := TGocciaNumberLiteralValue.InfinityValue; + Exit; + end; + IntegerPart := 0; - // Step 3b: Parse DecimalDigits (integer part) + // Step 3c: Parse DecimalDigits (integer part) while I <= Length(InputStr) do begin C := InputStr[I]; @@ -241,7 +251,7 @@ function TGocciaGlobalNumber.NumberParseFloat(const AArgs: TGocciaArgumentsColle FractionalPart := 0; FractionDivisor := 1; - // Step 3c: Parse optional '.' DecimalDigits (fractional part) + // Step 3d: Parse optional '.' DecimalDigits (fractional part) if (I <= Length(InputStr)) and (InputStr[I] = '.') then begin Inc(I);