Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mask.js 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. export var defaultDelimiters = /[-!$%^&*()_+|~=`{}[\]:";'<>?,./\\ ]/;
  2. export var isMaskDelimiter = function isMaskDelimiter(char) {
  3. return char ? defaultDelimiters.test(char) : false;
  4. };
  5. var allowedMasks = {
  6. '#': {
  7. test: function test(char) {
  8. return (/[0-9]/.test(char)
  9. );
  10. }
  11. },
  12. 'A': {
  13. test: function test(char) {
  14. return (/[A-Z]/i.test(char)
  15. );
  16. },
  17. convert: function convert(char) {
  18. return char.toUpperCase();
  19. }
  20. },
  21. 'a': {
  22. test: function test(char) {
  23. return (/[a-z]/i.test(char)
  24. );
  25. },
  26. convert: function convert(char) {
  27. return char.toLowerCase();
  28. }
  29. },
  30. 'N': {
  31. test: function test(char) {
  32. return (/[0-9A-Z]/i.test(char)
  33. );
  34. },
  35. convert: function convert(char) {
  36. return char.toUpperCase();
  37. }
  38. },
  39. 'n': {
  40. test: function test(char) {
  41. return (/[0-9a-z]/i.test(char)
  42. );
  43. },
  44. convert: function convert(char) {
  45. return char.toLowerCase();
  46. }
  47. },
  48. 'X': {
  49. test: isMaskDelimiter
  50. }
  51. };
  52. var isMask = function isMask(char) {
  53. return allowedMasks.hasOwnProperty(char);
  54. };
  55. var convert = function convert(mask, char) {
  56. return allowedMasks[mask].convert ? allowedMasks[mask].convert(char) : char;
  57. };
  58. var maskValidates = function maskValidates(mask, char) {
  59. if (char == null || !isMask(mask)) return false;
  60. return allowedMasks[mask].test(char);
  61. };
  62. export var maskText = function maskText(text, masked, dontFillMaskBlanks) {
  63. if (text == null) return '';
  64. text = String(text);
  65. if (!masked.length || !text.length) return text;
  66. if (!Array.isArray(masked)) masked = masked.split('');
  67. var textIndex = 0;
  68. var maskIndex = 0;
  69. var newText = '';
  70. while (maskIndex < masked.length) {
  71. var mask = masked[maskIndex];
  72. // Assign the next character
  73. var char = text[textIndex];
  74. // Check if mask is delimiter
  75. // and current char matches
  76. if (!isMask(mask) && char === mask) {
  77. newText += mask;
  78. textIndex++;
  79. // Check if not mask
  80. } else if (!isMask(mask) && !dontFillMaskBlanks) {
  81. newText += mask;
  82. // Check if is mask and validates
  83. } else if (maskValidates(mask, char)) {
  84. newText += convert(mask, char);
  85. textIndex++;
  86. } else {
  87. return newText;
  88. }
  89. maskIndex++;
  90. }
  91. return newText;
  92. };
  93. export var unmaskText = function unmaskText(text) {
  94. return text ? String(text).replace(new RegExp(defaultDelimiters, 'g'), '') : text;
  95. };
  96. //# sourceMappingURL=mask.js.map