From ab7903937094aeef05a101f33d59dd041de6cddb Mon Sep 17 00:00:00 2001 From: Anderson Date: Fri, 19 May 2023 19:45:44 -0400 Subject: [PATCH] feat: parse numeric strings as BigInt on supported systems --- src/util.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/util.ts b/src/util.ts index 09ed748..51c2883 100644 --- a/src/util.ts +++ b/src/util.ts @@ -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 = ( str: string,