browser-storage-class
TypeScript icon, indicating that this package has built-in type declarations

1.2.0 • Public • Published

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/browser-storage-class@1.2/dist/browser-storage-class.min.js"></script>

<!-- Get a specific version -->
<!-- Recommended for production sites! -->
<script src="https://cdn.jsdelivr.net/npm/browser-storage-class@1.2.0/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

Package Sidebar

Install

npm i browser-storage-class

Weekly Downloads

8

Version

1.2.0

License

MIT

Unpacked Size

39.2 kB

Total Files

13

Last publish

Collaborators

  • aalfiann