All files index.js

89.55% Statements 60/67
82.61% Branches 57/69
90% Functions 9/10
89.39% Lines 59/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216  1x 1x 1x             1x 1x     1x     1x 1x   1x   1x           28x 28x   28x               14x       14x     14x             14x 14x   14x             14x     10x             4x     14x               28x     1x 14x         14x 14x 14x   14x 95x     14x           1x 95x               95x           95x             95x         95x     1x 28x     1x 171x                 1x   105x   105x         105x       105x           1x   1x 14x 4x   10x     14x   14x 18x           4x             4x           14x       14x                    
class SimpleKeyboardInputMask {
  init = keyboard => {
    keyboard.registerModule("inputMask", module => {
      Iif (!keyboard.options.inputMask) {
        console.warn(
          "SimpleKeyboardInputMask: You must provide the inputMask option with your input mask"
        );
        return false;
      }
 
      Eif (!keyboard.options.disableCaretPositioning) {
        console.warn(
          "SimpleKeyboardInputMask: Caret placement is not supported in this release. Option disableCaretPositioning will be enabled. To disable this warning, set option disableCaretPositioning to true."
        );
        keyboard.options.disableCaretPositioning = true;
      }
 
      module.currentButton = "";
      module.fn = {};
 
      module.fn.getUpdatedInput = keyboard.utilities.getUpdatedInput;
 
      keyboard.utilities.getUpdatedInput = (
        button,
        input,
        caretPos,
        moveCaret
      ) => {
        let inputMask = keyboard.options.inputMask;
        let resultingInput = "";
 
        if (
          inputMask &&
          (typeof inputMask === "object" || typeof inputMask === "string") &&
          !module.isBksp(button) &&
          moveCaret
        ) {
          let inputMaskStr;
 
          Iif (typeof inputMask === "object") {
            inputMaskStr =
              keyboard.options.inputMask[keyboard.options.inputName];
          } else {
            inputMaskStr = inputMask;
          }
 
          let overrides = module.autoAddSymbol(
            caretPos,
            input,
            inputMaskStr,
            button
          );
 
          input = overrides.input || input;
          caretPos = overrides.caretPos || caretPos;
 
          let inputProposal = module.fn.getUpdatedInput(
            button,
            input,
            caretPos,
            false
          );
 
          if (
            module.validateInputProposal(inputProposal, inputMaskStr, caretPos)
          ) {
            resultingInput = module.fn.getUpdatedInput(
              button,
              input,
              caretPos,
              moveCaret
            );
          } else {
            resultingInput = keyboard.getInput();
          }
        } else {
          resultingInput = module.fn.getUpdatedInput(
            button,
            input,
            caretPos,
            moveCaret
          );
        }
 
        return resultingInput;
      };
 
      module.validateInputProposal = (inputProposal, inputMask, caretPos) => {
        Eif (
          inputProposal &&
          typeof inputProposal === "string" &&
          (inputMask && typeof inputProposal === "string")
        ) {
          let inputPropArr = inputProposal.split("");
          let validated = true;
          let i = caretPos || 0;
 
          for (i = 0; i < inputPropArr.length; i++) {
            validated = module.isCharAllowed(inputPropArr[i], inputMask[i]);
          }
 
          return validated;
        } else {
          return false;
        }
      };
 
      module.isCharAllowed = (character, maskCharacter) => {
        Iif (!(character && maskCharacter) && character !== "0") {
          return false;
        }
 
        /**
         * Number check
         */
        let numberCheck =
          module.isNumber(character) && module.isNumber(maskCharacter);
 
        /**
         * Letter check
         */
        let letterCheck =
          module.isLetter(character) && module.isLetter(maskCharacter);
 
        /**
         * Symbol check
         */
        let symbolCheck =
          // If char not maskChar are numbers or letters
          !numberCheck &&
          !letterCheck &&
          // If char and maskChar are the same
          character === maskCharacter;
 
        return numberCheck || letterCheck || symbolCheck;
      };
 
      module.isBksp = button => {
        return button === "{bksp}" || button === "{backspace}";
      };
 
      module.isNumber = input => {
        return (
          input === "0" ||
          // If char is a number
          (!isNaN(Number(input)) &&
            // If char is not a whitespace
            input.match(/^\s$/) === null)
        );
      };
 
      module.isLetter = input => {
        let letterCheckPattern;
        let defaultCheckPattern = /[a-z]/i;
 
        Iif (typeof keyboard.options.letterCheckPattern === "object") {
          letterCheckPattern =
            keyboard.options.letterCheckPattern[keyboard.options.inputName] ||
            defaultCheckPattern;
        } else {
          letterCheckPattern =
            keyboard.options.letterCheckPattern || defaultCheckPattern;
        }
 
        return (
          // If char is a letter
          input.match(letterCheckPattern)
        );
      };
 
      module.isSymbol = () => {};
 
      module.autoAddSymbol = (caretPos, input, inputMaskStr, button) => {
        if (!input.trim() && !caretPos) {
          caretPos = 0;
        } else {
          caretPos = input.length;
        }
 
        let inputMaskArr = inputMaskStr.split("");
 
        for (let i = caretPos; i < inputMaskArr.length; i++) {
          if (
            inputMaskArr[i] &&
            !module.isNumber(inputMaskArr[i]) &&
            !module.isLetter(inputMaskArr[i]) &&
            (Number(button) || Number(button) === 0)
          ) {
            input = keyboard.utilities.addStringAt(
              input,
              inputMaskArr[i],
              i,
              false
            );
 
            Iif (!keyboard.options.disableCaretPositioning) {
              keyboard.caretPosition = keyboard.caretPosition
                ? keyboard.caretPosition + 1
                : 1;
            }
          } else {
            break;
          }
        }
 
        return {
          input: input,
          caretPos: keyboard.caretPosition
        };
      };
    });
  };
}
 
export default SimpleKeyboardInputMask;