-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataConversion.ts
43 lines (36 loc) · 1.01 KB
/
DataConversion.ts
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
import Stack from "./lib/Stack";
type nodeObj = { name: string; children?: Array<nodeObj> };
/**
* 字符串转树结构
* @param text
* @constructor
*/
export function DataConversion(text: string): nodeObj {
const splitArr = text.split("\n");
const json = { name: "root" };
const strStack = new Stack();
const deepStack = new Stack();
strStack.push(json);
deepStack.push(-1);
for (let i = 0; i < splitArr.length; i++) {
let str = splitArr[i];
if (!str) continue;
// 获取空格总数
const len = str.lastIndexOf(" ") + 1;
str = str.replace(/\s/g, "");
const curObj = { name: str };
// 寻找当前入栈元素的父层级
while (len <= deepStack.peek()) {
deepStack.pop();
strStack.pop();
}
const stackTop: nodeObj = strStack.peek();
stackTop.children
? stackTop.children.push(curObj)
: (stackTop.children = [curObj]);
// 元素入栈,继续下一轮的比对
strStack.push(curObj);
deepStack.push(len);
}
return json;
}