ostrio:cstorage

v2.1.1Published 7 years ago

This package has not had recent updates. Please investigate it's current state before committing to using it in your project.

Client Storage

  • Bulletproof Client storage functions, localStorage with fall-back to cookies
  • Works in browser with disabled localStorage and cookies
  • 100% tests coverage

Install:

npm install ClientStorage

Install Meteor:

meteor add ostrio:cstorage

Require:

1var ClientStorage = require('ClientStorage').ClientStorage;

ES6 Import (Meteor):

1import { ClientStorage } from 'meteor/ostrio:cstorage';

Usage:

Get

  • ClientStorage.get('key') - Read a record. If the key doesn't exist a null value will be returned.

Set

  • ClientStorage.set('key', value) - Create/overwrite a value in storage

Remove

  • ClientStorage.remove('key') - Remove a record

Has

  • ClientStorage.has('key') - Check whether a record exists, returns boolean value

Keys

  • ClientStorage.keys() - Returns an array of all storage keys

Empty

  • ClientStorage.empty() - Empty storage (remove all key/value pairs). Use with caution! (May remove cookies which was set not by you)

Use cookies only

To use cookies as a driver for ClientStorage create new instance of clientStorage (camel-case, first letter lower-case)

1var clientStorage  = require('ClientStorage').clientStorage;
2var csCookies = new clientStorage('cookies');

or in ES6 (Meteor):

1import { clientStorage } from 'meteor/ostrio:cstorage';
2let csLocalStorage = new clientStorage('cookies');

Use localStorage only

To use localStorage as a driver for ClientStorage create new instance of clientStorage (camel-case, first letter lower-case):

1var clientStorage  = require('ClientStorage').clientStorage;
2var csLocalStorage = new clientStorage('localStorage');

or in ES6 (Meteor):

1import { clientStorage } from 'meteor/ostrio:cstorage';
2let csLocalStorage = new clientStorage('localStorage');

Note: All instances shares same cookies and localStorage records!

Example:

1ClientStorage.set('locale', 'en'); // true
2ClientStorage.set('country', 'usa'); // true
3ClientStorage.set('gender', 'male'); // true
4
5ClientStorage.get('gender'); // male
6
7ClientStorage.has('locale'); // true
8ClientStorage.has('city'); // false
9
10ClientStorage.keys(); // ['locale', 'country', 'gender']
11
12ClientStorage.remove('locale'); // true
13ClientStorage.get('locale'); // undefined
14
15ClientStorage.keys(); // ['country', 'gender']
16
17ClientStorage.empty(); // true
18ClientStorage.keys(); // []
19
20ClientStorage.empty(); // false