Persistent Client (Browser) Storage
- Bulletproof persistent Client storage;
- Support for Unicode values and keys;
- Works in browser with disabled
localStorage
and cookies
; - 100% tests coverage.
Install:
npm install --save ClientStorage
Install Meteor:
# Via Atmosphere
meteor add ostrio:cstorage
# Via NPM
meteor npm install --save ClientStorage
Require:
var ClientStorage = require('ClientStorage').ClientStorage;
ES6 Import (Meteor):
import { 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 a 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 weren't set by you).
Alternate usage:
Use cookies
only
To use cookies
as a driver for ClientStorage
create new instance of clientStorage
(camel-case, first letter lower-case)
var clientStorage = require('ClientStorage').clientStorage;
var csCookies = new clientStorage('cookies');
or in ES6 (Meteor):
import { clientStorage } from 'meteor/ostrio:cstorage';
const 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):
var clientStorage = require('ClientStorage').clientStorage;
var csLocalStorage = new clientStorage('localStorage');
or in ES6 (Meteor):
import { clientStorage } from 'meteor/ostrio:cstorage';
const csLocalStorage = new clientStorage('localStorage');
Note: All instances shares same cookies and localStorage records!
Example:
ClientStorage.set('locale', 'en');
ClientStorage.set('country', 'usa');
ClientStorage.set('gender', 'male');
ClientStorage.get('gender');
ClientStorage.has('locale');
ClientStorage.has('city');
ClientStorage.keys();
ClientStorage.remove('locale');
ClientStorage.get('locale');
ClientStorage.keys();
ClientStorage.empty();
ClientStorage.keys();
ClientStorage.empty();