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
|
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)
|
Node.JS (Server)
|
||||||
|
|
||||||
|
```bash
|
||||||
npm install localStorage json-storage
|
npm install localStorage json-storage
|
||||||
|
```
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
===
|
===
|
||||||
|
|
||||||
Made fo for Node.JS and Ender.JS (browser-side).
|
Made for Node.js and Bower (browser-side).
|
||||||
|
|
||||||
var localStorage = require('localStorage')
|
var localStorage = require('localStorage')
|
||||||
, JsonStorage = require('json-storage')
|
, JsonStorage = require('json-storage').JsonStorage
|
||||||
, store = JsonStorage.create(localStorage, 'my-widget-namespace')
|
, store = JsonStorage.create(localStorage, 'my-widget-namespace', { stringify: false })
|
||||||
, myValue = {
|
, myValue = {
|
||||||
foo: "bar"
|
foo: "bar"
|
||||||
, baz: "quux"
|
, baz: "quux"
|
||||||
@ -31,6 +37,9 @@ Made fo for Node.JS and Ender.JS (browser-side).
|
|||||||
store.set('myKey', myValue);
|
store.set('myKey', myValue);
|
||||||
myValue = store.get('myKey');
|
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
|
API
|
||||||
===
|
===
|
||||||
|
|
||||||
|
@ -35,7 +35,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
me._opts = opts || {};
|
me._opts = opts || {};
|
||||||
if (false === opts.stringify) {
|
if (false === me._opts.stringify) {
|
||||||
me._stringify = false;
|
me._stringify = false;
|
||||||
} else {
|
} else {
|
||||||
me._stringify = true;
|
me._stringify = true;
|
||||||
@ -69,7 +69,15 @@
|
|||||||
var item = this._store.getItem(key + this._namespace)
|
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) {
|
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",
|
"name": "json-storage",
|
||||||
"description": "A wrapper for storage engines which use the W3C Storage API",
|
"description": "A wrapper for storage engines which use the W3C Storage API",
|
||||||
"keywords": ["localStorage", "sessionStorage", "globalStorage", "Storage"],
|
"keywords": ["dom", "storage", "json", "w3c", "localStorage", "sessionStorage", "globalStorage", "Storage"],
|
||||||
"version": "1.1.3",
|
"version": "2.0.0",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "git://github.com/coolaj86/json-storage-js.git"
|
"url": "git://github.com/coolaj86/json-storage-js.git"
|
||||||
@ -11,7 +11,7 @@
|
|||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= v0.2.0"
|
"node": ">= v0.2.0"
|
||||||
},
|
},
|
||||||
"main": "index",
|
"main": "json-storage",
|
||||||
"browserDependencies": {},
|
"browserDependencies": {},
|
||||||
"dependencies": {},
|
"dependencies": {},
|
||||||
"devDependencies": {}
|
"devDependencies": {}
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var localStorage = require('localStorage')
|
var localStorage = require('localStorage')
|
||||||
, JsonStorage = require('json-storage')
|
, JsonStorage = require('../lib/').JsonStorage
|
||||||
, db = JsonStorage(localStorage)
|
, db = JsonStorage.create(localStorage)
|
||||||
, assert = require('assert')
|
, assert = require('assert')
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
, localStorage = require('localStorage')
|
, localStorage = require('localStorage')
|
||||||
, JsonStorage = require('json-storage')
|
, JsonStorage = require('../lib/').JsonStorage
|
||||||
, db = JsonStorage(localStorage)
|
, db = JsonStorage.create(localStorage)
|
||||||
;
|
;
|
||||||
|
|
||||||
assert.strictEqual(null, db.get('a'));
|
assert.strictEqual(null, db.get('a'));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user