JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q26877F
  • License MIT

A versatile module for managing hierarchical tree structures in JavaScript. Supports adding branches and data, retrieving, and converting between tree and linear structures.

Package Exports

  • structree
  • structree/structree.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (structree) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

structree

structree is a module for managing hierarchical tree structures in JavaScript.

Installation

Install the module via npm:

npm install structree

Usage

import Tree from 'structree';

// Alternatively, for client-side usage
import Tree from './path/to/node_modules/structree/structree.js';

// Create a new Tree instance
const myTree = new Tree();

// Set initial branches
myTree.setBranch([1]);
myTree.setBranch([2]);

// Add data
myTree.setData([1, 1], 'Data at 1/1');
myTree.setData([2, 1], 'Data at 2/1');

// Retrieve data
const data = myTree.get([1, 1]); // 'Data at 1/1'

// Convert to linear structure
const linearStructure = Tree.toLinearStructure(myTree.getTree());

// Convert back to tree structure
const treeStructure = Tree.toTreeStructure(linearStructure);

API

class Tree

Constructor

new Tree(tree = [], maxId = 0)
  • tree (array) — Initial tree structure.
  • maxId (number) — Maximum identifier.

Methods

  • getTree()
    • Returns the current tree.
  • getLinear()
    • Returns the current tree as a linear structure.
  • setBranch(path, content = [])
    • Adds a new branch at the specified path.
    • path (array) — Path to the new branch.
    • content (array) — Content of the branch.
  • setData(path, data)
    • Adds new data at the specified path.
    • path (array) — Path to the data.
    • data (any) — Data.
  • get(path)
    • Retrieves data or branch at the specified path.
    • path (array) — Path to the element.
  • getUnparsed(path)
    • Gets all data or branch properties at the specified path.
    • path (array) — Beginning of the path.
  • remove(path)
    • Removes the element at the specified path.
    • path (array) — Path to the element to be removed.
  • contains(path)
    • Checks if the path is contained in the tree.
    • path (array) — Path to check.

Static Methods

  • static toLinearStructure(tree)
    • Converts the tree to a linear structure.
    • tree (array) — Tree to be converted.
  • static toTreeStructure(array)
    • Converts the linear structure back to a tree.
    • array (array) — Linear structure to be converted.
  • static getContent(el, tree)
    • Returns the content of the element.
    • el (object) — Element.
    • tree (array) — Tree.
  • static getPath(el)
    • Returns the path of the element.
    • el (object) — Element.
  • static getType(el)
    • Returns the type of the element.
    • el (object) — Element.

Examples

// Create a new tree
const myTree = new Tree();

// Add branches and data
myTree.setBranch([1]);
myTree.setBranch([1, 2]);
myTree.setData([1, 2, 3], 'Data at 1/2/3');

// Retrieve data
const data = myTree.get([1, 2, 3]); // 'Data at 1/2/3'

// Remove an element
myTree.remove([1, 2, 3]);

// Check if path exists
const containsPath = myTree.contains([1, 2]); // true

// Convert tree to linear structure
const linearStructure = Tree.toLinearStructure(myTree.getTree());

// Convert back to tree structure
const treeStructure = Tree.toTreeStructure(linearStructure);

License

This project is licensed under the MIT License.