fix(integer-base-converter): handle non-decimal char and better error message

This commit is contained in:
Corentin Thomasset 2022-12-07 21:52:24 +01:00
parent 0ff853437b
commit 8476cf319b
No known key found for this signature in database
GPG key ID: DBD997E935996158
4 changed files with 93 additions and 11 deletions

View file

@ -7,7 +7,7 @@ export function convertBase({ value, fromBase, toBase }: { value: string; fromBa
.reverse()
.reduce((carry: number, digit: string, index: number) => {
if (!fromRange.includes(digit)) {
throw new Error('Invalid digit `' + digit + '` for base ' + fromBase + '.');
throw new Error('Invalid digit "' + digit + '" for base ' + fromBase + '.');
}
return (carry += fromRange.indexOf(digit) * Math.pow(fromBase, index));
}, 0);