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

@amatiasq/client-storage

v3.0.3

Published

This library manages stores like `localStorage`, `sessionStorage` or [`localforage`](https://github.com/localForage/localForage)

Downloads

23

Readme

Client Storage

This library manages stores like localStorage, sessionStorage or localforage

Installation

Install with npm i --save @amatiasq/client-storage.

Usage

import { ClientStorage } from 'https://unpkg.com/@amatiasq/client-storage';

const lang = new ClientStorage<string>('my-app-language', {
  // default 'localStorage', also accepts export value of `localforage`;
  store: 'sessionStorage'
  // default 0
  version: 1,
  // required if <type> is not nullable
  default: 'en',
});

main();

async function main() {
  // will trigger a load and return default
  console.log(lang.cache); // 'en'

  // get the value from cache if cached or load it otherwise
  console.log(await lang.get()); // 'fr'

  // fetch the value from the store
  console.log(await lang.load()); // 'fr'

  lang.set('it');

  console.log(lang.cache) // 'it'
  console.log(await lang.get()) // 'it'

  const stored = lang.clear();
  console.log(stored); // 'it';
  console.log(lang.get()) // 'en'
}

JSON serialisation

When setting store = 'localStorage' (default) or store = 'sessionStorage' serialization / deserialization is managed automatically by JSON.stringify / JSON.parse.

Versioning

This class will store a version number next to the stored value. If the stored version number does not match the current one, it will remove the stored value. This prevents an updated application to receive legacy formats and removes the need to ask the clients to wipe site data.

import { ClientStorage } from 'https://unpkg.com/@amatiasq/client-storage';

const first = new ClientStorage<string>('test', { version: 0 });
first.set('this is a test');
console.log(first.get()); // 'this is a test'

const second = new ClientStorage<string>('test', { version: 1 });
console.log(second.get()); // null

console.log(first.get()); // null

Transform

You can add a transformer to convert the stored value into something else and back:

import { ClientStorage, IClientStorage } from 'https://unpkg.com/@amatiasq/client-storage';

const friendId = new ClientStorage<string>('friend-ids', { default: 12 });

class Friend {
  constructor(id: string) {}
  toString() {
    return `Friend(${this.id})`
  }
}

const friend: IClientStorage = friendId.transform<Friend>({
  apply: id => new Friend(id),
  revert: friend => friendfriend.id;
  // required if `apply` returns a promise (not in this case)
  // default: [],
});

friendId.set(25);

console.log(friend.cache.toString()) // Friend(25)
console.log((await friend.get()).toString()) // Friend(25)
console.log(friend.cache.toString()) // Friend(25)

friend.set(new Friend(42));

console.log(friendId.cache); // 42
console.log((await friendId.get()).toString()); // 42
console.log(friendId.cache); // 42

const friendToString = friend.transform<string>({
  friend => friend.toString(),
  text => new Friend(parseInt(text.substr(7, -1))),
});

console.log(friendToString.cache) // Friend(42)

friendId.reset();

console.log(friend.cache.toString()) // Friend(12)
console.log((await friend.get()).toString()) // Friend(12)
console.log(friendToString.cache) // Friend(12)