From 0e40daecb6fa6ec60b8474ce2c4757ef10e515b8 Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 9 Mar 2020 09:13:02 +0000 Subject: [PATCH 1/3] Generates both the checksum and checkdigit. --- src/core/operations/LuhnChecksum.mjs | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/core/operations/LuhnChecksum.mjs b/src/core/operations/LuhnChecksum.mjs index 24549072..e6d5fe2b 100644 --- a/src/core/operations/LuhnChecksum.mjs +++ b/src/core/operations/LuhnChecksum.mjs @@ -23,18 +23,19 @@ class LuhnChecksum extends Operation { this.description = "The Luhn algorithm, also known as the modulus 10 or mod 10 algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, Canadian Social Insurance Numbers."; this.infoURL = "https://wikipedia.org/wiki/Luhn_algorithm"; this.inputType = "string"; - this.outputType = "number"; + this.outputType = "string"; this.args = []; } /** - * @param {string} input - * @param {Object[]} args + * Generates the Luhn Checksum from the input. + * + * @param {string} inputStr * @returns {number} */ - run(input, args) { + checksum(inputStr) { let even = false; - return input.split("").reverse().reduce((acc, elem) => { + return inputStr.split("").reverse().reduce((acc, elem) => { // Convert element to integer. let temp = parseInt(elem, 10); @@ -57,6 +58,18 @@ class LuhnChecksum extends Operation { }, 0) % 10; } + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const checkSum = this.checksum(input).toString(); + let checkDigit = this.checksum(input+"0"); + checkDigit = (checkDigit === 0 ? 0 : (10-checkDigit)).toString(); + return "Checksum: " + checkSum + "\n\nCheckdigit: " + checkDigit + "\n\nLuhn Validated String: "+ input + checkDigit; + } + } export default LuhnChecksum; From 54cb2d268b9eb0c6afa7d30c76995d07563729ec Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 9 Mar 2020 09:37:34 +0000 Subject: [PATCH 2/3] Luhn checksum tests --- src/core/operations/LuhnChecksum.mjs | 6 ++++++ tests/operations/tests/LuhnChecksum.mjs | 24 +++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/core/operations/LuhnChecksum.mjs b/src/core/operations/LuhnChecksum.mjs index e6d5fe2b..d9aff649 100644 --- a/src/core/operations/LuhnChecksum.mjs +++ b/src/core/operations/LuhnChecksum.mjs @@ -64,9 +64,15 @@ class LuhnChecksum extends Operation { * @returns {string} */ run(input, args) { + + if (!(input)) return "0"; + const checkSum = this.checksum(input).toString(); + let checkDigit = this.checksum(input+"0"); + checkDigit = (checkDigit === 0 ? 0 : (10-checkDigit)).toString(); + return "Checksum: " + checkSum + "\n\nCheckdigit: " + checkDigit + "\n\nLuhn Validated String: "+ input + checkDigit; } diff --git a/tests/operations/tests/LuhnChecksum.mjs b/tests/operations/tests/LuhnChecksum.mjs index 28b17854..c5c4c6b7 100644 --- a/tests/operations/tests/LuhnChecksum.mjs +++ b/tests/operations/tests/LuhnChecksum.mjs @@ -11,7 +11,29 @@ TestRegister.addTests([ { name: "Luhn Checksum on standard data", input: "35641709012469", - expectedOutput: "7", + expectedOutput: "Checksum: 7\n\nCheckdigit: 0\n\nLuhn Validated String: 356417090124690", + recipeConfig: [ + { + op: "Luhn Checksum", + args: [] + }, + ], + }, + { + name: "Luhn Checksum on standard data 2", + input: "896101950123440000", + expectedOutput: "Checksum: 5\n\nCheckdigit: 1\n\nLuhn Validated String: 8961019501234400001", + recipeConfig: [ + { + op: "Luhn Checksum", + args: [] + }, + ], + }, + { + name: "Luhn Checksum on standard data 3", + input: "35726908971331", + expectedOutput: "Checksum: 6\n\nCheckdigit: 7\n\nLuhn Validated String: 357269089713317", recipeConfig: [ { op: "Luhn Checksum", From 0eacab5ddcc8e0912807c05976400a456b9a8758 Mon Sep 17 00:00:00 2001 From: n1474335 Date: Thu, 12 Mar 2020 14:41:46 +0000 Subject: [PATCH 3/3] Tidied up Luhn checksum op --- src/core/operations/LuhnChecksum.mjs | 18 +++++++----------- tests/operations/tests/LuhnChecksum.mjs | 8 ++++---- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/core/operations/LuhnChecksum.mjs b/src/core/operations/LuhnChecksum.mjs index 713e0e92..cb3a7c24 100644 --- a/src/core/operations/LuhnChecksum.mjs +++ b/src/core/operations/LuhnChecksum.mjs @@ -36,7 +36,6 @@ class LuhnChecksum extends Operation { checksum(inputStr) { let even = false; return inputStr.split("").reverse().reduce((acc, elem) => { - // Convert element to integer. let temp = parseInt(elem, 10); @@ -46,7 +45,6 @@ class LuhnChecksum extends Operation { // If element is in an even position if (even) { - // Double the element and add the quotient and remainder together. temp = 2 * elem; temp = Math.floor(temp/10) + (temp % 10); @@ -54,7 +52,6 @@ class LuhnChecksum extends Operation { even = !even; return acc + temp; - }, 0) % 10; } @@ -64,16 +61,15 @@ class LuhnChecksum extends Operation { * @returns {string} */ run(input, args) { + if (!input) return ""; - if (!(input)) return "0"; + const checkSum = this.checksum(input); + let checkDigit = this.checksum(input + "0"); + checkDigit = checkDigit === 0 ? 0 : (10-checkDigit); - const checkSum = this.checksum(input).toString(); - - let checkDigit = this.checksum(input+"0"); - - checkDigit = (checkDigit === 0 ? 0 : (10-checkDigit)).toString(); - - return "Checksum: " + checkSum + "\n\nCheckdigit: " + checkDigit + "\n\nLuhn Validated String: "+ input + checkDigit; + return `Checksum: ${checkSum} +Checkdigit: ${checkDigit} +Luhn Validated String: ${input + "" + checkDigit}`; } } diff --git a/tests/operations/tests/LuhnChecksum.mjs b/tests/operations/tests/LuhnChecksum.mjs index c5c4c6b7..498a1542 100644 --- a/tests/operations/tests/LuhnChecksum.mjs +++ b/tests/operations/tests/LuhnChecksum.mjs @@ -11,7 +11,7 @@ TestRegister.addTests([ { name: "Luhn Checksum on standard data", input: "35641709012469", - expectedOutput: "Checksum: 7\n\nCheckdigit: 0\n\nLuhn Validated String: 356417090124690", + expectedOutput: "Checksum: 7\nCheckdigit: 0\nLuhn Validated String: 356417090124690", recipeConfig: [ { op: "Luhn Checksum", @@ -22,7 +22,7 @@ TestRegister.addTests([ { name: "Luhn Checksum on standard data 2", input: "896101950123440000", - expectedOutput: "Checksum: 5\n\nCheckdigit: 1\n\nLuhn Validated String: 8961019501234400001", + expectedOutput: "Checksum: 5\nCheckdigit: 1\nLuhn Validated String: 8961019501234400001", recipeConfig: [ { op: "Luhn Checksum", @@ -33,7 +33,7 @@ TestRegister.addTests([ { name: "Luhn Checksum on standard data 3", input: "35726908971331", - expectedOutput: "Checksum: 6\n\nCheckdigit: 7\n\nLuhn Validated String: 357269089713317", + expectedOutput: "Checksum: 6\nCheckdigit: 7\nLuhn Validated String: 357269089713317", recipeConfig: [ { op: "Luhn Checksum", @@ -55,7 +55,7 @@ TestRegister.addTests([ { name: "Luhn Checksum on empty data", input: "", - expectedOutput: "0", + expectedOutput: "", recipeConfig: [ { op: "Luhn Checksum",