index.ts 740 B

123456789101112131415161718192021222324252627
  1. // @ts-nocheck
  2. /**
  3. * 获取对象的类名字符串
  4. * @param obj - 需要处理的对象
  5. * @returns 由对象属性作为类名组成的字符串
  6. */
  7. export function getClassStr<T>(obj: T): string {
  8. let classNames: string[] = [];
  9. // 遍历对象的属性
  10. for (let key in obj) {
  11. // 检查属性确实属于对象自身且其值为true
  12. if ((obj as any).hasOwnProperty(key) && obj[key]) {
  13. // 将属性名添加到类名数组中
  14. classNames.push(key);
  15. }
  16. }
  17. // 将类名数组用空格连接成字符串并返回
  18. return classNames.join(' ');
  19. }
  20. // 示例
  21. // const obj = { foo: true, bar: false, baz: true };
  22. // const classNameStr = getClassStr(obj);
  23. // console.log(classNameStr); // 输出: "foo baz"