More than 150 000 nodes per second!
Simple library for generating JSON trees
No external dependency, highly customizable
You can install it using yarn
or npm
npm install tree-json-generator
# or
yarn add tree-json-generator
NodeJS:
const TreeGen = require("tree-json-generator");
WebPack:
import * as TreeGen from "tree-json-generator";
Use generate(config)
for generating tree
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);
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)
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.