-
Notifications
You must be signed in to change notification settings - Fork 257
feat(NODE-5648): add Long.fromStringStrict() #675
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
21 commits
Select commit
Hold shift + click to select a range
c0f5350
initial blah
aditi-khare-mongoDB 272e4f1
another tempp commit
aditi-khare-mongoDB 4483ac4
finished validateStringCharacters
aditi-khare-mongoDB a87dade
feat(NODE-5648): add Long.fromStringStrict method
aditi-khare-mongoDB 76801e8
EOD progress
aditi-khare-mongoDB a001590
ready for review
aditi-khare-mongoDB c4818d9
make error message for overflows more accurate
aditi-khare-mongoDB 59f3911
Merge branch 'main' into NODE-5648/long-validateString
aditi-khare-mongoDB 4c7d5b5
add whitespace test case
aditi-khare-mongoDB 8939fe9
fix test naming
aditi-khare-mongoDB 667168e
requested changes
aditi-khare-mongoDB 88fb767
lint fix
aditi-khare-mongoDB e616e88
requested changes team review
aditi-khare-mongoDB 70c6162
add argument parsing check within fromStringStrict
aditi-khare-mongoDB 1d261c5
removed extraneous functionality from _fromString and moved it to fro…
aditi-khare-mongoDB 60b2272
remove unneeded param from _fromString
aditi-khare-mongoDB 9f308aa
requested changes: alter support for infinity/nan cases and add expli…
aditi-khare-mongoDB 63beeea
optimize removeLeadingZerosAndExplicitPlus function and fix name typo
aditi-khare-mongoDB d212f7e
requested changes - check str length in removeLeadingZerosAndExplicit…
aditi-khare-mongoDB c98b513
remove fromString fix
aditi-khare-mongoDB 152db8f
lint fix
aditi-khare-mongoDB 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,44 @@ | ||
/** | ||
* @internal | ||
* Removes leading zeros and explicit plus from textual representation of a number. | ||
*/ | ||
export function removeLeadingZerosAndExplicitPlus(str: string): string { | ||
if (str === '') { | ||
return str; | ||
} | ||
|
||
let startIndex = 0; | ||
|
||
const isNegative = str[startIndex] === '-'; | ||
const isExplicitlyPositive = str[startIndex] === '+'; | ||
|
||
if (isExplicitlyPositive || isNegative) { | ||
startIndex += 1; | ||
} | ||
|
||
let foundInsignificantZero = false; | ||
|
||
for (; startIndex < str.length && str[startIndex] === '0'; ++startIndex) { | ||
foundInsignificantZero = true; | ||
} | ||
|
||
if (!foundInsignificantZero) { | ||
return isExplicitlyPositive ? str.slice(1) : str; | ||
} | ||
|
||
return `${isNegative ? '-' : ''}${str.length === startIndex ? '0' : str.slice(startIndex)}`; | ||
} | ||
|
||
/** | ||
* @internal | ||
* Returns false for an string that contains invalid characters for its radix, else returns the original string. | ||
* @param str - The textual representation of the Long | ||
* @param radix - The radix in which the text is written (2-36), defaults to 10 | ||
*/ | ||
export function validateStringCharacters(str: string, radix?: number): false | string { | ||
radix = radix ?? 10; | ||
const validCharacters = '0123456789abcdefghijklmnopqrstuvwxyz'.slice(0, radix); | ||
// regex is case insensitive and checks that each character within the string is one of the validCharacters | ||
const regex = new RegExp(`[^-+${validCharacters}]`, 'i'); | ||
return regex.test(str) ? false : str; | ||
} |
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
Oops, something went wrong.
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.