Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
oziks committed Jul 4, 2019
0 parents commit a7d441b
Show file tree
Hide file tree
Showing 20 changed files with 5,032 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.rpt2_cache
dist
node_modules
coverage
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.rpt2_cache
tsconfig.json
src
16 changes: 16 additions & 0 deletions .travis.yml
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
166 changes: 166 additions & 0 deletions README.md
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.

[![NPM Version](https://img.shields.io/npm/v/baobab-tree-logic.svg?style=flat)](https://www.npmjs.com/package/baobab-tree-logic)
[![NPM Downloads](https://img.shields.io/npm/dm/baobab-tree-logic.svg?style=flat)](https://www.npmjs.com/package/baobab-tree-logic)
[![Build Status](https://travis-ci.org/oziks/tree-logic.svg?branch=master)](https://travis-ci.org/oziks/tree-logic)
[![codecov](https://codecov.io/gh/oziks/tree-logic/branch/master/graph/badge.svg)](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.
65 changes: 65 additions & 0 deletions package.json
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$"
}
}
32 changes: 32 additions & 0 deletions rollup.config.js
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" }
]
}
];
Loading

0 comments on commit a7d441b

Please sign in to comment.