UNPKG

last-typed-letter-password

Version:

Show the last typed letter in the password field with input type as password.

55 lines (51 loc) 1.79 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var lastTyped = function lastTyped(idOfTheInput) { var iFieldCopy = document.getElementById(idOfTheInput); var flag = ""; var mask = function mask(cc) { var num = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 4; var mask = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : "•"; return "".concat(cc).slice(-num).padStart("".concat(cc).length, mask); }; iFieldCopy.addEventListener("input", function () { iFieldCopy.type = "text"; if (flag.length < iFieldCopy.value.length) { if (!iFieldCopy.value.includes("•")) { flag = iFieldCopy.value; } else { flag = flag + iFieldCopy.value.charAt(iFieldCopy.value.length - 1); } } else if (iFieldCopy.value.length === 1 || iFieldCopy.value.length === 0) { flag = iFieldCopy.value; } else { flag = flag.substring(0, flag.length - 1); } var maskedVal = mask(iFieldCopy.value, 1); iFieldCopy.value = maskedVal; processChanges(); }); function debounce(func) { var _this = this; var timeout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000; var timer; return function () { for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } clearTimeout(timer); timer = setTimeout(function () { func.apply(_this, args); }, timeout); }; } var processChanges = debounce(function () { saveInput(); }); function saveInput() { iFieldCopy.value = flag; iFieldCopy.type = "password"; console.log("Saving data ", iFieldCopy.value); } }; exports.lastTyped = lastTyped;