concept-tree 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -1
- package/dist/index.js +40 -13
- package/package.json +5 -3
package/README.md
CHANGED
|
@@ -7,15 +7,70 @@ Presentation
|
|
|
7
7
|
|
|
8
8
|
The package *concept-tree* helps to make a virtual tree structure and output it in different ways.
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
Alternative solutions:
|
|
11
11
|
|
|
12
12
|
- https://github.com/D-Sketon/fast-archy
|
|
13
13
|
- https://mermaid.ai/open-source/syntax/treeView.html
|
|
14
14
|
|
|
15
15
|
|
|
16
|
+
Links
|
|
17
|
+
-----
|
|
18
|
+
|
|
19
|
+
- [sources](https://github.com/charlyoleg2/bopa_mono/tree/main/pkg/concept-tree)
|
|
20
|
+
- [pkg](https://www.npmjs.com/package/concept-tree)
|
|
21
|
+
|
|
22
|
+
|
|
16
23
|
Getting started
|
|
17
24
|
---------------
|
|
18
25
|
|
|
26
|
+
```bash
|
|
27
|
+
npm i concept-tree@latest
|
|
28
|
+
vim blabla.ts
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
blabla.ts:
|
|
32
|
+
```JavaScript
|
|
33
|
+
import { conceptTree } from '../src/index.ts';
|
|
34
|
+
|
|
35
|
+
// create a tree
|
|
36
|
+
const myTree = conceptTree('aaa');
|
|
37
|
+
myTree.addPath('xxx/zzz');
|
|
38
|
+
myTree.addPath('xxx/yyy');
|
|
39
|
+
myTree.addSubTree(otherTree);
|
|
40
|
+
myTree.addElem('Abc');
|
|
41
|
+
|
|
42
|
+
// get a sub-tree
|
|
43
|
+
const subTree = myTree.find('xxx')
|
|
44
|
+
|
|
45
|
+
// configure the output
|
|
46
|
+
myTree.setPrefix(0); // 0, 1, 2
|
|
47
|
+
myTree.setRootPrefix(true);
|
|
48
|
+
myTree.setNumbering(true);
|
|
49
|
+
myTree.setSorting(true);
|
|
50
|
+
|
|
51
|
+
// outputs
|
|
52
|
+
const lElem = myTree.listElem(); // string[]
|
|
53
|
+
const txt = myTree.drawTree(); // string
|
|
54
|
+
const lPath = myTree.listTree(); // string[]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Combining with Mermaid.js
|
|
58
|
+
-------------------------
|
|
59
|
+
|
|
60
|
+
```mermaid
|
|
61
|
+
treeView-beta
|
|
62
|
+
├── src/
|
|
63
|
+
│ ├── App.tsx ## main component
|
|
64
|
+
│ └── index.ts ## entry point
|
|
65
|
+
├── .env ## environment variables
|
|
66
|
+
├── Dockerfile
|
|
67
|
+
└── package.json
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
Development
|
|
72
|
+
-----------
|
|
73
|
+
|
|
19
74
|
```bash
|
|
20
75
|
git clone https://github.com/charlyoleg2/bopa_mono
|
|
21
76
|
cd bopa_mono
|
package/dist/index.js
CHANGED
|
@@ -3,17 +3,20 @@ const cBDchar = [
|
|
|
3
3
|
[
|
|
4
4
|
"├──",
|
|
5
5
|
"└──",
|
|
6
|
-
"│"
|
|
6
|
+
"│",
|
|
7
|
+
"─"
|
|
7
8
|
],
|
|
8
9
|
[
|
|
9
10
|
"+--",
|
|
10
11
|
"+--",
|
|
11
|
-
"|"
|
|
12
|
+
"|",
|
|
13
|
+
"-"
|
|
12
14
|
],
|
|
13
15
|
[
|
|
14
16
|
" ",
|
|
15
17
|
" ",
|
|
16
|
-
" "
|
|
18
|
+
" ",
|
|
19
|
+
""
|
|
17
20
|
]
|
|
18
21
|
];
|
|
19
22
|
var ConceptTree = class ConceptTree {
|
|
@@ -21,9 +24,12 @@ var ConceptTree = class ConceptTree {
|
|
|
21
24
|
this.name = "";
|
|
22
25
|
this.sub = [];
|
|
23
26
|
this.prefixx = 0;
|
|
27
|
+
this.rootPrefix = true;
|
|
24
28
|
this.numbering = true;
|
|
25
29
|
this.sorting = true;
|
|
26
|
-
|
|
30
|
+
const lName = iName.replace(/$\//, "").replace(/\/^/, "");
|
|
31
|
+
if (/\//.test(lName)) throw new Error(`ERR019: ${lName} constains slash (/)! Empty string will be used`);
|
|
32
|
+
else if (!["", "."].includes(lName)) this.name = lName;
|
|
27
33
|
}
|
|
28
34
|
getPrefix() {
|
|
29
35
|
return cBDchar[this.prefixx];
|
|
@@ -32,6 +38,9 @@ var ConceptTree = class ConceptTree {
|
|
|
32
38
|
const id = Math.max(0, Math.min(iId, cBDchar.length - 1));
|
|
33
39
|
this.prefixx = id;
|
|
34
40
|
}
|
|
41
|
+
setRootPrefix(iRootPrefix) {
|
|
42
|
+
this.rootPrefix = iRootPrefix;
|
|
43
|
+
}
|
|
35
44
|
setNumbering(iNumbering) {
|
|
36
45
|
this.numbering = iNumbering;
|
|
37
46
|
}
|
|
@@ -47,6 +56,7 @@ var ConceptTree = class ConceptTree {
|
|
|
47
56
|
return rTree;
|
|
48
57
|
}
|
|
49
58
|
addSubTree(iTree) {
|
|
59
|
+
if (iTree.name === "") throw new Error("ERR064: subTree without name can not be added!");
|
|
50
60
|
let pTree = this.findElem(iTree.name);
|
|
51
61
|
if (pTree === null) {
|
|
52
62
|
this.sub.push(iTree);
|
|
@@ -55,6 +65,9 @@ var ConceptTree = class ConceptTree {
|
|
|
55
65
|
return pTree;
|
|
56
66
|
}
|
|
57
67
|
addElem(iName) {
|
|
68
|
+
if (/\s/.test(iName)) throw new Error(`ERR082: the element '${iName}' contains a space!`);
|
|
69
|
+
if (/\//.test(iName)) throw new Error(`ERR087: the element ${iName} contains a slash (/)!`);
|
|
70
|
+
if (iName === "") throw new Error("ERR092: the added element is empty");
|
|
58
71
|
let pTree = this.findElem(iName);
|
|
59
72
|
if (pTree === null) {
|
|
60
73
|
const nTree = new ConceptTree(iName);
|
|
@@ -74,11 +87,23 @@ var ConceptTree = class ConceptTree {
|
|
|
74
87
|
}
|
|
75
88
|
_genTxtDiag(iRoot, iLast, iPad, idx) {
|
|
76
89
|
const rList = [];
|
|
90
|
+
let nextFirst = false;
|
|
91
|
+
const numb = this.numbering ? `${(idx + 1).toString().padStart(3)}.` : "";
|
|
92
|
+
let rootPrefLen = 0;
|
|
77
93
|
if (iRoot) {
|
|
78
|
-
if (this.name
|
|
94
|
+
if (this.name === "") nextFirst = true;
|
|
95
|
+
else {
|
|
96
|
+
let rootPref = "";
|
|
97
|
+
if (this.rootPrefix) {
|
|
98
|
+
const rootPref2 = this.getPrefix()[3];
|
|
99
|
+
if (this.numbering) rootPref = `${rootPref2}${numb} `;
|
|
100
|
+
else rootPref = rootPref2.length > 0 ? `${rootPref2} ` : "";
|
|
101
|
+
}
|
|
102
|
+
rList.push(`${iPad}${rootPref}${this.name}`);
|
|
103
|
+
rootPrefLen = rootPref.length;
|
|
104
|
+
}
|
|
79
105
|
} else {
|
|
80
106
|
const pref = iLast ? this.getPrefix()[1] : this.getPrefix()[0];
|
|
81
|
-
const numb = this.numbering ? `${(idx + 1).toString().padStart(3)}.` : "";
|
|
82
107
|
rList.push(`${iPad}${pref}${numb} ${this.name}`);
|
|
83
108
|
}
|
|
84
109
|
const lName = this.listElem();
|
|
@@ -87,15 +112,15 @@ var ConceptTree = class ConceptTree {
|
|
|
87
112
|
const eTree = this.findElem(elem);
|
|
88
113
|
if (eTree !== null) {
|
|
89
114
|
eTree.setPrefix(this.prefixx);
|
|
115
|
+
eTree.setRootPrefix(this.rootPrefix);
|
|
90
116
|
eTree.setNumbering(this.numbering);
|
|
91
117
|
eTree.setSorting(this.sorting);
|
|
92
118
|
const last = idx2 === this.sub.length - 1 ? true : false;
|
|
93
119
|
let lePad = iPad;
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
rList.push(...eTree._genTxtDiag(false, last, lePad, idx2));
|
|
120
|
+
if (iRoot) lePad += " ".repeat(rootPrefLen);
|
|
121
|
+
else if (iLast) lePad += " ".repeat(this.numbering ? 8 : 4);
|
|
122
|
+
else lePad += this.getPrefix()[2] + " ".repeat(this.numbering ? 7 : 3);
|
|
123
|
+
rList.push(...eTree._genTxtDiag(nextFirst, last, lePad, idx2));
|
|
99
124
|
} else console.log("ERR127: this should never happen!");
|
|
100
125
|
}
|
|
101
126
|
return rList;
|
|
@@ -105,9 +130,11 @@ var ConceptTree = class ConceptTree {
|
|
|
105
130
|
}
|
|
106
131
|
_genPathList(iRoot, iPad) {
|
|
107
132
|
const rList = [];
|
|
133
|
+
let nextFirst = false;
|
|
108
134
|
let lePad = iPad;
|
|
109
135
|
if (iRoot) {
|
|
110
|
-
if (this.name
|
|
136
|
+
if (this.name === "") nextFirst = true;
|
|
137
|
+
else {
|
|
111
138
|
lePad = `${iPad}${this.name}`;
|
|
112
139
|
rList.push(lePad);
|
|
113
140
|
}
|
|
@@ -115,7 +142,7 @@ var ConceptTree = class ConceptTree {
|
|
|
115
142
|
lePad = `${iPad}/${this.name}`;
|
|
116
143
|
rList.push(lePad);
|
|
117
144
|
}
|
|
118
|
-
for (const elem of this.sub) rList.push(...elem._genPathList(
|
|
145
|
+
for (const elem of this.sub) rList.push(...elem._genPathList(nextFirst, lePad));
|
|
119
146
|
return rList;
|
|
120
147
|
}
|
|
121
148
|
listTree() {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "concept-tree",
|
|
3
3
|
"description": "build and output virtual tree",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.2",
|
|
5
5
|
"private": false,
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -62,7 +62,9 @@
|
|
|
62
62
|
"test:unit": "vitest",
|
|
63
63
|
"test:unit:once": "vitest --run",
|
|
64
64
|
"ci": "run-s check build pretty lint test:unit:once",
|
|
65
|
-
"
|
|
65
|
+
"run1": "tsx scr/run-concept-tree.ts",
|
|
66
|
+
"run2": "tsx scr/loop-settings.ts",
|
|
67
|
+
"runAll": "run-s run1 run2",
|
|
66
68
|
"clean": "rimraf node_modules build dist tmp"
|
|
67
69
|
},
|
|
68
70
|
"devDependencies": {
|
|
@@ -71,7 +73,7 @@
|
|
|
71
73
|
"@types/node": "^26.6.6",
|
|
72
74
|
"eslint": "^10.15.0",
|
|
73
75
|
"eslint-config-prettier": "^10.1.8",
|
|
74
|
-
"npm-run-all2": "^9.0
|
|
76
|
+
"npm-run-all2": "^9.1.0",
|
|
75
77
|
"prettier": "^3.10.1",
|
|
76
78
|
"rimraf": "^6.1.4",
|
|
77
79
|
"tsdown": "^0.23.7",
|