-
-
Notifications
You must be signed in to change notification settings - Fork 185
Add NaN, Infinity and related number functions #557
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
23c95ca
Add NaN, Infinity and related number functions
ark120202 05de680
Add noSelfInFile to global declorations file
ark120202 01b03bd
Add few number test cases
ark120202 1d4dd52
Remove TODOs
ark120202 e10d146
Merge remote-tracking branch 'upstream/master' into nan-infinity-number
ark120202 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| function __TS__Number(this: void, value: unknown): number { | ||
| const valueType = type(value); | ||
| if (valueType === "number") { | ||
| return value as number; | ||
| } else if (valueType === "string") { | ||
| const numberValue = tonumber(value); | ||
| if (numberValue) return numberValue; | ||
|
|
||
| if (value === "Infinity") return Infinity; | ||
| if (value === "-Infinity") return -Infinity; | ||
| const [stringWithoutSpaces] = string.gsub(value as string, '%s', ''); | ||
| if (stringWithoutSpaces === "") return 0; | ||
|
|
||
| return NaN; | ||
| } else if (valueType === "boolean") { | ||
| return value ? 1 : 0; | ||
| } else { | ||
| return NaN; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| function __TS__NumberIsFinite(this: void, value: unknown): boolean { | ||
| return ( | ||
| typeof value === "number" && value === value && value !== Infinity && value !== -Infinity | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| function __TS__NumberIsNaN(this: void, value: unknown): boolean { | ||
| return value !== value; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| /** @noSelfInFile */ | ||
|
|
||
| declare function tonumber(value: any, base?: number): number | undefined; | ||
| declare function type( | ||
| this: void, | ||
| value: any | ||
| ): "nil" | "number" | "string" | "boolean" | "table" | "function" | "thread" | "userdata"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import * as util from "../util"; | ||
|
|
||
| test.each([ | ||
| "NaN === NaN", | ||
| "NaN !== NaN", | ||
| "NaN + NaN", | ||
| "NaN - NaN", | ||
| "NaN * NaN", | ||
| "NaN / NaN", | ||
| "NaN + 1", | ||
| "1 + NaN", | ||
| "1 / NaN", | ||
| "NaN * 0", | ||
| ])("%s", code => expect(util.transpileAndExecute(`return ${code}`)).toBe(eval(code))); | ||
|
|
||
| test("NaN reassignment", () => { | ||
| const result = util.transpileAndExecute(`const NaN = 1; return NaN`); | ||
|
|
||
| expect(result).toBe(NaN); | ||
| }); | ||
|
|
||
| test.each([ | ||
| "Infinity", | ||
| "Infinity - Infinity", | ||
| "Infinity / -1", | ||
| "Infinity * -1", | ||
| "Infinity + 1", | ||
| "Infinity - 1", | ||
| ])("%s", code => expect(util.transpileAndExecute(`return ${code}`)).toBe(eval(code))); | ||
|
|
||
| test("Infinity reassignment", () => { | ||
| const result = util.transpileAndExecute(`const Infinity = 1; return Infinity`); | ||
|
|
||
| expect(result).toBe(Infinity); | ||
| }); | ||
|
|
||
| const numberCases = [-1, 0, 1, 1.5, Infinity, -Infinity]; | ||
| const stringCases = ["-1", "0", "1", "1.5", "Infinity", "-Infinity"]; | ||
| const restCases: any[] = [true, false, "", " ", "\t", "\n", "foo", {}]; | ||
| const cases: any[] = [...numberCases, ...stringCases, ...restCases]; | ||
|
|
||
| // TODO: Add more general utils to serialize values | ||
| const valueToString = (value: unknown) => | ||
| value === Infinity || value === -Infinity || (typeof value === "number" && Number.isNaN(value)) | ||
| ? String(value) | ||
| : JSON.stringify(value); | ||
|
|
||
| describe("Number", () => { | ||
| test.each(cases)("constructor(%p)", value => { | ||
| const result = util.transpileAndExecute(`return Number(${valueToString(value)})`); | ||
| expect(result).toBe(Number(value)); | ||
| }); | ||
|
|
||
| test.each(cases)("isNaN(%p)", value => { | ||
| const result = util.transpileAndExecute(` | ||
| return Number.isNaN(${valueToString(value)} as any) | ||
| `); | ||
|
|
||
| expect(result).toBe(Number.isNaN(value)); | ||
| }); | ||
|
|
||
| test.each(cases)("isFinite(%p)", value => { | ||
| const result = util.transpileAndExecute(` | ||
| return Number.isFinite(${valueToString(value)} as any) | ||
| `); | ||
|
|
||
| expect(result).toBe(Number.isFinite(value)); | ||
| }); | ||
| }); | ||
|
|
||
| test.each(cases)("isNaN(%p)", value => { | ||
| const result = util.transpileAndExecute(`return isNaN(${valueToString(value)} as any)`); | ||
| expect(result).toBe(isNaN(value)); | ||
| }); | ||
|
|
||
| test.each(cases)("isFinite(%p)", value => { | ||
| const result = util.transpileAndExecute(`return isFinite(${valueToString(value)} as any)`); | ||
| expect(result).toBe(isFinite(value)); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.