-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a7d441b
Showing
20 changed files
with
5,032 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.rpt2_cache | ||
dist | ||
node_modules | ||
coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.rpt2_cache | ||
tsconfig.json | ||
src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
sudo: false | ||
language: node_js | ||
cache: | ||
directories: | ||
- node_modules | ||
notifications: | ||
email: false | ||
node_js: | ||
- "8" | ||
script: | ||
- npm test | ||
after_success: | ||
- npx codecov | ||
branches: | ||
only: | ||
- master |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
# baobab-tree-logic | ||
|
||
Tree Logic System. | ||
|
||
Many tree management libraries are available for JavaScript. But these libraries are often complicated to customize. Here, I wanted to create a project where the tree logic would have come out of any interface context. | ||
|
||
[](https://www.npmjs.com/package/baobab-tree-logic) | ||
[](https://www.npmjs.com/package/baobab-tree-logic) | ||
[](https://travis-ci.org/oziks/tree-logic) | ||
[](https://codecov.io/gh/oziks/tree-logic) | ||
|
||
## Installation | ||
|
||
```bash | ||
$ yarn add baobab-tree-logic | ||
``` | ||
|
||
## Getting start | ||
|
||
To start, your structure will have to respect a defined type called `TreeNode`. | ||
|
||
```ts | ||
interface TreeNode { | ||
data: any; // Your business data | ||
children?: Array<TreeNode>; | ||
expanded?: boolean; | ||
} | ||
``` | ||
|
||
Here is an example of a tree we use for testing: | ||
|
||
```js | ||
const nodes = [ | ||
{ | ||
data: "level 0" | ||
}, | ||
{ | ||
data: "level 1", | ||
children: [{ data: "child of level 1" }], | ||
expanded: false | ||
}, | ||
{ | ||
data: "level 2", | ||
children: [ | ||
{ | ||
data: "child of level 2", | ||
children: [{ data: "child of child of level 2" }] | ||
} | ||
] | ||
} | ||
]; | ||
``` | ||
|
||
This tree will need to be flattened so that you can use it in your applications. To do this, you can use the flattenNodes function. | ||
|
||
```js | ||
import { flattenNodes } from "baobab-tree-logic"; | ||
|
||
const flattenedNodes = flattenNodes(nodes); | ||
``` | ||
|
||
The `flattenNodes` function will return you an array of elements of type `TreeNodeExtra`. | ||
|
||
```ts | ||
interface TreeNodeExtra { | ||
data: any; | ||
children?: Array<string>; | ||
hasChildren: boolean; | ||
lastChild: boolean; | ||
expanded: boolean; | ||
visible: boolean; | ||
path: string; | ||
parent?: string; | ||
depth: number; | ||
} | ||
``` | ||
|
||
Here is what the function will have returned to you: | ||
|
||
```json | ||
[ | ||
{ | ||
"path": "0", | ||
"data": "level 0", | ||
"children": [], | ||
"expanded": true, | ||
"visible": true, | ||
"hasChildren": false, | ||
"lastChild": false, | ||
"depth": 0 | ||
}, | ||
{ | ||
"path": "1", | ||
"data": "level 1", | ||
"children": ["1.0"], | ||
"expanded": false, | ||
"visible": true, | ||
"hasChildren": true, | ||
"lastChild": false, | ||
"depth": 0 | ||
}, | ||
{ | ||
"path": "1.0", | ||
"data": "child of level 1", | ||
"children": [], | ||
"expanded": true, | ||
"visible": false, | ||
"parent": "1", | ||
"hasChildren": false, | ||
"lastChild": true, | ||
"depth": 1 | ||
}, | ||
{ | ||
"path": "2", | ||
"data": "level 2", | ||
"children": ["2.0"], | ||
"expanded": true, | ||
"visible": true, | ||
"hasChildren": true, | ||
"lastChild": true, | ||
"depth": 0 | ||
}, | ||
{ | ||
"path": "2.0", | ||
"data": "child of level 2", | ||
"children": ["2.0.0"], | ||
"expanded": true, | ||
"visible": true, | ||
"parent": "2", | ||
"hasChildren": true, | ||
"lastChild": true, | ||
"depth": 1 | ||
}, | ||
{ | ||
"path": "2.0.0", | ||
"data": "child of child of level 2", | ||
"children": [], | ||
"expanded": true, | ||
"visible": true, | ||
"parent": "2.0", | ||
"hasChildren": false, | ||
"lastChild": true, | ||
"depth": 2 | ||
} | ||
] | ||
``` | ||
|
||
## API | ||
|
||
The following can be imported from `baobab-tree-logic`. | ||
|
||
### `flattenNodes(nodes: Array<TreeNode>, parent: string = undefined, depth: number = 0, parentExpanded: boolean = true): Array<TreeNodeExtra>` | ||
|
||
Function that allows you to flatten the tree. It adds attributes to each node of the tree that will allow you to perform actions later. | ||
|
||
### `unflattenNodes(nodes: Array<TreeNodeExtra>): Array<TreeNode>` | ||
|
||
Function that allows you to funlatten the tree. It deletes the attributes added by the method `flattenNodes`. | ||
|
||
### `expandNode(node: TreeNodeExtra, nodes: Array<TreeNodeExtra>): Array<TreeNodeExtra>` | ||
|
||
Function that allows to modify the `extended` state of a node of the tree. | ||
|
||
### `changeNodePath(node: TreeNodeExtra, nodes: Array<TreeNodeExtra>, newPath: string): Array<TreeNodeExtra>` | ||
|
||
Function that allows you to change the path of a node in the tree. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{ | ||
"name": "baobab-tree-logic", | ||
"version": "0.1.0", | ||
"description": "Tree Logic", | ||
"keywords": [ | ||
"tree", | ||
"logic" | ||
], | ||
"main": "dist/tree-logic.cjs.js", | ||
"module": "dist/tree-logic.es.js", | ||
"browser": "dist/tree-logic.umd.js", | ||
"types": "dist/tree-logic.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"author": "Morgan Brunot <brunot.morgan@gmail.com> (http://github.com/oziks)", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/oziks/tree-logic.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/oziks/tree-logic/issues" | ||
}, | ||
"homepage": "https://github.com/oziks/tree-logic#readme", | ||
"devDependencies": { | ||
"@types/faker": "^4.1.5", | ||
"@types/jest": "^24.0.13", | ||
"@types/node": "^12.0.1", | ||
"faker": "^4.1.0", | ||
"husky": "^2.3.0", | ||
"jest": "^24.8.0", | ||
"prettier": "1.17.1", | ||
"rimraf": "^2.6.3", | ||
"rollup": "^1.12.0", | ||
"rollup-plugin-typescript2": "^0.21.0", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "3.4.5" | ||
}, | ||
"scripts": { | ||
"lint": "prettier ./src/* --write", | ||
"test": "jest --verbose", | ||
"build": "rollup -c", | ||
"watch": "rollup -cw", | ||
"prepare": "rimraf dist; npm run build" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "npm run lint", | ||
"pre-push": "npm test" | ||
} | ||
}, | ||
"jest": { | ||
"coverageDirectory": "./coverage/", | ||
"collectCoverage": true, | ||
"transform": { | ||
"^.+\\.ts$": "ts-jest" | ||
}, | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"js" | ||
], | ||
"testRegex": "^.+\\.spec\\.ts$" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import typescript from "rollup-plugin-typescript2"; | ||
import pkg from "./package.json"; | ||
|
||
const defaultConfig = { | ||
input: "src/index.ts", | ||
plugins: [ | ||
typescript({ | ||
typescript: require("typescript") | ||
}) | ||
] | ||
}; | ||
|
||
export default [ | ||
// browser-friendly UMD build | ||
{ | ||
...defaultConfig, | ||
output: { | ||
name: pkg.name, | ||
file: pkg.browser, | ||
format: "umd" | ||
} | ||
}, | ||
|
||
// CommonJS (for Node) and ES module (for bundlers) build. | ||
{ | ||
...defaultConfig, | ||
output: [ | ||
{ file: pkg.main, format: "cjs" }, | ||
{ file: pkg.module, format: "es" } | ||
] | ||
} | ||
]; |
Oops, something went wrong.