index_.uts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // @ts-nocheck
  2. // #ifndef UNI-APP-X
  3. interface CSSProperties {
  4. [key : string] : string | number
  5. }
  6. // #endif
  7. // #ifdef UNI-APP-X
  8. type CSSProperties = UTSJSONObject
  9. // #endif
  10. /**
  11. * 将字符串转换为带有连字符分隔的小写形式
  12. * @param key - 要转换的字符串
  13. * @returns 转换后的字符串
  14. */
  15. export function toLowercaseSeparator(key : string) : string {
  16. return key.replace(/([A-Z])/g, '-$1').toLowerCase();
  17. }
  18. /**
  19. * 获取样式对象对应的样式字符串
  20. * @param style - CSS样式对象
  21. * @returns 由非空有效样式属性键值对组成的字符串
  22. */
  23. export function getStyleStr(style : CSSProperties) : string {
  24. let styleStr = '';
  25. style.toMap().forEach((value, key) => {
  26. if(value !== null && value != '') {
  27. styleStr += `${toLowercaseSeparator(key as string)}: ${value};`
  28. }
  29. })
  30. return styleStr
  31. }
  32. // 示例
  33. // const style = { color: 'red', fontSize: '16px', backgroundColor: '', border: null };
  34. // const styleStr = getStyleStr(style);
  35. // console.log(styleStr);
  36. // 输出: "color: red; font-size: 16px;"