-
-
Notifications
You must be signed in to change notification settings - Fork 186
Implement Number.prototype.toString(radix) #752
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
Perryvw
merged 4 commits into
TypeScriptToLua:master
from
ark120202:number-to-string-radix
Nov 26, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // tslint:disable-next-line: variable-name | ||
| const ____radixChars = "0123456789abcdefghijklmnopqrstuvwxyz"; | ||
|
|
||
| // https://www.ecma-international.org/ecma-262/10.0/index.html#sec-number.prototype.tostring | ||
| function __TS__NumberToString(this: number, radix?: number): string { | ||
| if (radix === undefined || radix === 10 || this === Infinity || this === -Infinity || this !== this) { | ||
| return this.toString(); | ||
| } | ||
|
|
||
| radix = Math.floor(radix); | ||
| if (radix < 2 || radix > 36) { | ||
| // tslint:disable-next-line: no-string-throw | ||
| throw "toString() radix argument must be between 2 and 36"; | ||
| } | ||
|
|
||
| let [integer, fraction] = math.modf(Math.abs(this)); | ||
|
|
||
| let result = ""; | ||
| if (radix === 8) { | ||
| result = string.format("%o", integer); | ||
| } else if (radix === 16) { | ||
| result = string.format("%x", integer); | ||
| } else { | ||
| do { | ||
| result = ____radixChars[integer % radix] + result; | ||
| integer = Math.floor(integer / radix); | ||
| } while (integer !== 0); | ||
| } | ||
|
|
||
| // https://github.com/v8/v8/blob/f78e8d43c224847fa56b3220a90be250fc0f0d6e/src/numbers/conversions.cc#L1221 | ||
| if (fraction !== 0) { | ||
| result += "."; | ||
| let delta = 1e-16; | ||
| do { | ||
| fraction *= radix; | ||
| delta *= radix; | ||
| const digit = Math.floor(fraction); | ||
| result += ____radixChars[digit]; | ||
| fraction -= digit; | ||
| // TODO: Round to even | ||
| } while (fraction >= delta); | ||
| } | ||
|
|
||
| if (this < 0) { | ||
| result = "-" + result; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
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 @@ | ||
| /** @noSelf */ | ||
| declare namespace math { | ||
| /** @tupleReturn */ | ||
| function modf(x: number): [number, number]; | ||
| } |
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 +1,16 @@ | ||
| export const normalizeSlashes = (filePath: string) => filePath.replace(/\\/g, "/"); | ||
|
|
||
| export function flatMap<T, U>(array: readonly T[], callback: (value: T, index: number) => U | readonly U[]): U[] { | ||
| const result: U[] = []; | ||
|
|
||
| for (const [index, value] of array.entries()) { | ||
| const mappedValue = callback(value, index); | ||
| if (Array.isArray(mappedValue)) { | ||
| result.push(...mappedValue); | ||
| } else { | ||
| result[result.length] = mappedValue as U; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you use some kind of reference for this implementation? If so, could you add a comment with a link.