1
0

index.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // @ts-nocheck
  2. // #ifdef UNI-APP-X && APP
  3. import { isNumber } from '../isNumber'
  4. import { isString } from '../isString'
  5. import { isDef } from '../isDef'
  6. // #endif
  7. /**
  8. * 获取对象的类名字符串
  9. * @param obj - 需要处理的对象
  10. * @returns 由对象属性作为类名组成的字符串
  11. */
  12. export function getClassStr<T>(obj : T) : string {
  13. let classNames : string[] = [];
  14. // #ifdef UNI-APP-X && APP
  15. if (obj instanceof UTSJSONObject) {
  16. (obj as UTSJSONObject).toMap().forEach((value, key) => {
  17. if (isDef(value)) {
  18. if (isNumber(value)) {
  19. classNames.push(key);
  20. }
  21. if (isString(value) && value !== '') {
  22. classNames.push(key);
  23. }
  24. if (typeof value == 'boolean' && (value as boolean)) {
  25. classNames.push(key);
  26. }
  27. }
  28. })
  29. }
  30. // #endif
  31. // #ifndef UNI-APP-X && APP
  32. // 遍历对象的属性
  33. for (let key in obj) {
  34. // 检查属性确实属于对象自身且其值为true
  35. if ((obj as any).hasOwnProperty(key) && obj[key]) {
  36. // 将属性名添加到类名数组中
  37. classNames.push(key);
  38. }
  39. }
  40. // #endif
  41. // 将类名数组用空格连接成字符串并返回
  42. return classNames.join(' ');
  43. }
  44. // 示例
  45. // const obj = { foo: true, bar: false, baz: true };
  46. // const classNameStr = getClassStr(obj);
  47. // console.log(classNameStr); // 输出: "foo baz"