common.ts 719 B

123456789101112131415161718192021222324252627282930313233
  1. import { padStart } from 'lodash'
  2. /**
  3. * 补足数字位数
  4. * @param digit 数字
  5. * @param len 位数
  6. */
  7. export const fillDigit = (digit: number, len: number) => {
  8. return padStart('' + digit, len, '0')
  9. }
  10. /**
  11. * 判断设备
  12. */
  13. export const isPC = () => {
  14. // return !navigator.userAgent.match(/(iPhone|iPod|iPad|Android|Mobile|BlackBerry|Symbian|Windows Phone)/i)
  15. return true
  16. }
  17. /**
  18. * 判断URL字符串
  19. */
  20. export const isValidURL = (url: string) => {
  21. return /^(https?:\/\/)([\w-]+\.)+[\w-]{2,}(\/[\w-./?%&=]*)?$/i.test(url)
  22. }
  23. /**
  24. * HTML转纯文本
  25. */
  26. export const htmlToText = (html: string) => {
  27. const doc = new DOMParser().parseFromString(html, 'text/html')
  28. return doc.body.textContent || ''
  29. }