fix: handle invalid luhn checksum input
This commit is contained in:
parent
f30668aeff
commit
da11800cac
@ -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);
|
||||
|
||||
|
||||
@ -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]
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user