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

data-holder

v2.0.2

Published

A NodeJS package that provides implementations of some data structures

Downloads

11

Readme

Stories in Ready Build Status Build Status DevDependencies npm version

data-holder

A NodeJS package that provides asynchronous implementations for some data structures, with the flexibility needed in this cruel world divided by callbacks and promises.

You can find the complete docs in here.

Implemented structures

  • Linked list
  • Double linked list
  • Ordered List
  • Stack
  • Queue

A little introduction

Let's start simple, say you want a semi synchronous process (recommended mostly for short lived experiments and tests).

let structures = require(data-holder)('Sync');
let List = structures.List;

//Blah, blah, blah...

let l = new List();                                        // []
l.add(13);                                                 // [13]

//Another important thing

l.unshift(42);                                             // [42, 13]

//Here you must put code that is definitively not going to crash.

console.log(l.pop());                                      // [42]

But... what about callbacks?

let structures = require(data-holder)();
let List = structures.List;

//Blah, blah, blah...

//You simply pass the callback as an extra parameter.
let l = new List();                                        // []
l.add(13, () => {                                          // [42, 13]
    l.unshift(42, () => {
      l.pop((err, element) => {                            // [42]
        console.log(element);
      });                                      
    });
});

And do not panic, I won't forget about promises. If you want to use the ones implemented by default on Node, this example is for you.

let structures = require(data-holder)();
let List = structures.List;

//Blah, blah, blah...

//You simply pass the callback as an extra parameter.
let l = new List();                                        // []
let p1 = l.add(13);                                        // [13]
let p2 = p1.then(() => {
  return l.unshift(42);                                    // [42, 13]
});
let p3 = p2.then(() => {
  return l.pop();                                          // [42]
});

p3
  .then((element) => {
    console.log(element);
  })
  .catch(() => {
    console.log("):");
  });

Just as a pro-tip, you can also pass the string default or Default and get the same behaviour (callbacks and Node's promise).

But maybe, you are already using a promise library, then send the function through parameter. Don't worry, I'm pretty bad at explaining, let's see it it action (with Bluebird)

let Promise = require('bluebird');
let structures = require(data-holder)(Promise);
let List = structures.List;

//Blah, blah, blah...

//You simply pass the callback as an extra parameter.
let l = new List();                                        // []
let p1 = l.add(13);                                        // [13]
let p2 = p1.then(() => {
  return l.unshift(42);                                    // [42, 13]
});
let p3 = p2.then(() => {
  return l.pop();                                          // [42]
});

p3
  .then((element) => {
    console.log(element);
  })
  .catch(() => {
    console.log("):");
  });

And that's how you use it with Bluebird. And, yes, I didn't bothered to change the example.

Also, you don't need to import all of the structures (which we have been doing on the previous examples). Let's say you only need a list with callbacks; the second parameter must be a string with the name of the structure. Be careful, this returns the structure directly.

let List = require(data-holder)('Default', 'List');

//Blah, blah, bla

//You simply pass the callback as an extra parameter.
let l = new List();                                        // []
l.add(13, () => {                                          // [42, 13]
    l.unshift(42, () => {
      l.pop((err, element) => {                            // [42]
        console.log(element);
      });                                      
    });
});

Also, you might need more than one structure at the same time, to require them is as simple as passing an array of strings. It gives you the same arrangement as if you imported all of the structures available.

let data-holder = require(data-holder)('Default', 'List', 'OrderedList');
let List = structures.List;
let OrderedList = structures.OrderedList;

//Blah, blah, blah...

//You simply pass the callback as an extra parameter.
let l = new List();                                        // []
let l2 = new OrderedList();                                // []
l.add(13, () => {                                          // [42, 13]
    l.unshift(42, () => {
      l.pop((err, element) => {                            // [42]
        console.log(element);
      });                                      
    });
});