Match-id-71142987658f65616d22affbe9fbf5d524112007

This commit is contained in:
* 2022-03-29 17:58:24 +08:00 committed by *
parent e78feb8712
commit 4afe0f75cd
1 changed files with 76 additions and 0 deletions

View File

@ -0,0 +1,76 @@
export function parseAttr(rootAttr: any) {
const result = [];
let indentation = 0;
const parseSubAttr = (attr: any, parentIndentation: number, attrName: string) => {
const stateType = typeof attr;
let value: any;
let showType;
let addSubState;
if (stateType === 'boolean' ||
stateType === 'number' ||
stateType === 'string' ||
stateType === 'undefined') {
value = attr;
showType = stateType;
} else if (stateType === 'function') {
const funName = attr.name;
value = `f() ${funName}{}`;
} else if (stateType === 'symbol') {
value = attr.description;
} else if (stateType === 'object') {
if (attr === null) {
showType = 'null';
}else if (attr instanceof Map) {
showType = 'map';
const size = attr.size;
value = `Map(${size})`;
addSubState = () => {
attr.forEach((value, key) => {
parseSubAttr(value, parentIndentation + 2, key);
});
}
} else if (attr instanceof Set) {
showType = 'set';
const size = attr.size;
value = `Set(${size})`;
addSubState = () => {
let i = 0;
attr.forEach((value) => {
parseSubAttr(value, parentIndentation + 2, String(i));
});
i++;
};
} else if (Array.isArray(attr)) {
showType = 'array';
value = `Array(${attr.length})`;
addSubState = () => {
attr.forEach((value, index) => {
parseSubAttr(value, parentIndentation + 2, String(index));
})
}
} else {
showType = stateType;
value = '{...}';
addSubState = () => {
Object.keys(attr).forEach((key) => {
parseSubAttr(attr[key], parentIndentation + 2, key);
});
};
}
}
result.push({
name: attrName,
type: showType,
value,
indentation: parentIndentation + 1,
});
if (addSubState) {
addSubState();
}
};
Object.keys(rootAttr).forEach(key => {
parseSubAttr(rootAttr[key], indentation, key);
});
return result;
}