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

browser-storage-class

v1.2.0

Published

A Browser Storage Class for NodeJS or Browser

Downloads

101

Readme

Browser Storage Class

NPM js-semistandard-style

npm version Gitlab Pipeline Gitlab Coverage NPM download/month NPM download total

A Browser Storage Class for NodeJS or Browser

There are many library to handle local or session storage for browser out there. But many of them is too bloated for browser. Actually we just need a simple wrapper class, so your website still lightweight and running fast.

Minimum Requirement

  • Tested with NodeJS v16.
  • All browsers with support ES5.

Install using NPM

$ npm install browser-storage-class

Usage in NodeJS

// load using require
const BrowserStorageClass = require('browser-storage-class');

// or load using import
import BrowserStorageClass from 'browser-storage-class';

Or simply use in Browser with CDN

<!-- Always get the latest version -->
<!-- Not recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/browser-storage-class/dist/browser-storage-class.min.js"></script>

<!-- Get minor updates and patch fixes within a major version -->
<script src="https://cdn.jsdelivr.net/npm/browser-storage-class@1/dist/browser-storage-class.min.js"></script>

<!-- Get patch fixes within a minor version -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/browser-storage-class.min.js"></script>

<!-- Get a specific version -->
<!-- Recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/browser-storage-class.min.js"></script>

Usage

Local or Session Storage

const BrowserStorageClass = require('browser-storage-class'); // in browser doesn't need this line

// Example

// Use LocalStorage
var storage = new BrowserStorageClass(localStorage);

// Use SessionStorage
var storage = new BrowserStorageClass(sessionStorage);

// If you want asynchronous
//
// Note:
// - Force all methods to return promise.
var storage = new BrowserStorageClass(localStorage, true);

Save or add new value

storage.set('yourkeyname1', 'Saving value as string');
storage.set('yourkeyname2', ['saving', 'value', 'as', 'array']);
storage.set('yourkeyname3', {name: 'budi', address: 'jakarta', info:'saving value as object'});

Note:

  • You are able to save tipe data value as string, array, and object.
  • You are able to use set to update the value. Because Key name is primary, it will automatically replace old value.

Get value from key

storage.get('yourkeyname2');

Check value is exist or not

if(storage.has('yourkeyname1')) {
    console.log('Found!');
} else {
    console.log('Not Found!');
}

Remove single data

storage.remove('yourkeyname2');

Clear all data

storage.clear();

List all saved data

storage.list();

API

  • BrowserStorageClass(_typeStorage[, _promisify]) _typeStorage {WindowLocalStorage} : localStorage or sessionStorage. _promisify {boolean} : If set to true, all methods will return as promise. Optional, default is false.

  • has(key) key {string} : Key name for your data value. returns {boolean}

  • set(key, value) key {string} : Key name for your data value. value {mixed} : Value can be string / array / object.

  • get(key) key {string} : Key name for your data value. returns {mixed} : Value can be string / array / object.

  • remove(key) key {string} : Key name for your data value.

  • clear() This will clear all data storage.

  • list() returns {array}

This will show all saved data in storage.

Unit Test

If you want to playing around with test

npm test