Browse Source

Generates both the checksum and checkdigit.

n1073645 5 years ago
parent
commit
0e40daecb6
1 changed files with 18 additions and 5 deletions
  1. 18 5
      src/core/operations/LuhnChecksum.mjs

+ 18 - 5
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.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.infoURL = "https://wikipedia.org/wiki/Luhn_algorithm";
         this.inputType = "string";
         this.inputType = "string";
-        this.outputType = "number";
+        this.outputType = "string";
         this.args = [];
         this.args = [];
     }
     }
 
 
     /**
     /**
-     * @param {string} input
-     * @param {Object[]} args
+     * Generates the Luhn Checksum from the input.
+     *
+     * @param {string} inputStr
      * @returns {number}
      * @returns {number}
      */
      */
-    run(input, args) {
+    checksum(inputStr) {
         let even = false;
         let even = false;
-        return input.split("").reverse().reduce((acc, elem) => {
+        return inputStr.split("").reverse().reduce((acc, elem) => {
 
 
             // Convert element to integer.
             // Convert element to integer.
             let temp = parseInt(elem, 10);
             let temp = parseInt(elem, 10);
@@ -57,6 +58,18 @@ class LuhnChecksum extends Operation {
         }, 0)  % 10;
         }, 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;
 export default LuhnChecksum;