@@ -56,48 +56,66 @@ interface Tree {
56
56
}
57
57
const ID = () => Math .random ().toString (36 ).slice (2 )
58
58
// below code is aim to parse the key name to tree structure
59
- const parseLevel = (ori : Tree [], key : string ) => {
60
- // todo: split symbol should be configurable.
61
- // if not matched with `:` add it to tree structure immediately.
62
- if (! key .includes (' :' )) {
63
- ori .push ({ label: key , icon: " key" , type: ' key' , id: ID () })
64
- return
59
+ const parseLevel = (ori : Tree [], key : string , delimiter : string = ' :' ) => {
60
+ // Handle empty key
61
+ if (! key ) {
62
+ return ;
65
63
}
66
- // otherwise split it to and parse it to tree structure.
67
- const arr = key .split (' :' )
68
- // append children to root
64
+
65
+ // If not matched with delimiter, add it to tree structure immediately.
66
+ if (! key .includes (delimiter )) {
67
+ ori .push (createTreeNode (key , ' key' ));
68
+ return ;
69
+ }
70
+
71
+ // Otherwise split it and parse it to tree structure.
72
+ const arr = key .split (delimiter );
73
+
69
74
const appendChildren = (ori : Tree [], arr : string []) => {
70
- // search root key.
71
- const root = ori .find ((e ) => e .label === arr [0 ] && e .type != ' key' )
72
- // finded root key
75
+ // Search root key.
76
+ const root = ori .find ((e ) => e .label === arr [0 ] && e .type !== ' key' );
77
+
78
+ // Found root key
73
79
if (root ) {
74
- // if not children, create it.
75
- if (! root .children ) root .children = []
76
- // recursive parse it.
77
- appendChildren (root .children , arr .splice (1 ))
80
+ // If not children, create it.
81
+ if (! root .children ) root .children = [];
82
+ // Recursive parse it.
83
+ appendChildren (root .children , arr .slice (1 ));
78
84
} else {
79
- // if key length is less than 2, it means it is a key.
85
+ // If key length is less than 2, it means it is a key.
80
86
if (arr .length <= 1 ) {
81
- const obj = { label: key , icon: " key" , type: ' key' , id: ID () } as Tree
82
- ori .push (obj )
83
- return
87
+ ori .push (createTreeNode (key , ' key' ));
88
+ return ;
84
89
}
85
- // otherwise create as a folder.
86
- const obj = { label: arr [0 ], icon: " folder" , type: ' folder' , children: [], id: ID () } as Tree
87
- // push it to as root.
88
- ori .push (obj )
89
- // and recursive parse it.
90
- appendChildren (obj .children ! , arr .splice (1 ))
90
+ // Otherwise create as a folder.
91
+ const obj = createTreeNode (arr [0 ], ' folder' );
92
+ obj .children = [];
93
+ // Push it to as root.
94
+ ori .push (obj );
95
+ // And recursive parse it.
96
+ appendChildren (obj .children ! , arr .slice (1 ));
91
97
}
98
+ };
99
+
100
+ // Parse it
101
+ try {
102
+ appendChildren (ori , arr );
103
+ } catch (error ) {
104
+ console .error (' Error parsing level:' , error );
92
105
}
93
- // parse it
94
- appendChildren (ori , arr )
95
- }
106
+ };
107
+
108
+ const createTreeNode = (label : string , type : ' key' | ' folder' ): Tree => {
109
+ try {
110
+ return { label , icon: type === ' key' ? " key" : " folder" , type , id: ID () };
111
+ } catch (error ) {
112
+ console .error (' Error creating tree node:' , error );
113
+ throw error ;
114
+ }
115
+ };
96
116
// todo: configurable name space enable.
97
117
const nameSpaceEnable = ref (true )
98
- const icon = (e ) => {
99
- console .log (e .target )
100
- }
118
+
101
119
</script >
102
120
<template >
103
121
<div class =" flex flex-col h-full" >
0 commit comments