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-enhance

v1.0.8

Published

Enhanced version of localStorage

Downloads

261

Readme

localstorage-enhance

English | 中文

  • 'namespace' to avoid same key override
  • 'maxAge' set the data expiration time
  • LRU obsolescence mechanism
  • High performance (manipulate in-memory object, synchronize to localStorage as idleCallback + debounce)

PS: Do not do loop reference object or a large amount of data this violation.

Install

npm install --save localstorage-enhance
import LocalStorage from 'localstorage-enhance';

// data can be of the following types
type Data = string | number | boolean | undefined | null | object;

LocalStorage.setItem({ key: 'demo', data: { test: 234 }});
const item = LocalStorage.getItem('demo'); // { test: 234 }
const unsettedItem = LocalStorage.getItem('unsetted'); // null

LocalStorage.removeItem('demo');
const item2 = LocalStorage.getItem('demo'); // null

Usage

namespace

import LocalStorage from 'localstorage-enhance';

LocalStorage.setItem({ key: 'namespace-demo', value: 'in-default'});
LocalStorage.setItem({ key: 'namespace-demo', value: 'in-namespace', namespace: 'some-namespace' });

const item1 = LocalStorage.getItem('namespace-demo'); // 'in-default'
const item2 = LocalStorage.getItem('namespace-demo', 'some-namespace'); // 'in-namespace'

maxAge

import LocalStorage from 'localstorage-enhance';

LocalStorage.setItem({ key: 'maxAgeDemo', data: 'maxAge1000', maxAge: 1000  });

const item1 = LocalStorage.getItem('maxAgeDemo'); // 'maxAge1000'
setTimeout(() => {
    const item2 = LocalStorage.getItem('maxAgeDemo'); // null
}, 1500);

LRU

If you want to customize the capacity of the LRU elimination mechanism, you can manually generate an instance and specify the storageKey and capacity.

import LocalStorage, { LocalStorageClass } from 'localstorage-enhance';
import  from 'localstorage-enhance';

// default LocalStorage instance
// LocalStorage = new LocalStorageClass({ storageKey: 'localStorage_enhance', capacity: 200, encrypt: false });

const MyLocalStorage = new LocalStorageClass({ storageKey: 'my_localStorage', capacity: 50 });

Encrypted storage data

Manually generate an instance, set encrypt to true and it will encrypt the data in base64

import { { LocalStorageClass } } from 'localstorage-enhance';
const MyLocalStorage = new LocalStorageClass({ storageKey: 'encrypt_localStorage', encrypt: true });

Batch processing of data

    LocalStorage.setMuilty([{ key: 'string', data: 'string' }, { key: 'boolean', data: false }, { key: 'object', data: { test: 324} }]);

    const muiltyRes = LocalStorage.getMuilty(['string', 'boolean', 'object', 'unset']);
    // muiltyRes = { string: 'string', boolean: false, object: { test: 324 }, unset: null }

    // Chain call
    LocalStorage
        .setItem({ key: '1', data: 1 })
        .setItem({ key: '2', data: 2 });

All methods

interface LocalStorage {
    setItem: ({ key, data, maxAge, namespace }: {
        key: string;
        data: Data;
        maxAge?: number | undefined;
        namespace?: string | undefined;
    }) => this;
    setMuilty: (dataObjs: Array<{
        key: string;
        data: Data;
        maxAge?: number;
        namespace?: string;
    }>) => this;
    getItem: (key: string, namespace?: string) => Data;
    getMuilty: <T extends string>(keys: T[], namespace?: string) => { [K in T]: Data; };
    removeItem: (key: string, namespace?: string) => Data;
    removeMuilty: <T extends string>(keys: T[], namespace?: string) => { [K in T]: Data; };
    clearNamespace: (namespace: string) => this;
    clearAll: () => this;
}

Run test

Run the demos:

npm install
npm run test

Run build

npm install
npm run build

License

MIT