-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
ae8bac7
commit 9d5a5df
Showing
7 changed files
with
353 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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.