合同小程序前端代码仓库
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.

52 lines
1.3 KiB

  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"