others.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import { toTypeString } from '@vue/shared'
  2. export const toRawType = (value: any) => toTypeString(value).slice(8, -1)
  3. export const normalizeUnitToStyle = (
  4. unit: number | string
  5. ): number | string => {
  6. if (unit === 0) {
  7. return unit
  8. } else if (typeof unit === 'number') {
  9. return unit ? (unit <= 1 && unit >= 0 ? `${100 * unit}%` : `${unit}px`) : 0
  10. } else if (unit.includes('px')) {
  11. return normalizeUnitToStyle(Number.parseFloat(unit))
  12. } else if (unit.includes('%')) {
  13. return normalizeUnitToStyle(Number.parseFloat(unit) / 100)
  14. } else {
  15. return Number(unit)
  16. }
  17. }
  18. export const os = () => {
  19. const ua = navigator ? navigator.userAgent : ''
  20. const isWindowsPhone = /(?:Windows Phone)/.test(ua)
  21. const isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone
  22. const isAndroid = /(?:Android)/.test(ua)
  23. const isFireFox = /(?:Firefox)/.test(ua)
  24. // const isChrome = /(?:Chrome|CriOS)/.test(ua);
  25. let isTablet =
  26. /(?:iPad|PlayBook)/.test(ua) ||
  27. (isAndroid && !/(?:Mobile)/.test(ua)) ||
  28. (isFireFox && /(?:Tablet)/.test(ua))
  29. const isPhone = /(?:iPhone)/.test(ua) && !isTablet
  30. const isPc = !isPhone && !isAndroid && !isSymbian
  31. if (isPc && navigator && navigator.maxTouchPoints > 1) {
  32. isTablet = true
  33. }
  34. return {
  35. isTablet,
  36. isPhone,
  37. isAndroid,
  38. isPc,
  39. }
  40. }