Skip to content

Commit

Permalink
feat: parse numeric strings as BigInt on supported systems
Browse files Browse the repository at this point in the history
  • Loading branch information
Anderson committed May 22, 2023
1 parent 6029aee commit ab79039
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,23 @@ export const fromAlphabet = (

const safeToParseNumberRegExp = /^\+?\d+$/

export const safeParseInt10 = (str: string) =>
safeToParseNumberRegExp.test(str) ? Number.parseInt(str, 10) : Number.NaN
export const safeParseInt10 = (str: string) => {
if (!safeToParseNumberRegExp.test(str)) {
return Number.NaN
}

const int10 = Number.parseInt(str, 10)

if (Number.isSafeInteger(int10)) {
return int10
}

throwIfBigIntNotAvailable(
'Unable to encode the provided BigInt string without loss of information due to lack of support for BigInt type in the current environment',
)

return BigInt(str)
}

export const splitAtIntervalAndMap = <T>(
str: string,
Expand Down

0 comments on commit ab79039

Please sign in to comment.