json-storage.js/README.md

119 lines
3.5 KiB
Markdown
Raw Permalink Normal View History

2012-03-02 18:51:58 +00:00
JsonStorage
====
2012-03-02 18:53:23 +00:00
A light, sensible abstraction for DOMStorage (such as localStorage).
2011-08-28 02:44:20 +00:00
Installation
===
Bower (Browser)
2011-08-28 02:44:20 +00:00
```bash
bower install json-storage
# or
2018-07-31 16:49:03 +00:00
wget https://git.coolaj86.com/coolaj86/json-storage.js/raw/branch/master/json-storage.js
```
2011-08-28 02:44:20 +00:00
Node.JS (Server)
```bash
2014-02-03 21:34:54 +00:00
npm install -S localStorage json-storage
```
2011-08-28 02:44:20 +00:00
Usage
===
Made for Node.js and Bower (browser-side).
2014-02-24 12:23:47 +00:00
```javascript
var localStorage = require('localStorage')
, JsonStorage = require('json-storage').JsonStorage
, store = JsonStorage.create(localStorage, 'my-widget-namespace', { stringify: true })
, myValue = {
foo: "bar"
, baz: "quux"
}
;
2018-07-31 16:49:03 +00:00
store.set('myKey', myValue);
2014-02-24 12:23:47 +00:00
myValue = store.get('myKey');
```
NOTE: When using with Node and the `localStorage` module,
you may wish to pass the `{ stringify: false }` option to prevent double stringification.
API
===
2014-02-03 21:08:54 +00:00
* `JsonStorage.create(DOMStorage, namespace, opts)`
* `DOMStorage` should be globalStorage, sessionStorage, or localStorage. Defaults to window.localStorage if set to `null`.
2014-02-03 21:13:55 +00:00
* `namespace` is optional string which allows multiple non-conflicting storage containers. For example you could pass two widgets different storage containers and not worry about naming conflicts:
* `Gizmos.create(JsonStorage.create(null, 'my-gizmos'))`
* `Gadgets.create(JsonStorage.create(null, 'my-gadgets'))`
2014-02-24 12:23:47 +00:00
* Namespacing can be turned off by explicitly setting `false`
* `Gadgets.create(JsonStorage.create(null, false))`
2014-02-03 21:08:54 +00:00
* `opts`
* `stringify` set to `false` in `node` to avoid double stringifying
2012-03-02 18:51:58 +00:00
* `store.get(key)`
* `store.set(key, value)`
* `store.remove(key)`
* `store.clear()`
* `store.keys()`
* `store.size()`
* `store.toJSON()`
2012-03-02 18:35:53 +00:00
* `JSON.stringify(store)`
2014-02-03 21:13:19 +00:00
**NOTE**: You cannot omit optional parameters. Use `null` if you want accepts the defaults for some things and provide a values for others. For example: `JsonStorage.create(null, null, { stringify: false })`
2014-02-03 21:33:28 +00:00
JSON / DOMStorage Conversion Gotchas
===
2011-08-28 02:44:20 +00:00
These notes do not reflect a bugs or defects in this library,
they're simply to inform you of a few 'gotchas' inherent in JSON / DOMStorage conversion.
99.999% of the time these gotchas shouldn't effect you in any way.
If they do, you're probably doing something wrong in the first place.
2014-02-03 21:33:28 +00:00
### `undefined` vs `null`
2012-03-02 18:35:53 +00:00
It is not valid to set `undefined` in JSON. So setting a key to `undefined` will remove it from the store.
2011-08-28 02:44:20 +00:00
2012-03-02 18:35:53 +00:00
This means that `store.set('x')` is the same as `store.remove('x')`.
2011-08-28 02:44:20 +00:00
To save `undefined`, use `null` instead.
Note that both values that exist as `null` and values that don't exist at all will return `null`.
2014-02-24 12:23:47 +00:00
```javascript
store.set('existing-key', null);
null === store.get('existing-key');
null === store.get('non-existant-key');
```
2014-02-03 21:33:28 +00:00
### `null` vs `"null"`
2011-08-28 02:44:20 +00:00
The special case of `null` as `"null"`, aka `"\"null\""`:
2018-07-31 16:58:36 +00:00
```
typeof null // object
typeof "null" // string
typeof "\"null\"" // string
```
2011-08-28 02:44:20 +00:00
`null`, and `"null"` both parse as `null` the "object", instead of one being the string (which would be `"\"null\""`).
2018-07-31 16:58:36 +00:00
```
JSON.parse(null) // null (object)
JSON.parse("null") // null (object)
JSON.parse("\"null\"") // 'null' (string)
```
2012-03-02 18:35:53 +00:00
Objects containing `null`, however, parse as expected `{ "foo": null, "bar": "null" }` will parse as `foo` being `null` but `bar` being `"null"`, much unlike the value `"null"` being parsed on its own.
2018-07-31 16:58:36 +00:00
```
JSON.parse('{ "foo": null }') // { foo: null }
JSON.parse('{ "foo": "null" }') // { foo: 'null' }
```