concept-tree 1.0.1 → 1.0.3

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.
Files changed (3) hide show
  1. package/README.md +17 -0
  2. package/dist/index.js +41 -14
  3. package/package.json +5 -3
package/README.md CHANGED
@@ -13,6 +13,13 @@ Alternative solutions:
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
 
@@ -37,6 +44,7 @@ const subTree = myTree.find('xxx')
37
44
 
38
45
  // configure the output
39
46
  myTree.setPrefix(0); // 0, 1, 2
47
+ myTree.setRootPrefix(true);
40
48
  myTree.setNumbering(true);
41
49
  myTree.setSorting(true);
42
50
 
@@ -49,6 +57,15 @@ const lPath = myTree.listTree(); // string[]
49
57
  Combining with Mermaid.js
50
58
  -------------------------
51
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
+ ```
52
69
 
53
70
 
54
71
  Development
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
- if (!["", "."].includes(iName)) this.name = iName;
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 !== "") rList.push(`${iPad}${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 (!iRoot) {
95
- if (iLast) lePad += " ".repeat(this.numbering ? 8 : 4);
96
- else lePad += this.getPrefix()[2] + " ".repeat(this.numbering ? 7 : 3);
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,11 +142,11 @@ 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(false, lePad));
145
+ for (const elem of this.sub) rList.push(...elem._genPathList(nextFirst, lePad));
119
146
  return rList;
120
147
  }
121
148
  listTree() {
122
- return this._genPathList(true, " ");
149
+ return this._genPathList(true, "");
123
150
  }
124
151
  };
125
152
  function conceptTree(iName) {
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.1",
4
+ "version": "1.0.3",
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
- "run": "tsx scr/run-concept-tree.ts",
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.4",
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",