validate.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * 路径匹配器
  3. * @param {string} pattern
  4. * @param {string} path
  5. * @returns {Boolean}
  6. */
  7. export function isPathMatch(pattern: string, path: string) {
  8. const regexPattern = pattern
  9. .replace(/\//g, '\\/')
  10. .replace(/\*\*/g, '__DOUBLE_STAR__')
  11. .replace(/\*/g, '[^\\/]*')
  12. .replace(/__DOUBLE_STAR__/g, '.*');
  13. const regex = new RegExp(`^${regexPattern}$`);
  14. return regex.test(path);
  15. }
  16. /**
  17. * 判断url是否是http或https
  18. * @returns {Boolean}
  19. * @param url
  20. */
  21. export const isHttp = (url: string): boolean => {
  22. return url.indexOf('http://') !== -1 || url.indexOf('https://') !== -1;
  23. };
  24. /**
  25. * 判断path是否为外链
  26. * @param {string} path
  27. * @returns {Boolean}
  28. */
  29. export const isExternal = (path: string) => {
  30. return /^(https?:|mailto:|tel:)/.test(path);
  31. };
  32. /**
  33. * @param {string} str
  34. * @returns {Boolean}
  35. */
  36. export const validUsername = (str: string) => {
  37. const valid_map = ['admin', 'editor'];
  38. return valid_map.indexOf(str.trim()) >= 0;
  39. };
  40. /**
  41. * @param {string} url
  42. * @returns {Boolean}
  43. */
  44. export const validURL = (url: string) => {
  45. const reg =
  46. /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  47. return reg.test(url);
  48. };
  49. /**
  50. * @param {string} str
  51. * @returns {Boolean}
  52. */
  53. export const validLowerCase = (str: string) => {
  54. const reg = /^[a-z]+$/;
  55. return reg.test(str);
  56. };
  57. /**
  58. * @param {string} str
  59. * @returns {Boolean}
  60. */
  61. export const validUpperCase = (str: string) => {
  62. const reg = /^[A-Z]+$/;
  63. return reg.test(str);
  64. };
  65. /**
  66. * @param {string} str
  67. * @returns {Boolean}
  68. */
  69. export const validAlphabets = (str: string) => {
  70. const reg = /^[A-Za-z]+$/;
  71. return reg.test(str);
  72. };
  73. /**
  74. * @param {string} email
  75. * @returns {Boolean}
  76. */
  77. export const validEmail = (email: string) => {
  78. const reg =
  79. /^(([^<>()\]\\.,;:\s@"]+(\.[^<>()\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  80. return reg.test(email);
  81. };
  82. /**
  83. * @param {string} str
  84. * @returns {Boolean}
  85. */
  86. export const isString = (str: any) => {
  87. return typeof str === 'string' || str instanceof String;
  88. };
  89. /**
  90. * @param {Array} arg
  91. * @returns {Boolean}
  92. */
  93. export const isArray = (arg: string | string[]) => {
  94. if (typeof Array.isArray === 'undefined') {
  95. return Object.prototype.toString.call(arg) === '[object Array]';
  96. }
  97. return Array.isArray(arg);
  98. };