fast-tree-builder 0.1.1 → 0.1.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.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # fast-tree-builder
2
2
 
3
- [![Build Status](https://github.com/lionel87/fast-tree-builder/actions/workflows/coveralls.yaml/badge.svg)](https://github.com/lionel87/fast-tree-builder/actions/workflows/coveralls.yaml)
3
+ [![Build Status](https://github.com/lionel87/fast-tree-builder/actions/workflows/build.yaml/badge.svg)](https://github.com/lionel87/fast-tree-builder/actions/workflows/build.yaml)
4
4
  [![Coverage Status](https://coveralls.io/repos/github/lionel87/fast-tree-builder/badge.svg?branch=master)](https://coveralls.io/github/lionel87/fast-tree-builder?branch=master)
5
5
  ![npms.io (quality)](https://img.shields.io/npms-io/quality-score/fast-tree-builder?label=quality)
6
6
  ![Maintenance](https://img.shields.io/maintenance/yes/2023)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fast-tree-builder",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Efficiently construct highly customizable bi-directional tree structures from iterable data.",
5
5
  "type": "module",
6
6
  "main": "cjs/index.js",
@@ -13,14 +13,16 @@
13
13
  }
14
14
  },
15
15
  "scripts": {
16
- "prepack": "npm run build",
17
- "clean": "rimraf esm cjs",
16
+ "prepack": "npm run build && npm run test",
17
+ "postversion": "git push && git push --tags",
18
+ "clean": "rimraf esm cjs coverage",
18
19
  "build": "npm run build:esm && npm run build:cjs",
19
20
  "build:esm": "tsc",
20
21
  "watch:esm": "tsc --watch",
21
22
  "build:cjs": "tsc --outDir cjs --module commonjs && echo { \"type\": \"commonjs\" }>cjs/package.json",
22
23
  "watch:cjs": "npm run build:cjs && tsc --outDir cjs --module commonjs --watch",
23
- "test": "mocha tests/*.js --timeout 10000"
24
+ "test": "mocha",
25
+ "coverage": "c8 -r text -r text-summary -r lcov npm test"
24
26
  },
25
27
  "repository": {
26
28
  "type": "git",
@@ -56,8 +58,7 @@
56
58
  },
57
59
  "homepage": "https://github.com/lionel87/fast-tree-builder#readme",
58
60
  "devDependencies": {
59
- "@types/chai": "^4.3.5",
60
- "chai": "^4.3.7",
61
+ "c8": "^8.0.0",
61
62
  "mocha": "^10.2.0",
62
63
  "rimraf": "^5.0.1",
63
64
  "typescript": "^5.1.3"
@@ -1,132 +0,0 @@
1
- import { expect } from 'chai';
2
- import buildTree from '../esm/index.js';
3
-
4
- describe('buildTree', () => {
5
- it('should build a tree with root and child nodes', () => {
6
- const items = [
7
- { id: 1, parent: null, name: 'Root 1' },
8
- { id: 2, parent: null, name: 'Root 2' },
9
- { id: 3, parent: 1, name: 'Child 1.1' },
10
- { id: 4, parent: 1, name: 'Child 1.2' },
11
- { id: 5, parent: 2, name: 'Child 2.1' },
12
- ];
13
-
14
- const { roots, nodes } = buildTree(items);
15
-
16
- expect(roots).to.have.lengthOf(2);
17
- expect(nodes).to.have.keys([1, 2, 3, 4, 5]);
18
-
19
- expect(roots[0].data.name).to.equal('Root 1');
20
- expect(roots[0].children[0].data.name).to.equal('Child 1.1');
21
- expect(roots[0].children[0].parent.data.name).to.equal('Root 1');
22
- expect(roots[0].children[1].data.name).to.equal('Child 1.2');
23
- expect(roots[0].children[1].parent.data.name).to.equal('Root 1');
24
- expect(roots[1].data.name).to.equal('Root 2');
25
- expect(roots[1].children[0].data.name).to.equal('Child 2.1');
26
- });
27
-
28
- it('should build a tree with customized nodes keys', () => {
29
- const items = [
30
- { id: 1, parent: null, name: 'Root 1' },
31
- { id: 2, parent: null, name: 'Root 2' },
32
- { id: 3, parent: 1, name: 'Child 1.1' },
33
- { id: 4, parent: 1, name: 'Child 1.2' },
34
- { id: 5, parent: 2, name: 'Child 2.1' },
35
- ];
36
-
37
- const { roots, nodes } = buildTree(items, {
38
- nodeDataKey: 'DATA',
39
- nodeParentKey: 'PARENT',
40
- nodeChildrenKey: 'CHILDREN',
41
- });
42
-
43
- expect(roots).to.have.lengthOf(2);
44
- expect(nodes).to.have.keys([1, 2, 3, 4, 5]);
45
-
46
- expect(roots[0].DATA.name).to.equal('Root 1');
47
- expect(roots[0].CHILDREN[0].DATA.name).to.equal('Child 1.1');
48
- expect(roots[0].CHILDREN[0].PARENT.DATA.name).to.equal('Root 1');
49
- expect(roots[0].CHILDREN[1].DATA.name).to.equal('Child 1.2');
50
- expect(roots[0].CHILDREN[1].PARENT.DATA.name).to.equal('Root 1');
51
- expect(roots[1].DATA.name).to.equal('Root 2');
52
- expect(roots[1].CHILDREN[0].DATA.name).to.equal('Child 2.1');
53
- });
54
-
55
- it('should build a tree with parent keys disabled', () => {
56
- const items = [
57
- { id: 1, parent: null, name: 'Root 1' },
58
- { id: 2, parent: null, name: 'Root 2' },
59
- { id: 3, parent: 1, name: 'Child 1.1' },
60
- { id: 4, parent: 1, name: 'Child 1.2' },
61
- { id: 5, parent: 2, name: 'Child 2.1' },
62
- ];
63
-
64
- const { roots, nodes } = buildTree(items, {
65
- nodeParentKey: false,
66
- });
67
-
68
- expect(roots[0].children[0].parent).to.be.undefined;
69
- expect(roots[0].children[1].parent).to.be.undefined;
70
- });
71
-
72
- it('should map node data when mapper fn configured', () => {
73
- const items = [
74
- { id: 1, parent: null, name: 'Root 1' },
75
- { id: 2, parent: null, name: 'Root 2' },
76
- { id: 3, parent: 1, name: 'Child 1.1' },
77
- { id: 4, parent: 1, name: 'Child 1.2' },
78
- { id: 5, parent: 2, name: 'Child 2.1' },
79
- ];
80
-
81
- const { roots, nodes } = buildTree(items, {
82
- mapNodeData(item) {
83
- return { title: item.name };
84
- }
85
- });
86
-
87
- expect(roots[0].children[0].data).to.deep.equal({ title: 'Child 1.1' });
88
- expect(roots[0].children[1].data).to.deep.equal({ title: 'Child 1.2' });
89
- });
90
-
91
- it('should handle circular references and throw an error #1', () => {
92
- const items = [
93
- { id: 1, parent: 2, name: 'Item 1' },
94
- { id: 2, parent: 1, name: 'Item 2' },
95
- ];
96
-
97
- expect(() => buildTree(items, { validateTree: true }))
98
- .to.throw(Error, 'Tree validation error: Stucture is a cyclic graph.');
99
- });
100
-
101
- it('should handle circular references and throw an error #2', () => {
102
- const items = [
103
- { id: 1, parent: 2, name: 'Item 1' },
104
- { id: 2, parent: 1, name: 'Item 2' },
105
- { id: 3, parent: null, name: 'Root' },
106
- ];
107
-
108
- expect(() => buildTree(items, { validateTree: true }))
109
- .to.throw(Error, 'Tree validation error: Stucture is a cyclic graph.');
110
- });
111
-
112
- it('should handle duplicate identifiers and throw an error', () => {
113
- const items = [
114
- { id: 1, parent: null, name: 'Item 1' },
115
- { id: 1, parent: null, name: 'Item 2' },
116
- ];
117
-
118
- expect(() => buildTree(items))
119
- .to.throw(Error, 'Duplicate identifier detected for "1"');
120
- });
121
-
122
- it('should handle invalid parent keys and throw an error', () => {
123
- const items = [
124
- { id: 1, parent: null, name: 'Item 1' },
125
- { id: 2, parent: 3, name: 'Item 2' },
126
- ];
127
-
128
- expect(() => buildTree(items, { validateParentKeys: [null] }))
129
- .to.throw(Error, 'Invalid parent key "3"');
130
- });
131
-
132
- });