index.ts 907 B

12345678910111213141516171819202122232425
  1. // @ts-nocheck
  2. import {isNumeric} from '../isNumeric'
  3. import {isDef} from '../isDef'
  4. /**
  5. * 给一个值添加单位(像素 px)
  6. * @param value 要添加单位的值,可以是字符串或数字
  7. * @returns 添加了单位的值,如果值为 undefined 则返回 undefined
  8. */
  9. export function addUnit(value?: string | number): string | undefined {
  10. if (!isDef(value)) {
  11. return undefined;
  12. }
  13. value = String(value); // 将值转换为字符串
  14. // 如果值是数字,则在后面添加单位 "px",否则保持原始值
  15. return isNumeric(value) ? `${value}px` : value;
  16. }
  17. // console.log(addUnit(100)); // 输出: "100px"
  18. // console.log(addUnit("200")); // 输出: "200px"
  19. // console.log(addUnit("300px")); // 输出: "300px"(已经包含单位)
  20. // console.log(addUnit()); // 输出: undefined(值为 undefined)
  21. // console.log(addUnit(null)); // 输出: undefined(值为 null)