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

storage-wrapper

v1.0.4

Published

A small utility for working with localStorage and sessionStorage in the browser

Downloads

24

Readme

StorageWrapper

npm version License: MIT

A small utility for working with localStorage and sessionStorage in the browser, with support for type safety, expiration, and namespacing.

Features

  • Get, set, and remove items with type safety.
  • Expiration support for stored items.
  • Namespacing for avoiding key collisions.

Installation

npm install storage-wrapper

Usage

TypeScript

import { StorageWrapper } from 'storage-wrapper';

const localStorageWrapper = new StorageWrapper('local', 'myApp');
const sessionStorageWrapper = new StorageWrapper('session', 'myApp');

// Setting items
localStorageWrapper.set('user', { name: 'John Doe' }, 30); // Expires in 30 minutes
sessionStorageWrapper.set('sessionID', 'abc123');

// Getting items
console.log(localStorageWrapper.get('user')); // { name: 'John Doe' }
console.log(sessionStorageWrapper.get('sessionID')); // 'abc123'

// Removing items
localStorageWrapper.remove('user');
sessionStorageWrapper.remove('sessionID');

// Clearing all items in the namespace
localStorageWrapper.clear();
sessionStorageWrapper.clear();

Event Listeners

const storageWithEvents = new StorageWrapper('local', 'myApp');

// Adding event listeners
storageWithEvents.on('set', 'print', (key, value) => {
  console.log(`Item set: ${key} = ${value}`);
});

storageWithEvents.on('remove', 'print', (key) => {
  console.log(`Item removed: ${key}`);
});

storageWithEvents.on('clear', 'print', () => {
  console.log('Storage cleared');
});

// Setting an item
storageWithEvents.set('key', 'value'); // Console: Item set: key = value

// Removing an item
storageWithEvents.remove('key'); // Console: Item removed: key

// Clearing storage
storageWithEvents.clear(); // Console: Storage cleared

API

StorageWrapper

constructor(storageType: 'local' | 'session', namespace?: string)

  • storageType: Determines whether to use localStorage or sessionStorage.
  • namespace: Optional namespace to avoid key collisions.

set(key: string, value: any, expirationInMinutes?: number): void

  • key: The key under which the value is stored.
  • value: The value to store.
  • expirationInMinutes: Optional expiration time in minutes.

get<T>(key: string): T | null

  • key: The key of the value to retrieve.
  • Returns the value associated with the key, or null if the key doesn't exist or has expired.

remove(key: string): void

  • key: The key of the value to remove.

clear(): void

  • Clears all items within the namespace or all items if no namespace is specified.

on(event: 'set' | 'remove' | 'clear' | 'expired', event_name: string, callback: (key?: string, value?: any) => void): void

Demo

Check out the demo.

Contributing

This project is open to contributions. Feel free to open an issue or submit a pull request.

License

MIT