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.9KB

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