made more browser-friendly and added stringify option
This commit is contained in:
parent
699f673b79
commit
a430f94fce
|
@ -6,22 +6,28 @@ A light, sensible abstraction for DOMStorage (such as localStorage).
|
|||
Installation
|
||||
===
|
||||
|
||||
Ender.JS (Browser)
|
||||
Bower (Browser)
|
||||
|
||||
ender build json-storage
|
||||
```bash
|
||||
bower install json-storage
|
||||
# or
|
||||
wget https://raw2.github.com/coolaj86/json-storage-js/master/lib/json-storage.js
|
||||
```
|
||||
|
||||
Node.JS (Server)
|
||||
|
||||
npm install localStorage json-storage
|
||||
```bash
|
||||
npm install localStorage json-storage
|
||||
```
|
||||
|
||||
Usage
|
||||
===
|
||||
|
||||
Made fo for Node.JS and Ender.JS (browser-side).
|
||||
Made for Node.js and Bower (browser-side).
|
||||
|
||||
var localStorage = require('localStorage')
|
||||
, JsonStorage = require('json-storage')
|
||||
, store = JsonStorage.create(localStorage, 'my-widget-namespace')
|
||||
, JsonStorage = require('json-storage').JsonStorage
|
||||
, store = JsonStorage.create(localStorage, 'my-widget-namespace', { stringify: false })
|
||||
, myValue = {
|
||||
foo: "bar"
|
||||
, baz: "quux"
|
||||
|
@ -31,6 +37,9 @@ Made fo for Node.JS and Ender.JS (browser-side).
|
|||
store.set('myKey', myValue);
|
||||
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
|
||||
===
|
||||
|
||||
|
|
|
@ -35,7 +35,7 @@
|
|||
}
|
||||
|
||||
me._opts = opts || {};
|
||||
if (false === opts.stringify) {
|
||||
if (false === me._opts.stringify) {
|
||||
me._stringify = false;
|
||||
} else {
|
||||
me._stringify = true;
|
||||
|
@ -69,7 +69,15 @@
|
|||
var item = this._store.getItem(key + this._namespace)
|
||||
;
|
||||
|
||||
return this._stringify && parse(item) || item;
|
||||
if ('undefined' === typeof item) {
|
||||
item = null;
|
||||
}
|
||||
|
||||
if (this._stringify) {
|
||||
item = parse(item);
|
||||
}
|
||||
|
||||
return item;
|
||||
};
|
||||
|
||||
proto.set = function (key, val) {
|
|
@ -1,9 +1,9 @@
|
|||
{
|
||||
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
|
||||
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.com)",
|
||||
"name": "json-storage",
|
||||
"description": "A wrapper for storage engines which use the W3C Storage API",
|
||||
"keywords": ["localStorage", "sessionStorage", "globalStorage", "Storage"],
|
||||
"version": "1.1.3",
|
||||
"keywords": ["dom", "storage", "json", "w3c", "localStorage", "sessionStorage", "globalStorage", "Storage"],
|
||||
"version": "2.0.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/coolaj86/json-storage-js.git"
|
||||
|
@ -11,7 +11,7 @@
|
|||
"engines": {
|
||||
"node": ">= v0.2.0"
|
||||
},
|
||||
"main": "index",
|
||||
"main": "json-storage",
|
||||
"browserDependencies": {},
|
||||
"dependencies": {},
|
||||
"devDependencies": {}
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
"use strict";
|
||||
|
||||
var localStorage = require('localStorage')
|
||||
, JsonStorage = require('json-storage')
|
||||
, db = JsonStorage(localStorage)
|
||||
, JsonStorage = require('../lib/').JsonStorage
|
||||
, db = JsonStorage.create(localStorage)
|
||||
, assert = require('assert')
|
||||
;
|
||||
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
|
||||
var assert = require('assert')
|
||||
, localStorage = require('localStorage')
|
||||
, JsonStorage = require('json-storage')
|
||||
, db = JsonStorage(localStorage)
|
||||
, JsonStorage = require('../lib/').JsonStorage
|
||||
, db = JsonStorage.create(localStorage)
|
||||
;
|
||||
|
||||
assert.strictEqual(null, db.get('a'));
|
||||
|
|
Loading…
Reference in New Issue