npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

structz

v1.0.1

Published

ES2015 Class versions of a Node and Tree data structure

Downloads

20

Readme

structz

ES2015 Class versions of a Node and Tree

Build Status codecov

Install

npm install structz or yarn add structz

ES Module

import { Node, Tree } from 'structz';

CJS

const Structz = require('structz'),
  Node = Structz.Node,
  Tree = Structz.Tree;

Getting Started

After importing the library, you can create as many nodes as you'd like.

let rootSoilNode = new Node('soil');
let fertilizerChildNode = new Node('fertilizer');
let waterChildNode = new Node('h2o');

The new Node() constructor accepts an argument of any Javascript type.

You can append child nodes to a root node like so:

rootSoilNode.append(fertilizerChildNode);
rootSoilNode.append(waterChildNode);

You can detect if a Node is a root or not as well.

rootSoilNode.isRoot(); // -> true
waterChildNode.isRoot(); // -> false

Accessing child nodes is simple.

rootSoilNode.children // returns [fertilizerChildNode, waterChildNode]

You can perform immutable operations on the Node data structure using the Tree class. You instantiate it with a root Node.

let rootNode = new Node(1);
rootNode.append(new Node(2));
let newRootNode = Tree.map(rootNode, (node) => {
  return node + 10;
}); // this returns a `new Tree()` with the newly created `Node` as the `.root` value.
newRootNode.root // returns Node of modified type
// in this case, our new structure looks like:
[11] (root)
|
[12] (child)

Documentation

See docs/index.html for up to date documentation.

Node

Class representing a Node data structure type

Kind: global class

node.children ⇒ Array

Returns an array of child nodes.

Kind: instance property of Node

node.value ⇒ *

Returns the value of the this.attributes.

Kind: instance property of Node

node.value

Sets the value of this.attributes.

Kind: instance property of Node

| Param | | --- | | attribute |

node.isRoot() ⇒ boolean

Detects if this Node is a parent node or not.

Kind: instance method of Node

node.hasChildren() ⇒ boolean

Detects if this node has children or not.

Kind: instance method of Node

node.append(child) ⇒ Node

Appends a child Node to this Node.

Kind: instance method of Node

| Param | | --- | | child |

node.toString() ⇒ string

Returns a String that represents this Node.

Kind: instance method of Node

Classes

Tree

Class representing a Tree data structure type that contains a Root node

Kind: global class

new Tree(root)

Instantiates a Node as a Binary Tree

| Param | | --- | | root |

tree.root ⇒ *

Returns the root of the Node.

Kind: instance property of Tree

Tree.map(node, func, tree) ⇒ *

Performs a function on a Node with the provided function and returns the output. Optionally accepts a Tree to perform this otherwise a new Tree is instantiated and returned (breaks immutability).

Kind: static method of Tree

| Param | Default | | --- | --- | | node | | | func | | | tree | |

Current Test Coverage

> [email protected] test ~/github/structz
> nyc mocha

  A Node data structure
    ✓ Can instantiate a new Node
    ✓ Returns [] children if empty
    ✓ Returns [] children if not empty
    ✓ successfully can append a child
    ✓ returns false if there are no children
    ✓ returns true if there are children
    ✓ can return its own value
    ✓ can set its own value
    ✓ can output itself as a String

  A Tree data structure type that contains a Root node data structure
    ✓ can instantiate itself
    ✓ returns the root Node
    ✓ maps over a Node


  12 passing (12ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |    88.46 |    71.43 |    91.67 |       92 |                   |
 Node.js  |       80 |        0 |     87.5 |    85.71 |             23,24 |
 Tree.js  |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|

Changelog

  • 1.0.0 - Preparing initial release, Updating to support CJS + ES Modules
  • 0.2.4 - Updated JSDoc Generation
  • 0.2.3 - Updated README with starter guide
  • 0.2.2 - Docs updated
  • 0.2.1 - Corrected bug with exports
  • 0.2.0 - Changed package structure for modularity
  • 0.1.1 - Added Test Coverage
  • 0.1.0 - Initial release.