index.ts 929 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // @ts-nocheck
  2. import {isString} from '../isString'
  3. import {isNumeric} from '../isNumeric'
  4. // export const unitConvert = (value: number | string): number => {
  5. // if (typeof value === 'string') {
  6. // if (value.includes('rpx')) {
  7. // return (parseInt(value, 10) * (systemInfo?.screenWidth ?? 750)) / 750;
  8. // }
  9. // return parseInt(value, 10);
  10. // }
  11. // return value;
  12. // };
  13. /**
  14. * 将 rpx | px 转成 number
  15. */
  16. export function unitConvert(value: string | number) : number{
  17. // 如果是字符串数字
  18. if (isNumeric(value)) {return Number(value)}
  19. // 如果有单位
  20. if (isString(value)) {
  21. const reg = /^-?([0-9]+)?([.]{1}[0-9]+){0,1}(em|rpx|px|%)$/g
  22. const results = reg.exec(value);
  23. if (!value || !results) {
  24. return 0;
  25. }
  26. const unit = results[3];
  27. value = parseFloat(value);
  28. if (unit === 'rpx') {
  29. return uni.upx2px(value);
  30. }
  31. if (unit === 'px') {
  32. return value * 1;
  33. }
  34. }
  35. return 0
  36. }