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

local-storage-proxy

v4.0.4

Published

A proxy for accessing localstorage as an object

Downloads

214

Readme

local-storage-proxy

Work with window.localStorage as if it were an object

npm install local-storage-proxy

Usage

import localStorageProxy from 'https://unpkg.com/local-storage-proxy@^2?module'

window.localStorage.clear()

const state = localStorageProxy('namespace', {
  defaults: {
    some: [], // Doesn't override saved state
    defaults: null
  },
  lspReset: false
})

console.log(state.some) // []
state.some.push('foo')
console.log(state.some) // [ 'foo' ]

state.addEventListener('update', ev => {
  console.log('state was updated, or localStorage was updated on another tab')
})

console.log(window.localStorage.getItem('namespace')) // {"some":["foo"],"defaults":null}

API

lsp = localStorageProxy(namespace, [opts])

Create a local storage proxy attatched to a root namespace. All gets and sets will JSON.stringify to this key. Returns nested proxies that update the key when you set the value. Performance may be limited, but its great for small peices of data with limited writes.

lsp is also an instance of an EventTarget and will emit an event under the update namespace. This is triggered whenever you set a value on the local storage proxy, or when the storage event fires (e.g. when localStorage updates in another tab). The storage event is only listened for in the browser since there is no concept of a separate page or tab in node.js.

opts include:

{
  defaults: {}, // Default keys to set.  Overridden by any existing local storage state
  lspReset: false,
  storageEventListener: true
}

Additions to the opts.defaults object can be safely added without overwriting existing data, and can also be assumed to be available even after the user has instantiated default and new data to the same namepace.

Default values are stored in localStorage. New default values can be added at any time, however if you change a default, you may need to updaate lspReset.

An lspReset key, set to false by default, can be used to force clear out local storage on all clients reloading if the value is different than the time they last loaded state. It can be any JSON serializable object. Strings work well. This will need to be changed whenever change an existing default.

storageEventListener is a boolean that determines if the storage event is listened for, (only in the browser). Since there is no fined grained way to listen for a single namespace on this event, you might want to turn this off if localStorage is updated frequently by other routines which will cause local storage proxy to emit many update events.

License

MIT