From 4afe0f75cd78e8f668f79df978a0acc4281024d6 Mon Sep 17 00:00:00 2001 From: * <8> Date: Tue, 29 Mar 2022 17:58:24 +0800 Subject: [PATCH] Match-id-71142987658f65616d22affbe9fbf5d524112007 --- libs/extension/src/parser/parseAttr.ts | 76 ++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 libs/extension/src/parser/parseAttr.ts diff --git a/libs/extension/src/parser/parseAttr.ts b/libs/extension/src/parser/parseAttr.ts new file mode 100644 index 00000000..0c5335d1 --- /dev/null +++ b/libs/extension/src/parser/parseAttr.ts @@ -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; +}