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

json-storage

v2.1.2

Published

A wrapper for storage engines which use the W3C Storage API

Downloads

365

Readme

JsonStorage

A light, sensible abstraction for DOMStorage (such as localStorage).

Installation

Bower (Browser)

bower install json-storage
# or
wget https://git.coolaj86.com/coolaj86/json-storage.js/raw/branch/master/json-storage.js

Node.JS (Server)

npm install -S localStorage json-storage

Usage

Made for Node.js and Bower (browser-side).

var localStorage = require('localStorage')
  , JsonStorage = require('json-storage').JsonStorage
  , store = JsonStorage.create(localStorage, 'my-widget-namespace', { stringify: true })
  , myValue = {
        foo: "bar"
      , baz: "quux"
    }
  ;

store.set('myKey', myValue);
myValue = store.get('myKey');

NOTE: When using with Node and the localStorage module, you may wish to pass the { stringify: false } option to prevent double stringification.

API

  • JsonStorage.create(DOMStorage, namespace, opts)
    • DOMStorage should be globalStorage, sessionStorage, or localStorage. Defaults to window.localStorage if set to null.
    • namespace is optional string which allows multiple non-conflicting storage containers. For example you could pass two widgets different storage containers and not worry about naming conflicts:
      • Gizmos.create(JsonStorage.create(null, 'my-gizmos'))
      • Gadgets.create(JsonStorage.create(null, 'my-gadgets'))
      • Namespacing can be turned off by explicitly setting false
        • Gadgets.create(JsonStorage.create(null, false))
    • opts
      • stringify set to false in node to avoid double stringifying
  • store.get(key)
  • store.set(key, value)
  • store.remove(key)
  • store.clear()
  • store.keys()
  • store.size()
  • store.toJSON()
  • JSON.stringify(store)

NOTE: You cannot omit optional parameters. Use null if you want accepts the defaults for some things and provide a values for others. For example: JsonStorage.create(null, null, { stringify: false })

JSON / DOMStorage Conversion Gotchas

These notes do not reflect a bugs or defects in this library, they're simply to inform you of a few 'gotchas' inherent in JSON / DOMStorage conversion.

99.999% of the time these gotchas shouldn't effect you in any way. If they do, you're probably doing something wrong in the first place.

undefined vs null

It is not valid to set undefined in JSON. So setting a key to undefined will remove it from the store.

This means that store.set('x') is the same as store.remove('x').

To save undefined, use null instead.

Note that both values that exist as null and values that don't exist at all will return null.

store.set('existing-key', null);
null === store.get('existing-key');
null === store.get('non-existant-key');

null vs "null"

The special case of null as "null", aka "\"null\"":

null, and "null" both parse as null the "object", instead of one being the string (which would be "\"null\"").

Objects containing null, however, parse as expected { "foo": null, "bar": "null" } will parse as foo being null but bar being "null", much unlike the value "null" being parsed on its own.