validate.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @param {string} path
  3. * @returns {Boolean}
  4. */
  5. export function isExternal(path: string): boolean {
  6. return /^(https?:|mailto:|tel:)/.test(path)
  7. }
  8. /**
  9. * @param {string} str
  10. * @returns {Boolean}
  11. */
  12. export function validUsername(str: string): boolean {
  13. const valid_map = ['admin', 'editor']
  14. return valid_map.indexOf(str.trim()) >= 0
  15. }
  16. /**
  17. * @param {string} url
  18. * @returns {Boolean}
  19. */
  20. export function validURL(url: string): boolean {
  21. const reg = /^(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.,?'\\+&%$#=~_-]+))*$/
  22. return reg.test(url)
  23. }
  24. /**
  25. * @param {string} str
  26. * @returns {Boolean}
  27. */
  28. export function validLowerCase(str: string): boolean {
  29. const reg = /^[a-z]+$/
  30. return reg.test(str)
  31. }
  32. /**
  33. * @param {string} str
  34. * @returns {Boolean}
  35. */
  36. export function validUpperCase(str: string): boolean {
  37. const reg = /^[A-Z]+$/
  38. return reg.test(str)
  39. }
  40. /**
  41. * @param {string} str
  42. * @returns {Boolean}
  43. */
  44. export function validAlphabets(str: string): boolean {
  45. const reg = /^[A-Za-z]+$/
  46. return reg.test(str)
  47. }
  48. /**
  49. * @param {string} email
  50. * @returns {Boolean}
  51. */
  52. // export function validEmail(email: string): boolean {
  53. // const reg = /^(([^<>()\[\]\\.,;:\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,}))$/
  54. // return reg.test(email)
  55. // }
  56. /**
  57. * @param {string} str
  58. * @returns {Boolean}
  59. */
  60. export function isString(str: any): boolean {
  61. return typeof str === 'string' || str instanceof String
  62. }
  63. /**
  64. * @param {Array} arg
  65. * @returns {Boolean}
  66. */
  67. export function isValue(arg: []) {
  68. if (Array.isArray(arg)) {
  69. return arg.length
  70. } else {
  71. if (typeof arg === 'object') {
  72. return Object.keys(arg).length
  73. } else {
  74. return false
  75. }
  76. }
  77. }
  78. /**
  79. * Parse the time to string
  80. * @param {(Object|string|number)} time
  81. * @param {string} cFormat
  82. * @returns {string | null}
  83. */
  84. export function parseTime(time: any, cFormat: string) {
  85. if (arguments.length === 0 || !time) {
  86. return null
  87. }
  88. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  89. let date
  90. if (typeof time === 'object') {
  91. date = time
  92. } else {
  93. if (typeof time === 'string') {
  94. if (/^[0-9]+$/.test(time)) {
  95. // support "1548221490638"
  96. time = parseInt(time)
  97. } else {
  98. // support safari
  99. // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
  100. time = time.replace(new RegExp(/-/gm), '/')
  101. }
  102. }
  103. if (typeof time === 'number' && time.toString().length === 10) {
  104. time = time * 1000
  105. }
  106. date = new Date(time)
  107. }
  108. const formatObj = {
  109. y: date.getFullYear(),
  110. m: date.getMonth() + 1,
  111. d: date.getDate(),
  112. h: date.getHours(),
  113. i: date.getMinutes(),
  114. s: date.getSeconds(),
  115. a: date.getDay(),
  116. }
  117. const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
  118. const value = formatObj[key]
  119. // Note: getDay() returns 0 on Sunday
  120. if (key === 'a') {
  121. return ['日', '一', '二', '三', '四', '五', '六'][value]
  122. }
  123. return value.toString().padStart(2, '0')
  124. })
  125. return time_str
  126. }
  127. export function translateDataToTreeAll(arr, parentKey, key) {
  128. const map = {}
  129. const result = []
  130. arr.forEach((element) => {
  131. const id = element[key]
  132. const pid = element[parentKey]
  133. if (map[id]) {
  134. map[id] = {
  135. ...element,
  136. children: map[id].children,
  137. }
  138. } else {
  139. map[id] = {
  140. ...element,
  141. children: [],
  142. }
  143. }
  144. const item = map[id]
  145. if (pid <= 0) {
  146. result.push(item)
  147. } else {
  148. if (map[pid]) {
  149. map[pid].children.push(item)
  150. } else {
  151. map[pid] = {
  152. children: [item],
  153. }
  154. }
  155. }
  156. })
  157. return result
  158. }