fix: handle invalid luhn checksum input

This commit is contained in:
suzunn 2026-06-10 13:52:44 +03:00
parent f30668aeff
commit da11800cac
2 changed files with 20 additions and 2 deletions

View File

@ -81,8 +81,15 @@ class LuhnChecksum extends Operation {
throw new OperationError("Error: Radix argument must be divisible by 2");
}
const checkSum = this.checksum(input, radix).toString(radix);
let checkDigit = this.checksum(input + "0", radix);
let checkSum, checkDigit;
try {
checkSum = this.checksum(input, radix).toString(radix);
checkDigit = this.checksum(input + "0", radix);
} catch (err) {
throw new OperationError(err.message);
}
checkDigit = checkDigit === 0 ? 0 : (radix - checkDigit);
checkDigit = checkDigit.toString(radix);

View File

@ -433,4 +433,15 @@ TestRegister.addTests([
},
],
},
{
name: "Luhn Checksum on invalid radix character",
input: "o",
expectedOutput: "Character: o is not valid in radix 4.",
recipeConfig: [
{
op: "Luhn Checksum",
args: [4]
},
],
},
]);