Home

Awesome

support support <a href="https://ostr.io/info/built-by-developers-for-developers"> <img src="https://ostr.io/apple-touch-icon-60x60.png" height="20"> </a>

Persistent Browser (Client) Storage

ClientStorage NPM library logo

Install:

npm install --save ClientStorage

Require:

const ClientStorage = require('ClientStorage').ClientStorage;
const clientStorage = new ClientStorage();

ES6 Import:

import { ClientStorage } from 'ClientStorage';
const clientStorage = new ClientStorage();

Usage:

Storage-specific usage:

By default ClientStorage package handle selecting storage driver in the next order (descending priority):

  1. localStorage
  2. cookies
  3. js (JS Object driven storage)

To alter priority pass "preferred driver" to new ClientStorage(driverName) constructor.

Use cookies only:

Pass cookies as an argument to new instance of ClientStorage:

const { clientStorage } = require('ClientStorage');
const cookiesStorage = new ClientStorage('cookies');
cookiesStorage.has('locale'); // false
cookiesStorage.set('locale', 'en_US'); // true

Use localStorage only:

Pass localStorage as an argument to new instance of ClientStorage:

const { clientStorage } = require('ClientStorage');
const locStorage = new ClientStorage('localStorage');
locStorage.has('locale'); // false
locStorage.set('locale', 'en_US'); // true

Use js only:

Pass js (in-memory js object) as an argument to new instance of ClientStorage:

const { clientStorage } = require('ClientStorage');
const jsStorage = new ClientStorage('js');
jsStorage.has('locale'); // false
jsStorage.set('locale', 'en_US'); // true

Note: All instances are sharing same cookie and localStorage records!

Examples:

const clientStorage = new (require('ClientStorage').ClientStorage);

clientStorage.set('locale', 'en'); // true
clientStorage.set('country', 'usa'); // true
clientStorage.set('gender', 'male'); // true

clientStorage.get('gender'); // male

clientStorage.has('locale'); // true
clientStorage.has('city'); // false

clientStorage.keys(); // ['locale', 'country', 'gender']

clientStorage.remove('locale'); // true
clientStorage.get('locale'); // undefined

clientStorage.keys(); // ['country', 'gender']

clientStorage.empty(); // true
clientStorage.keys(); // []

clientStorage.empty(); // false

Running Tests

Tests are written using Tiny, follow testing instruction in meteor docs

Support this project: