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

@diaskappassov/hungry-local-storage

v1.0.1

Published

💾⌛ Tool for store temporary data in local storage (with autodelete expired data).

Downloads

18

Readme

hungry-local-storage

💾⌛ Tool for store temporary data in local storage (with autodelete expired data).

About

This is a simple tool for storing data in localStorage with expiration time. It's useful for storing temporary data, such as user preferences, but also for storing data that is not needed after a certain period of time.

Why hungry-local-storage?

The reason why I called this package hungry-local-storage is that he is endlessly hungry thats allowed to eat only after the item's store time expires. And it checks at specified intervals (every N seconds you specify) whether the object's storage period has expired so that it can be eaten. Such an obedient, hungry tool.

Usage

Installing

$ npm i "@diaskappassov/hungry-local-storage"

Usage

Setting values to localStorage with expiration

import { hls } from "@diaskappassov/hungry-local-storage";

// No expire time
hls.set("cherry", "string");

const curTimestamp = new Date().getTime() / 1000;
// Expires after 60 seconds
hls.set("banana", 123, curTimestamp + 60);

// Expires after 3600 seconds
hls.set("apple", [1, 2, 3], curTimestamp + 3600);

// Expires after 10 minutes
hls.set("avocado", {}, { minutes: 10 });

// Expires after 3 days
hls.set("tea", { type: "green", count: 2 }, { days: 3 });

// Expires after 7 seconds + 6 minutes + 5 hours + 4 days+ 3 weeks + 2 months + 1 year
hls.set("cake", "CAAAKE!", {
  seconds: 7,
  minutes: 6,
  hours: 5,
  days: 4,
  weeks: 3,
  months: 2,
  years: 1,
});

Getting value from localStorage

Imagine that you run hls.set("apple", [1, 2, 3], { hours: 1 });, and you want to get this value from localStorage.

import { hls } from "@diaskappassov/hungry-local-storage";

// Returns parsed value if not expired, otherwise returns null with removing expired item from localStorage
const apple = hls.get("apple");

console.log(apple); // [1, 2, 3]

Removing expired items from localStorage

import { hls } from "@diaskappassov/hungry-local-storage";

// Removes all expired items and returns count of removed items by function flush
const countRemoved = hls.flush();
console.log("flush returned:", countRemoved);

Don't worry, flush not removes localStorage items that's not have expiration field on top level of structure.

Setting Auto Flush

By default auto flush is disabled. To enable it, execute setAutoFlush method with setting values in seconds.

import { hls } from "@diaskappassov/hungry-local-storage";

// Set auto flush that runs flush every 10 minutes
hls.setAutoFlush(60 * 10);

If you run setAutoFlush method twice with different values, then the last value will be used, and it will finish all old autoFlush timers.

If you set auto flush value to null then auto flush will be disabled.

import { hls } from "@diaskappassov/hungry-local-storage";

// Disable auto flush
hls.setAutoFlush(null);

Removing item from localStorage

import { hls } from "@diaskappassov/hungry-local-storage";

// Removes item with key "apple".
hls.remove("apple");

Under code hls.remove just runs localStorage.removeItem method

Structure of created item by hls

After running code hls.set("apple", [1, 2, 3], { hours: 5 });, in local storage it will be stored as follows:

{
  creation: CURRENT_TIMESTAMP,
  data: "[1,2,3]",
  expiration: CURRENT_TIMESTAMP + HOURS_5,
}

Property expiration is optional, it means, if expiration is not exists, it will be stored in local storage while you will not delete it.

License

MIT

Authors