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

node-data-structures

v1.0.1

Published

Simple implementation of Java like data structures in Javascript.

Downloads

6

Readme

node-data-structures

Build Status

Simple Java-esque implementation of some common data structures in Node.

Based on [adrianko's] (https://github.com/adrianko) implementation of [PHP Data Structures] (https://github.com/adrianko/php-data-structures).

Installing and using the module

  • Install using npm

npm install node-data-structures

  • Require the module
    var ds = require('node-data-structures');

    // Use the data structures you want
    var Set = ds.Set
      , SortedSet = ds.SortedSet
      , Stack = ds.Stack
      , Queue = ds.Queue
      , Iterator = ds.Iterator;

Data Structures

  • Sets
  • Queues
  • Sorted Sets
  • Stack

Helpers

  • Iterator

Implemented features

  • Sets
  • Sorted Sets
  • Queues
  • Stacks
  • Collections
  • Iterator

Documentation

Collection API

All structures inherit from the Collection class. Available methods:

  • getAll(): Returns all elements of the collection.
  • toJson(): Returns the collection in JSON format.
  • size(): Returns the size of the collection.
  • add(item): Add an element to the collection. Use this method to add a single element. Returns true if operation was successful; else returns false. Can also be used to add a single Collection to it.
  • addAll(): Add all the elements in arguments to the collection. Returns a boolean similar to add(item).
  • clear(): Clears the collection.
  • contains(item): Returns true if item exists in the collection; else returns false.
  • containsAll(): Similar to contains(item); but checks for all elements in arguments.
  • isEmpty(): Returns true if the collection is empty, else returns false.
  • remove(item): Removes item from the collection. Returns boolean.
  • removeAll(): Removes all elements in arguments. Returns a boolean value.
  • equals(Collection): Compares one Collection to another. Returns boolean if each element is the same in each collection. Nested collections are further compared.

Iterator API

Provides some helper methods to iterate over collections. Available methods:

  • next(): Returns the next element in the collection. Throws Error('No such element') if there are no more elements to be returned.
  • hasNext(): Returns a boolean ; true if the next element exists, else returns false. It is advisable to use hasNext() before calling next().
  • remove(): Removes the last element that was iterated in the collection. Throws Error('Next() was not called') when remove() is called without next() being called.

Set API

No duplicate elements can be added to a Set. Methods are inherited from Collection. add(item) and addAll() differ only on duplicate elements being added.

Sorted Set API

SortedSet inherits from Set and contains additional methods. Elements are sorted when added to a SortedSet. Available methods:

  • first() : Returns the first element of the collection.
  • last(): Returns the last element of the collection.
  • headSet(end): Returns a SortedSet containing elements from the parent set strictly less than end.
  • tailSet(start): Returns a SortedSet containing elements inclusive of start.
  • subSet(start, end): Returns a SortedSet containing elements inclusive of start but not inclusive of end inheriting from the parent collection.

Queue API

Queue inherits from Collection. All methods in Collection are available to Queue. Other methods available are:

  • element(): Returns the first element in the Queue. Throws an Error('No such element') if the Queue is empty.
  • remove(): Retrieves and removes the element at the head of the Queue. Throws an Error('Queue is empty') if Queue is empty.
  • poll(): Removes and retrieves the element at the head of the Queue. Differs from remove() only in that it returns null if the Queue is empty.
  • peek(): Retrieves the element at the head of the Queue. Differs from element() only in that it returns null if Queue is empty.

Stack API

Available Stack methods:

  • push(item): Pushes an item onto the top of the Stack.
  • pop(): Pop the top element off the Stack and return it. Trying to pop() on an empty Stack will throw Error('Stack is empty.').
  • empty(): This is synonymous to the isEmpty() function in the base class. Returns a boolean value.
  • search(item): Search for an item in the Stack. Will return an offset from the top of the stack. If item does not exist in the Stack, a Error('No such element') is thrown.

Examples

  • Creating a simple Set with elements.
    var set = new Set("a", "b", "c");
    set.add("d"); // Adds "d" to the set
    set.addAll("x", "y"); // Adds "x" and "y" to the set
    set.size(); // Will return 6
  • Creating an empty Set.
    var set = new Set();
    set.addAll("a", "b", "c"); // Adds all elements to the set
  • Create a Set by adding another Collection to it.
    var setOne = new Set("a", "b");
    var setTwo = new Set("x", "y", setOne);
    var setThree = new Set(["r", "s"]);
    setTwo.add(setThree);
  • Create a new Iterator
    var set = new Set();
    var setOne = new Set("a", "b", ["x", "y"]);
    set.add(setOne);

    var iterator = new Iterator(set);
    while(iterator.hasNext()) {
        console.log(iterator.next());
    }
  • Create a new SortedSet
    var sortedSet = new SortedSet("z", "x", "c", "v");
    sortedSet.getAll(); // Will return ["c", "v", "x", "z"]
    var set = new Set(["r", "s", "t"], "k");
    sortedSet.add(set);

    sortedSet.first(); // Will return "c"
    sortedSet.tailSet("v"); // Will return ["v", "x", "z"]
  • Create a new Queue
    var q = new Queue("z", "x", "c");
    q.peek(); // Will return "z"
    q.poll(); // Will remove and return "z"
    q.getAll(); // Contains ["x", "c"]
  • Create a new Stack
    var stack = new Stack();
    stack.push("z");
    stack.push(["r", "s"]);
    console.log(stack.getAll()); // Will return [["r", "s"], "z"]

    stack.pop(); // Will remove ["r", "s"] off the Stack and return it.
    stack.search("z"); // Will return 1

Running Tests

    npm test

Test Frameworks:

  • Mocha - Test suite runner.
  • Chai - Assert, Expect and Should (BDD).

Updates / Todo

  • Possibly make data structures synchronised.
  • Make SortedSets backed by parent sets.