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

localstorage-vcs

v2.0.1

Published

Library that will keep your localStorage predictable.

Downloads

402

Readme

localstorage-vcs

Build Status codecov Maintainability

NPM

Introduction

As usage of localStorage evolves over time, particular meaning of keys in the storage may change it's shape or meaning. That may result in inconsistent state of an application. localstorage-vcs let's you track versions of localStorage and enables you to programmatically manage changes of stored data, so localStorage is always up to date.

Localstorage script exposes a single function, that will run each time your client code gets executed in the browser. This function will mutate localStorage based on a configuration that gets passed into the function as it's only argument.

Config

config.version: string

This property notes current version of localStorage that the application expects. Any string will qualify as a valid version, as long as it is unique in reference to previously used versions.

Since this property is used to determinate whether localStorage mutation should be executed, it is advised to set it manually only when localStorage changes it's shape.

Note: version will be kept in localStorage under LOCAL_STORAGE_VERSION key

Bad practice

  • Last commit's hash of each version is discouraged as it will trigger update of localStorage after EVERY release

Good practice

  • Usually a incrementally updated digit is advisable, as it suggests continues nature of versioning.

config.removeAll: boolean

If set true each time version changes, every key on localStorage will be removed via localStorage.clear() method.

Beware that this set to true will have priority over other below methods.

Example:

const config = {
    version: '1',
    removeAll: true
}

config.remove: string[]

Collection of keys on localStorage that should be removed after version changes.

A key refers to localStorage.getItem(key)

Beware that key present in this array will not be migrated even if specified in below option

Example:

const config = {
    version: '1',
    remove: ['someKey1', 'someKey2']
}

config.migrations

  • Array of chronological sequence of Migrations
  • A Migration is represented as an array of length of 2.
  • First element of Migration is a reference to version, that triggers migration functions.
  • Second element of Migration is an object that has localStorage keys, as properties and functions as vales

Migration function

  • Function that accepts current key directly from localStorage of given version
  • Returned value will replace previous value of localStorage item.

Example:

const config = {
    version: '2',
    migrations: [
        ['1', { someKey: oldKey => `${oldKey}_v1` }],
        ['2', { someKey: oldKey => oldKey.replace('_v1', '_v2') }],
    ]
}

API in terms of types

type Key = string;
type Version = string;
type MigrationFunction = (oldKey: Key | null) => Key;

interface IMigrationKeys {
    [key: string]: MigrationFunction
}

type Migration = [Version, IMigrationKeys]

export interface IConfig {
    migrations?: Migration[],
    removeAll?: boolean,
    remove?: Key[],
    version: Version
}