Coder Social home page Coder Social logo

tree-structure-ts's Introduction

@itpropro/tree-structure-ts

npm (scoped) npm bundle size (scoped) ci Code Coverage

Introduction

This module helps interacting with Tree structures in TypeScript. It is optimized to work with big trees without causing overflows. Therefore it doesn't use recursion and the implementations for preOrder and postOrder traversals use Promise.all for concurrency to traverse multiple nodes at ones. It is fully typed and has over 95% test coverage.

๐Ÿš€ Zero dependency
๐Ÿท๏ธ Fully typed
โœจ Optimized for big trees
๐Ÿšง No recursion -> no memory overflows
๐Ÿค Small bundle size

Quick Start

Installation

To install the module, run the following command:

# pnpm
pnpm install @itpropro/tree-structure-ts

# npm
npm install @itpropro/tree-structure-ts

# yarn
yarn add @itpropro/tree-structure-ts

Import

// ESM / TypeScript
import { Tree } from '@itpropro/tree-structure-ts'
import type { TreeNode } from '@itpropro/tree-structure-ts'

Usage

To create a new Tree instance, use the Tree constructor:

const tree = new Tree('root')
const root = tree.root

To add a child node to a TreeNode, use the addChild method:

const child1 = root.addChild('child1')
const child2 = root.addChild('child2')

To get all nodes in the tree below a TreeNode, use the all method:

const nodes = root.all()

To traverse a tree, use the traverse method:

root.traverse((node) => {
  // This function is called for each node in the tree
})

You can specify the traversal order by passing one of the following values to the traverse method:

  • breadthFirst (the default): visits nodes in breadth-first order
  • depthFirst: visits nodes in depth-first order
  • preOrder: visits the current node, then traverses the left subtree, then traverses the right subtree
  • postOrder: traverses the left subtree, then traverses the right subtree, then visits the current node

for all avalaible methods and fields, please read the detailed documentation of the Tree and TreeNode class: Class docs.

Contribution

See Contributing Guide.

License

Made with โค๏ธ

Published under MIT License.

tree-structure-ts's People

Contributors

itpropro avatar renovate[bot] avatar

Stargazers

 avatar

Watchers

 avatar

tree-structure-ts's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Warning

These dependencies are deprecated:

Datasource Name Replacement PR?
npm @vitest/coverage-c8 Unavailable

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • chore(deps): update dependency eslint to v8.57.0
  • chore(deps): update dependency typescript to v5.5.4
  • chore(deps): update dependency unbuild to v1.2.1
  • chore(deps): update mathieudutour/github-tag-action action to v6.2
  • chore(deps): update dependency eslint to v9
  • chore(deps): update dependency typedoc-plugin-markdown to v4
  • chore(deps): update dependency vitest to v2
  • chore(deps): update pnpm to v9
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • pnpm/action-setup v2
  • actions/setup-node v3
  • mathieudutour/github-tag-action v6.1
  • stefanzweifel/git-auto-commit-action v4
  • mathieudutour/github-tag-action v6.1
.github/workflows/release.yml
  • actions/checkout v3
  • pnpm/action-setup v2
  • actions/setup-node v3
.github/workflows/test-build.yml
  • actions/checkout v3
  • pnpm/action-setup v2
  • actions/setup-node v3
npm
package.json
  • @antfu/eslint-config-ts ^0.33.1
  • @typescript-eslint/eslint-plugin ^5.45.1
  • @typescript-eslint/parser ^5.45.1
  • @vitest/coverage-c8 ^0.25.8
  • changelogen ^0.4.0
  • eslint ^8.29.0
  • typedoc ^0.23.21
  • typedoc-plugin-markdown ^3.14.0
  • typescript ^5.0.0
  • unbuild ^1.0.2
  • vitest ^0.25.5
  • pnpm 7.33.6

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.