ostrio:cstorage

v3.0.0Published 5 years ago

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

Persistent Client (Browser) Storage

  • 👷 100% Tests coverage;
  • 📦 No external dependencies;
  • 💪 Bulletproof persistent Client storage;
  • ㊗️ With Unicode support for values and keys;
  • 👨‍💻 With String, Array, Object, and Boolean support as values;
  • ♿ Works with disabled localStorage and cookies.

ClientStorage NPM library logo

Install:

npm install --save ClientStorage

Install Meteor:

# Via Atmosphere
meteor add ostrio:cstorage
# Via NPM
meteor npm install --save ClientStorage

Require:

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

ES6 Import (Meteor):

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

Usage:

  • ClientStorage.get('key') - Read a record. If the key doesn't exist a undefined value will be returned;
    • key - {String} - Record's key;
  • ClientStorage.set('key', value[, ttl]) - Create/overwrite a value in storage;
    • key - {String} - Record's key;
    • value - {String|[mix]|Boolean|Object} - Record's value (content);
    • ttl - {Number} — [Optional] Record's TTL in seconds;
  • ClientStorage.remove('key') - Remove a record;
    • key - {String} - Record's key;
  • ClientStorage.has('key') - Check whether a record exists, returns a boolean value;
    • key - {String} - Record's key;
  • ClientStorage.keys() - Returns an array of all storage keys;
  • 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):

1var clientStorage  = require('ClientStorage').clientStorage;
2var csCookies = new clientStorage('cookies');
3csCookies.has('locale'); // false
4csCookies.set('locale', 'en_US'); // true

or in ES6 (Meteor):

1import { clientStorage } from 'meteor/ostrio:cstorage';
2const csLocalStorage = new clientStorage('cookies');
3csLocalStorage.has('locale'); // false
4csLocalStorage.set('locale', 'en_US'); // true

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');
3csLocalStorage.has('locale'); // false
4csLocalStorage.set('locale', 'en_US'); // true

or in ES6 (Meteor):

1import { clientStorage } from 'meteor/ostrio:cstorage';
2const csLocalStorage = new clientStorage('localStorage');
3csLocalStorage.has('locale'); // false
4csLocalStorage.set('locale', 'en_US'); // true

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

[Meteor] Add reactivity:

1import { ReactiveVar }   from 'meteor/reactive-var';
2import { ClientStorage } from 'meteor/ostrio:cstorage';
3
4const persistentReactive = (name, initial = false) => {
5  let reactive;
6  if (ClientStorage.has(name)) {
7    reactive = new ReactiveVar(ClientStorage.get(name));
8  } else {
9    ClientStorage.set(name, initial);
10    reactive = new ReactiveVar(initial);
11  }
12
13  reactive.set = function (newValue) {
14    let oldValue = reactive.curValue;
15    if ((reactive.equalsFunc || ReactiveVar._isEqual)(oldValue, newValue)) {
16      return;
17    }
18    reactive.curValue = newValue;
19    ClientStorage.set(name, newValue);
20    reactive.dep.changed();
21  };
22
23  return reactive;
24};
25
26const UILayout = persistentReactive('UILayout', 'two-columns');
27UILayout.get(); // two-columns
28UILayout.set('single-column');

Examples:

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

Running Tests

  1. Clone this package
  2. In Terminal (Console) go to directory where package is cloned
  3. Then run:

Meteor/Tinytest

# Default
meteor test-packages ./

# With custom port
meteor test-packages ./ --port 8888

# With local MongoDB and custom port
MONGO_URL="mongodb://127.0.0.1:27017/client-storage-tests" meteor test-packages ./ --port 8888

Support this project: