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

sortof-local-storage

v1.0.1

Published

Same interface as the browser's localStorage, but works in Node. 0 persistence. Useful for testing.

Downloads

1

Readme

sortof-local-storage

Implements the Storage API (sort of) so that unit tests using it can be run in Node.

Why?

Node does not support the Storage API (since it isn't a browser) which is fine for server-based apps. But sometimes you're building a browser-based app (e.g. something with create-react-app), and your tests are running in a Node context. This module gives you something that complies with the Storage API that you can use for testing.

Aren't there other modules that do this already?

Yes, and before doing this I considered:

I wanted something that didn't involve the global namespace and that didn't write to files. One of those projects may better suit your needs, so be sure to check them out.

Installation

npm:

npm install --save sortof-local-storage

Or if you use Yarn:

yarn add sortof-local-storage

Assumptions

That you're familiar with the Storage API and localStorage and that you just need something to use in unit tests. For example, only strings should be stored.

Usage

Creating an instance:

const createStorage = require('sortof-local-storage')

const storage = createStorage()

If you want to initialize it with some values, you can do that too:

const storage = createStorage([ { key: 'foo', value: 'bar' } ])

sortof-local-storage instances support all the Storage API methods except for events.

Let's just just call that out again-- This package does not support Storage API events. PRs are welcome to add that if it would be useful to you.

How is this useful in testing?

Suppose that you had some function in your browser-based app that access window.localStorage. It might look something like this:

function loadThingFromLocalStorage() {
  return localStorage.getItem('thing')
}

If that function executes in a test suite that runs in a Node context, then it will fail when trying to access localStorage, because Node doesn't have that.

We can restructure that and use dependency injection:

function createThingLoader(storage) {
  return function loadThingFromLocalStorage() {
    return storage.getItem('thing')
  }
}

In a file that runs early in your browser app, you can set up your loadThingFromLocalStorage function thusly:

const loadThingFromLocalStorage = createThingLoader(window.localStorage)

The browser-based version will now run as it did before. Then, in your test suite, you can set up something like:

const createStorage = require('sortof-local-storage')

const quoteUnquoteLocalStorage = createStorage()

const loadThingFromLocalStorage = createThingLoader(quoteUnquoteLocalStorage)

Yay!

Issues

Please report 'em.

Contributing

If something needs changing, please open an issue. PRs with tests are most welcome.

I do think the library'a API is "complete" with the exception of not supporting events, so large changes would likely be more appropriate in another module. I'm trying to keep this one focused.

Last thing

This should work on Node 4.8.3 and above. If it doesen't, please file an issue on that. This module does not aim to support lower than that.

License

ISC