useTextMask.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * maskType
  3. * 1 - 全掩盖
  4. * 2 - 姓名-显示前1个字,后1个字
  5. * 3 - 手机号-显示前3位,后4位
  6. * 4 - 邮箱-显示前3个字,@和之后的字
  7. * 5 - 身份证-显示前6位,后3位,虚拟为4位
  8. * 6 - IP地址-显示第1段IP
  9. * 7 - 车牌号-显示前1个字,后2位
  10. * 8 - 银行卡号-显示前6位,后4位
  11. * 0 - 自定义规则
  12. */
  13. export const defaultMaskOptions = {
  14. filler: '*', // 填充符号
  15. maskType: 1, // 掩码规则
  16. prefixType: 1, // 开头显示
  17. prefixLimit: 0, // 开头字数
  18. prefixSpecifyChar: '', // 开头字符
  19. suffixType: 1, // 结尾显示
  20. suffixLimit: 0, // 结尾字数
  21. suffixSpecifyChar: '', // 结尾字符
  22. ignoreChar: '', // 显示字符
  23. useUnrealMask: false, // 虚拟掩码
  24. unrealMaskLength: 1, // 虚拟掩码长度
  25. };
  26. export function useTextMask(options = {}) {
  27. const config = {
  28. ...defaultMaskOptions,
  29. ...options
  30. };
  31. // 全掩盖
  32. function maskAll(str) {
  33. return config.filler.repeat(str.length);
  34. }
  35. //姓名 显示前1个字,后1个字
  36. function maskName(str) {
  37. if (str.length <= 1) return str;
  38. const prefix = str[0];
  39. if (str.length === 2) return prefix + config.filler;
  40. const suffix = str.slice(-1);
  41. const maskedChars = config.filler.repeat(str.length - 2);
  42. return prefix + maskedChars + suffix;
  43. }
  44. // 手机号 - 显示前3位,后4位
  45. function maskPhoneNumber(str) {
  46. if (str.length <= 7) return str;
  47. const prefix = str.slice(0, 3);
  48. const suffix = str.slice(-4);
  49. const maskedChars = config.filler.repeat(str.length - 7);
  50. return prefix + maskedChars + suffix;
  51. }
  52. // 邮箱 - 显示前3个字,@和之后的字
  53. function maskEmailAddress(str) {
  54. const atIndex = str.indexOf('@');
  55. if (str.length <= 3 || (atIndex > -1 && atIndex < 3)) return str;
  56. let suffixLength = 0;
  57. let maskedCharsLength = str.length - 3;
  58. if (atIndex > 0) {
  59. suffixLength = atIndex;
  60. maskedCharsLength = atIndex - 3;
  61. }
  62. const prefix = str.slice(0, 3);
  63. const suffix = suffixLength ? str.slice(suffixLength) : '';
  64. const maskedChars = config.filler.repeat(maskedCharsLength);
  65. return prefix + maskedChars + suffix;
  66. }
  67. // 身份证 - 显示前6位,后3位,虚拟为4位
  68. function maskIdNumber(str) {
  69. if (str.length <= 9) return str;
  70. const prefix = str.slice(0, 6);
  71. const suffix = str.slice(-3);
  72. const maskedChars = config.filler.repeat(4);
  73. return prefix + maskedChars + suffix;
  74. }
  75. // IP地址-显示第1段IP
  76. function maskIPAddress(str) {
  77. const segments = str.split('.');
  78. if (segments.length < 1) return str;
  79. const maskedChars = ('.' + config.filler.repeat(3)).repeat(3);
  80. return segments[0] + maskedChars;
  81. }
  82. // 车牌号-显示前1个字,后2位
  83. function maskLicensePlate(str) {
  84. if (str.length <= 3) return str;
  85. const prefix = str[0];
  86. const suffix = str.slice(-2);
  87. const maskedChars = config.filler.repeat(str.length - 3);
  88. return prefix + maskedChars + suffix;
  89. }
  90. // 银行卡号-显示前6位,后4位
  91. function maskBankCard(str) {
  92. if (str.length <= 10) return str;
  93. const prefix = str.slice(0, 6);
  94. const suffix = str.slice(-4);
  95. const maskedChars = config.filler.repeat(str.length - 10);
  96. return prefix + maskedChars + suffix;
  97. }
  98. // 自定义掩码规则
  99. function maskCustom(str) {
  100. let prefixLength = 0,
  101. suffixLength = 0;
  102. if (config.prefixType === 2) prefixLength = config.prefixLimit || 0;
  103. if ((config.prefixType === 3 || config.prefixType === 4) && config.prefixSpecifyChar) {
  104. let specifyCharIndex = str.indexOf(config.prefixSpecifyChar);
  105. if (specifyCharIndex > -1) prefixLength = config.prefixType === 3 ? specifyCharIndex :
  106. specifyCharIndex + config.prefixSpecifyChar.length;
  107. }
  108. if (config.suffixType === 2) suffixLength = config.suffixLimit || 0;
  109. if ((config.suffixType === 3 || config.suffixType === 4) && config.suffixSpecifyChar) {
  110. let specifyCharIndex = str.indexOf(config.suffixSpecifyChar);
  111. if (specifyCharIndex > -1) suffixLength = config.suffixType === 3 ?
  112. str.length - specifyCharIndex - config.suffixSpecifyChar.length :
  113. str.length - specifyCharIndex;
  114. }
  115. if (prefixLength + suffixLength >= str.length) return str;
  116. const prefix = prefixLength ? str.slice(0, prefixLength) : '';
  117. const suffix = suffixLength ? str.slice(-suffixLength) : '';
  118. let middleChar = '';
  119. if (!config.ignoreChar) {
  120. const maskedLength = config.useUnrealMask ? config.unrealMaskLength || 1 : str.length - prefixLength -
  121. suffixLength;
  122. middleChar = config.filler.repeat(maskedLength);
  123. } else {
  124. const ignoreCharList = config.ignoreChar.split(',');
  125. const middleStr = str.slice(prefixLength, str.length - suffixLength);
  126. const reg = new RegExp('(' + ignoreCharList.map(o => o.replace(/\*/g, '\\*')).join('|') + ')', 'g');
  127. let list = middleStr.split(reg);
  128. list = list.map(o => {
  129. if (o && !ignoreCharList.includes(o)) {
  130. const maskedLength = config.useUnrealMask ? config.unrealMaskLength || 1 : o.length;
  131. o = config.filler.repeat(maskedLength);
  132. }
  133. return o;
  134. });
  135. middleChar = list.join('');
  136. }
  137. return prefix + middleChar + suffix;
  138. }
  139. // 获取掩码后文本
  140. function getMaskedText(str) {
  141. if (!str) return '';
  142. if (config.maskType === 1) return maskAll(str);
  143. if (config.maskType === 2) return maskName(str);
  144. if (config.maskType === 3) return maskPhoneNumber(str);
  145. if (config.maskType === 4) return maskEmailAddress(str);
  146. if (config.maskType === 5) return maskIdNumber(str);
  147. if (config.maskType === 6) return maskIPAddress(str);
  148. if (config.maskType === 7) return maskLicensePlate(str);
  149. if (config.maskType === 8) return maskBankCard(str);
  150. if (config.maskType === 0) return maskCustom(str);
  151. return str;
  152. }
  153. return {
  154. maskAll,
  155. maskName,
  156. maskPhoneNumber,
  157. maskEmailAddress,
  158. maskIdNumber,
  159. maskIPAddress,
  160. maskLicensePlate,
  161. maskBankCard,
  162. maskCustom,
  163. getMaskedText
  164. };
  165. }