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