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

webstorage

v0.0.7

Published

A simple, pluggable, W3C Web Storage compliant API for key-value data

Downloads

258

Readme

webstorage

A simple, pluggable, W3C Web Storage compliant API for key-value data.

In webstorage, all persistence layer is pluggable. You can integrate or reuse webstorage interface across any backends such as in-memory database, local file system, mongodb, redis, etc.

By default, webstorage does not persist data. It means that it will use in-memory object as its backend.

var webstorage = require('webstorage');
var storage = webstorage();

storage.setItem('foo', 'bar');
storage.getItem('foo') // => 'bar'

If you want to persist data locally, you can use webstorage-local plugin.

var webstorage = require('webstorage');
var local = require('webstorage-local');
var localStorage = webstorage(local('/tmp'));

localStorage.setItem('foo', 'bar');
localStorage.getItem('foo') // => 'bar'

Installation

$ npm install webstorage

Plugins

Currently the following plugins are available:

API

Check out the W3C Web Storage for detailed specification.

Storage

Exposed by require('webstorage').

Storage()

Creates a new Storage. Works with and without new:

var storage = require('webstorage')();
// or
var Storage = require('webstorage');
var storage = new Storage();

Each Storage object provides access to a list of key/value pairs. Keys are strings. Any string (including the empty string => TODO) is a valid key. Values are similarly strings.

Storage(persistence:Persistence)

Creates a new Storage and attaches it to the given persistence. Default persistence is webstorage-memory.

#length

Returns the number of key/value pairs currently present in the list associated with the object.

#key(n)

Returns the name of the nth key in the list. The order of keys must be consistent within an object so long as the number of keys doesn't change. If n is greater than or equal to the number of key/value pairs in the object, then this method must return null.

var storage = require('webstorage')();
storage.setItem('foo', 'bar');
storage.key(0); // => 'bar'
storage.key(1); // => null

#getItem(key)

Returns the current value associated with the given key. If the given key does not exist in the list associated with the object then this method must return null.

#setItem(key, value)

First, checks if a key/value pair with the given key already exists in the list associated with the object. If it does not, then a new key/value pair is added to the list, with the given key and with its value set to value. If the key does exist in the list, then it has its value updated to value.

#removeItem(key)

Causes the key/value pair with the given key to be removed from the list associated with the object, if it exists. If no item with that key exists,the method does nothing.

#clear()

Causes the list associated with the object to be emptied of all key/value pairs, if there are any. If there are none, then the method does nothing.

Implementing persistent storage plugin

Your persistent storage class for a plugin module should implement the following methods.

#keys()

Returns all keys stored.

#getItem(key)

Returns the value associated with the given key.

#setItem(key, value)

Adds or updates the key/value pair.

#removeItem(key)

Removes the item with the given key.

Here is an example of default in-memory persistent storage, see full example at lib/persistent/memory.

function MemoryStorage() {
  if (!(this instanceof MemoryStorage)) {
    return new MemoryStorage();
  }
}

MemoryStorage.prototype.keys = function() {
  return Object.keys(this);
};

MemoryStorage.prototype.getItem = function(key) {
  return String(this[key]);
};

MemoryStorage.prototype.setItem = function(key, value) {
  this[key] = String(value);
};

MemoryStorage.prototype.removeItem = function(key) {
  delete this[key];
};

module.exports = MemoryStorage;

License

MIT