合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.3 KiB

  1. // @ts-nocheck
  2. import {isNumeric} from '../isNumeric'
  3. import {isDef} from '../isDef'
  4. /**
  5. * px
  6. * @param value
  7. * @returns null null
  8. */
  9. // #ifndef UNI-APP-X && APP
  10. export function addUnit(value?: string | number): string | null {
  11. if (!isDef(value)) {
  12. return null;
  13. }
  14. value = String(value); // 将值转换为字符串
  15. // 如果值是数字,则在后面添加单位 "px",否则保持原始值
  16. return isNumeric(value) ? `${value}px` : value;
  17. }
  18. // #endif
  19. // #ifdef UNI-APP-X && APP
  20. function addUnit(value: string): string
  21. function addUnit(value: number): string
  22. function addUnit(value: any|null): string|null {
  23. if (!isDef(value)) {
  24. return null;
  25. }
  26. value = `${value}` //value.toString(); // 将值转换为字符串
  27. // 如果值是数字,则在后面添加单位 "px",否则保持原始值
  28. return isNumeric(value) ? `${value}px` : value;
  29. }
  30. export {addUnit}
  31. // #endif
  32. // console.log(addUnit(100)); // 输出: "100px"
  33. // console.log(addUnit("200")); // 输出: "200px"
  34. // console.log(addUnit("300px")); // 输出: "300px"(已经包含单位)
  35. // console.log(addUnit()); // 输出: undefined(值为 undefined)
  36. // console.log(addUnit(null)); // 输出: undefined(值为 null)