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

lski-storage

v1.0.0

Published

Simple javascript storage wrapper with optional expiry time.

Downloads

14

Readme

Lski-StorageJS

Simple wrapper for storing data in the browser with optional expiry time. Built into es module, cjs and umd formats, so supports everything from tree-shaking to running "as is" in the browser.

Reasoning

Basically you create a store, or stores, to contain data, those stores can either sit on top of localStorage, sessionStorage or cookies (depending on what type of store you want), so why not just use those directly?

  • Stores accept different data types, whereas localStorage/sessionStorage/cookies only accept strings.
  • All types of store accept an expiry time when setting a value, simulating the expiry time on cookies. This is useful for holding time sensitive data (like auth tokens).
  • Each type of store provides a shared interface between them, so easy to switch.
  • Stores can be typed (if using Typescript) so you know the data type of the value returned.
  • It encapsulates getting/setting values so that keys dont have to be remembered and reducing magic strings.
  • Stores can be passed easily, if needed, they are just an object.

Install

You can install from github directly using npm:

npm i lski-storage

Or you can link to the file with a script tag in the browser:

<script crossorigin src="https://unpkg.com/lski-storage/dist/lski-storage.min.js" />

Usage

Create a store, with an optional namespace, below we create a new store, which sits on local storage. NB We could have choosen a store based on cookies or session storage as well. See Options below

import { local } from 'lski-storage';

// NB: Does not need to be a string it can be any type you want
const store = local<string>('aNamespace');

Then set a value, with an optional expiry time:

// Sets a value with a expiry time in 1 second
store.set("my value", Date.now() + 1000);

Then we can access the value, if this is called before the expired time then aValue would equal "my value" otherwise is would be null

// NB If using typescript myValue would be automatically a `string` type in this instance
// As the type was declared was set when creating the store
const myValue = store.get();

To clear (delete) a value early:

store.clear();

// Or check the store has a value
store.has();

Sometimes you might want to know when a value will expire (if it still exists), use the data function which also returns the expiresAt value.

const data = store.data();

// E.g.
// data.value -> ""
// data.expiresAt -> 1544891393670

Options

There are 3 options available local, session and cookie. They all create stores that have the same interface (as shown above), they differ in their underlying usage, which can be used to your advantage:

Local sits on localStorage so if an expiry time is not supplied the value will remain available via the store until manually cleared.

Session however will be available until either the expiry time or the user session ends, whichever is earliest.

Cookie works like session in that if an expiry isnt given then it will expire naturally at the end of the session, but the advantage/disadvantage is it will get sent to the server on each request as well.

Browser support

See https://caniuse.com/#feat=namevalue-storage for full listing, as thats the underlying tech used. But it includes the following:

  • IE 8
  • FF 3.5
  • Chrome 4
  • Safari 4
  • Plus others

Dependencies

No package dependancies

Todo

Implement testing using a jest coupled with something like puppeteer, as its a wrapper over browser intrinsic objects that are used underneath.