Skip to content

Commit e957f88

Browse files
committed
📔 Documentation update
1 parent 18a83f1 commit e957f88

File tree

1 file changed

+73
-57
lines changed

1 file changed

+73
-57
lines changed

README.md

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
# Persistent Client (Browser) Storage
2-
3-
<a href="https://www.patreon.com/bePatron?u=20396046">
4-
<img src="https://c5.patreon.com/external/logo/become_a_patron_button@2x.png" width="160">
1+
[![support](https://img.shields.io/badge/support-GitHub-white)](https://github.com/sponsors/dr-dimitru)
2+
[![support](https://img.shields.io/badge/support-PayPal-white)](https://paypal.me/veliovgroup)
3+
<a href="https://ostr.io/info/built-by-developers-for-developers">
4+
<img src="https://ostr.io/apple-touch-icon-60x60.png" height="20">
55
</a>
66

7+
# Persistent Browser (Client) Storage
8+
79
- 👷 __100% Tests coverage__;
810
- 📦 No external dependencies;
911
- 💪 Bulletproof persistent Client storage;
@@ -35,85 +37,98 @@ meteor npm install --save ClientStorage
3537

3638
```js
3739
var ClientStorage = require('ClientStorage').ClientStorage;
40+
var clientStorage = new ClientStorage();
41+
```
42+
43+
### ES6 Import:
44+
45+
```js
46+
import { ClientStorage } from 'ClientStorage';
47+
const clientStorage = new ClientStorage();
3848
```
3949

4050
### ES6 Import (Meteor):
4151

4252
```js
4353
import { ClientStorage } from 'meteor/ostrio:cstorage';
54+
const clientStorage = new ClientStorage();
4455
```
4556

4657
## Usage:
4758

48-
- `ClientStorage.get('key')` - Read a record. If the key doesn't exist a *undefined* value will be returned;
59+
- `clientStorage.get('key')` - Read a record. If the key doesn't exist a *undefined* value will be returned;
4960
- `key` - {*String*} - Record's key;
50-
- `ClientStorage.set('key', value[, ttl])` - Create/overwrite a value in storage;
61+
- `clientStorage.set('key', value[, ttl])` - Create/overwrite a value in storage;
5162
- `key` - {*String*} - Record's key;
5263
- `value` - {*String*|[*mix*]|*Boolean*|*Object*} - Record's value (content);
5364
- `ttl` - {*Number*} — [Optional] Record's TTL in seconds;
54-
- `ClientStorage.remove('key')` - Remove a record;
65+
- `clientStorage.remove('key')` - Remove a record;
5566
- `key` - {*String*} - Record's key;
56-
- `ClientStorage.has('key')` - Check whether a record exists, returns a boolean value;
67+
- `clientStorage.has('key')` - Check whether a record exists, returns a boolean value;
5768
- `key` - {*String*} - Record's key;
58-
- `ClientStorage.keys()` - Returns an array of all storage keys;
59-
- `ClientStorage.empty()` - Empty storage (remove all key/value pairs). __Use with caution! (*May remove cookies which weren't set by you*)__.
69+
- `clientStorage.keys()` - Returns an array of all storage keys;
70+
- `clientStorage.empty()` - Empty storage (remove all key/value pairs). __Use with caution! (*May remove cookies which weren't set by you*)__.
6071

6172
## Alternate usage:
6273

63-
### Use `cookies` only:
74+
By default ClientStorage package handle selecting storage driver in the next order (descending priority):
6475

65-
To use `cookies` as a driver for `ClientStorage` create new instance of `clientStorage` (*camel-case, first letter __lower-case__*):
76+
1. `localStorage`
77+
2. `cookies`
78+
3. `js` (*JS Object driven storage*)
6679

67-
```js
68-
var clientStorage = require('ClientStorage').clientStorage;
69-
var csCookies = new clientStorage('cookies');
70-
csCookies.has('locale'); // false
71-
csCookies.set('locale', 'en_US'); // true
72-
```
80+
To alter priority pass "preferred driver" to `new ClientStorage(driverName)` constructor.
81+
82+
### Use `cookies` only:
7383

74-
or in ES6 (Meteor):
84+
Pass `cookies` as an argument to new instance of `ClientStorage`:
7585

7686
```js
77-
import { clientStorage } from 'meteor/ostrio:cstorage';
78-
const csLocalStorage = new clientStorage('cookies');
79-
csLocalStorage.has('locale'); // false
80-
csLocalStorage.set('locale', 'en_US'); // true
87+
const { clientStorage } = require('ClientStorage');
88+
var cookiesStorage = new ClientStorage('cookies');
89+
cookiesStorage.has('locale'); // false
90+
cookiesStorage.set('locale', 'en_US'); // true
8191
```
8292

8393
### Use `localStorage` only:
8494

85-
To use `localStorage` as a driver for `ClientStorage` create new instance of `clientStorage` (*camel-case, first letter __lower-case__*):
95+
Pass `localStorage` as an argument to new instance of `ClientStorage`:
8696

8797
```js
88-
var clientStorage = require('ClientStorage').clientStorage;
89-
var csLocalStorage = new clientStorage('localStorage');
90-
csLocalStorage.has('locale'); // false
91-
csLocalStorage.set('locale', 'en_US'); // true
98+
const { clientStorage } = require('ClientStorage');
99+
var locStorage = new ClientStorage('localStorage');
100+
locStorage.has('locale'); // false
101+
locStorage.set('locale', 'en_US'); // true
92102
```
93103

94-
or in ES6 (Meteor):
104+
### Use `js` only:
105+
106+
Pass `js` (*in-memory js object*) as an argument to new instance of `ClientStorage`:
95107

96108
```js
97-
import { clientStorage } from 'meteor/ostrio:cstorage';
98-
const csLocalStorage = new clientStorage('localStorage');
99-
csLocalStorage.has('locale'); // false
100-
csLocalStorage.set('locale', 'en_US'); // true
109+
const { clientStorage } = require('ClientStorage');
110+
var jsStorage = new ClientStorage('js');
111+
jsStorage.has('locale'); // false
112+
jsStorage.set('locale', 'en_US'); // true
101113
```
102114

103115
__Note:__ *All instances are sharing same cookie and localStorage records!*
104116

105117
## [Meteor] Add reactivity:
106118

119+
Persistent `ReactiveVar` implementation:
120+
107121
```js
108-
import { ReactiveVar } from 'meteor/reactive-var';
122+
import { ReactiveVar } from 'meteor/reactive-var';
109123
import { ClientStorage } from 'meteor/ostrio:cstorage';
124+
const clientStorage = new ClientStorage();
110125

111-
const persistentReactive = (name, initial = false) => {
126+
const persistentReactive = (name, initial = undefined) => {
112127
let reactive;
113-
if (ClientStorage.has(name)) {
114-
reactive = new ReactiveVar(ClientStorage.get(name));
128+
if (clientStorage.has(name)) {
129+
reactive = new ReactiveVar(clientStorage.get(name));
115130
} else {
116-
ClientStorage.set(name, initial);
131+
clientStorage.set(name, initial);
117132
reactive = new ReactiveVar(initial);
118133
}
119134

@@ -123,43 +138,43 @@ const persistentReactive = (name, initial = false) => {
123138
return;
124139
}
125140
reactive.curValue = newValue;
126-
ClientStorage.set(name, newValue);
141+
clientStorage.set(name, newValue);
127142
reactive.dep.changed();
128143
};
129144

130145
return reactive;
131146
};
132147

133-
const UILayout = persistentReactive('UILayout', 'two-columns');
134-
UILayout.get(); // two-columns
135-
UILayout.set('single-column');
148+
const layout = persistentReactive('ui-layout', 'two-columns');
149+
layout.get(); // two-columns
150+
layout.set('single-column');
136151
```
137152

138153
## Examples:
139154

140155
```js
141-
var ClientStorage = require('ClientStorage').ClientStorage;
156+
const clientStorage = new (require('ClientStorage').ClientStorage);
142157

143-
ClientStorage.set('locale', 'en'); // true
144-
ClientStorage.set('country', 'usa'); // true
145-
ClientStorage.set('gender', 'male'); // true
158+
clientStorage.set('locale', 'en'); // true
159+
clientStorage.set('country', 'usa'); // true
160+
clientStorage.set('gender', 'male'); // true
146161

147-
ClientStorage.get('gender'); // male
162+
clientStorage.get('gender'); // male
148163

149-
ClientStorage.has('locale'); // true
150-
ClientStorage.has('city'); // false
164+
clientStorage.has('locale'); // true
165+
clientStorage.has('city'); // false
151166

152-
ClientStorage.keys(); // ['locale', 'country', 'gender']
167+
clientStorage.keys(); // ['locale', 'country', 'gender']
153168

154-
ClientStorage.remove('locale'); // true
155-
ClientStorage.get('locale'); // undefined
169+
clientStorage.remove('locale'); // true
170+
clientStorage.get('locale'); // undefined
156171

157-
ClientStorage.keys(); // ['country', 'gender']
172+
clientStorage.keys(); // ['country', 'gender']
158173

159-
ClientStorage.empty(); // true
160-
ClientStorage.keys(); // []
174+
clientStorage.empty(); // true
175+
clientStorage.keys(); // []
161176

162-
ClientStorage.empty(); // false
177+
clientStorage.empty(); // false
163178
```
164179

165180
## Running Tests
@@ -183,5 +198,6 @@ MONGO_URL="mongodb://127.0.0.1:27017/client-storage-tests" meteor test-packages
183198

184199
## Support this project:
185200

186-
- [Become a patron](https://www.patreon.com/bePatron?u=20396046) — support my open source contributions with monthly donation
201+
- [Sponsor via GitHub](https://github.com/sponsors/dr-dimitru)
202+
- [Support via PayPal](https://paypal.me/veliovgroup)
187203
- Use [ostr.io](https://ostr.io)[Monitoring](https://snmp-monitoring.com), [Analytics](https://ostr.io/info/web-analytics), [WebSec](https://domain-protection.info), [Web-CRON](https://web-cron.info) and [Pre-rendering](https://prerendering.com) for a website

0 commit comments

Comments
 (0)