ClientStorage
Advanced tools
Comparing version 2.1.3 to 2.1.4
{ | ||
"name": "ClientStorage", | ||
"version": "2.1.3", | ||
"version": "2.1.4", | ||
"description": "Bulletproof persistent browser storage, works with disabled Cookies and/or localStorage", | ||
@@ -41,5 +41,8 @@ "main": "./client-storage.js", | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/VeliovGroup/Client-Storage/issues" | ||
}, | ||
"homepage": "https://github.com/VeliovGroup/Client-Storage", | ||
"author": "Dmitriy A. (https://veliovgroup.com)", | ||
"author": "dr.dimitru (https://veliovgroup.com)", | ||
"license": "BSD-3-Clause" | ||
} | ||
} |
137
README.md
@@ -1,11 +0,12 @@ | ||
Persistent Client (Browser) Storage | ||
======== | ||
# 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. | ||
- 😎 No external dependencies; | ||
- 💪 Bulletproof persistent Client storage; | ||
- ㊗️ Support for Unicode values and keys; | ||
- 👨💻 Support for `String`, `Array`, `Object`, and `Boolean` as values; | ||
- ♿︎ Works with disabled `localStorage` and `cookies`; | ||
- 👷♂️ __100%__ tests coverage. | ||
Install: | ||
======== | ||
## Install: | ||
```shell | ||
@@ -15,4 +16,4 @@ npm install --save ClientStorage | ||
Install Meteor: | ||
======== | ||
### Install Meteor: | ||
```shell | ||
@@ -28,67 +29,110 @@ # Via Atmosphere | ||
Require: | ||
======== | ||
```jsx | ||
### Require: | ||
```js | ||
var ClientStorage = require('ClientStorage').ClientStorage; | ||
``` | ||
ES6 Import (Meteor): | ||
======== | ||
```jsx | ||
### ES6 Import (Meteor): | ||
```js | ||
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. | ||
## Usage: | ||
#### Set | ||
- `ClientStorage.set('key', value)` - Create/overwrite a value in storage. | ||
- `ClientStorage.get('key')` - Read a record. If the key doesn't exist a null value will be returned; | ||
- `key` - `{String}` - Record's key; | ||
- `ClientStorage.set('key', value)` - Create/overwrite a value in storage; | ||
- `key` - `{String}` - Record's key; | ||
- `value` - `{String|[mix]|Boolean|Object}` - Record's value (content); | ||
- `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*)__. | ||
#### Remove | ||
- `ClientStorage.remove('key')` - Remove a record. | ||
## Alternate usage: | ||
#### Has | ||
- `ClientStorage.has('key')` - Check whether a record exists, returns a boolean value. | ||
### Use `cookies` only: | ||
#### Keys | ||
- `ClientStorage.keys()` - Returns an array of all storage keys. | ||
To use `cookies` as a driver for `ClientStorage` create new instance of `clientStorage` (*camel-case, first letter __lower-case__*): | ||
#### 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__*) | ||
```js | ||
var clientStorage = require('ClientStorage').clientStorage; | ||
var csCookies = new clientStorage('cookies'); | ||
csCookies.has('locale'); // false | ||
csCookies.set('locale', 'en_US'); // true | ||
``` | ||
or in ES6 (Meteor): | ||
```jsx | ||
```js | ||
import { clientStorage } from 'meteor/ostrio:cstorage'; | ||
const csLocalStorage = new clientStorage('cookies'); | ||
csLocalStorage.has('locale'); // false | ||
csLocalStorage.set('locale', 'en_US'); // true | ||
``` | ||
#### Use `localStorage` only | ||
### Use `localStorage` only: | ||
To use `localStorage` as a driver for `ClientStorage` create new instance of `clientStorage` (*camel-case, first letter __lower-case__*): | ||
```js | ||
var clientStorage = require('ClientStorage').clientStorage; | ||
var csLocalStorage = new clientStorage('localStorage'); | ||
csLocalStorage.has('locale'); // false | ||
csLocalStorage.set('locale', 'en_US'); // true | ||
``` | ||
or in ES6 (Meteor): | ||
```jsx | ||
```js | ||
import { clientStorage } from 'meteor/ostrio:cstorage'; | ||
const csLocalStorage = new clientStorage('localStorage'); | ||
csLocalStorage.has('locale'); // false | ||
csLocalStorage.set('locale', 'en_US'); // true | ||
``` | ||
__Note:__ *All instances shares same cookies and localStorage records!* | ||
__Note:__ *All instances are sharing same cookie and localStorage records!* | ||
Example: | ||
========= | ||
```javascript | ||
## [Meteor] Add reactivity: | ||
```js | ||
import { ReactiveVar } from 'meteor/reactive-var'; | ||
import { ClientStorage } from 'meteor/ostrio:cstorage'; | ||
const persistentReactive = (name, initial = false) => { | ||
let reactive; | ||
if (ClientStorage.has(name)) { | ||
reactive = new ReactiveVar(ClientStorage.get(name)); | ||
} else { | ||
ClientStorage.set(name, initial); | ||
reactive = new ReactiveVar(initial); | ||
} | ||
reactive.set = function (newValue) { | ||
let oldValue = reactive.curValue; | ||
if ((reactive.equalsFunc || ReactiveVar._isEqual)(oldValue, newValue)) { | ||
return; | ||
} | ||
reactive.curValue = newValue; | ||
ClientStorage.set(name, newValue); | ||
reactive.dep.changed(); | ||
}; | ||
return reactive; | ||
}; | ||
const UILayout = persistentReactive('UILayout', 'two-columns'); | ||
UILayout.get(); // two-columns | ||
UILayout.set('single-column'); | ||
``` | ||
## Examples: | ||
```js | ||
var ClientStorage = require('ClientStorage').ClientStorage; | ||
ClientStorage.set('locale', 'en'); // true | ||
@@ -115,1 +159,10 @@ ClientStorage.set('country', 'usa'); // true | ||
``` | ||
## Testing: | ||
Meteor/Tinytest | ||
```shell | ||
meteor test-packages ./ | ||
# PORT is required, and can be changed to any local open port | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
24070
11
0
166