Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kelreel/tree-json-generator

Simple JavaScript Tree Generator library
https://github.com/kelreel/tree-json-generator

fake-data fake-json json-generator mock-data tree tree-generator tree-structure

Last synced: about 1 month ago
JSON representation

Simple JavaScript Tree Generator library

Awesome Lists containing this project

README

        



Tree JSON Generator






version


More than 150 000 nodes per second!
Simple library for generating JSON trees
No external dependency, highly customizable




DEMO

## Installation

You can install it using `yarn` or `npm`

```bash
npm install tree-json-generator
# or
yarn add tree-json-generator
```

![](./docs/split.png)

## Usage

NodeJS:

```javascript
const TreeGen = require("tree-json-generator");
```

WebPack:

```javascript
import * as TreeGen from "tree-json-generator";
```

Use `generate(config)` for generating tree

```javascript
const config = {
node: { // Node fields, required
id: "@id()", // Pipes
parent: "@parent()",
level: "@level()",
name: "@randomName()",
age: "@randomInteger(14,99)",
email: "@randomEmail()",
registered: "@randomBoolean(0.79)",
child: "@child()" // Child field pointer (not required, if children are not needed)
},
rootNodesNumber: 7, // Number of root nodes
childNodesNumber: [2, 5], // Number of children nodes (from 2 to 5)
hasChildRate: 0.4, // Probability of children
maxLevel: 3 // Max nesting
}

let tree = TreeGen.generate(config);
```

![](./docs/split.png)

## Pipes

This allows you to generate fields values for the nodes.

Warning: custom user functions only assign the same value to all nodes

Remember: Pipes are strings

Prebuilded pipes:

`"@id()"`

Random node ID

`"@child()"`

Child field pointer

`"@parent()"`

Field with parent node ID. (For this field, a field with an `"@id"` pipe before required.

`"@level()"`

Node level

`"@randomName()"`

Random name

`"@randomFruit()"`

Random fruit

`"@randomEmail()"`

Random E-Mail

`"@randomInteger(min, max)"`

Random Integer from range

`"@randomBoolean(value = 0.5)"`

Random Boolean (value - coefficient)

![](./docs/split.png)

## Config API

`node: {}`

Required. Contains node fields with pipes.

`rootNodesNumber:`

Not required. Number (5) or array range ([1, 40]). Default is 1.

`childNodesNumber:`

Not required. Number (9) or array range ([1, 15]). Default is 1.

`maxLevel:`

Not required. Max node level. Number (3). Default is 3.

`hasChildRate:`

Not required. The probability that the node has children. From 0 to 1. Default is 1.