From a430f94fce7afadc6b621d8a7ab8bcd2d3bf9378 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sun, 26 Jan 2014 14:39:47 -0700 Subject: [PATCH] made more browser-friendly and added stringify option --- lib/README.md | 21 +++++++++++++++------ lib/{index.js => json-storage.js} | 12 ++++++++++-- lib/package.json | 8 ++++---- test/test.js | 4 ++-- test/test2.js | 4 ++-- 5 files changed, 33 insertions(+), 16 deletions(-) rename lib/{index.js => json-storage.js} (93%) diff --git a/lib/README.md b/lib/README.md index 0d6b937..c57ccc2 100644 --- a/lib/README.md +++ b/lib/README.md @@ -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 === diff --git a/lib/index.js b/lib/json-storage.js similarity index 93% rename from lib/index.js rename to lib/json-storage.js index 8a06072..33e9cde 100644 --- a/lib/index.js +++ b/lib/json-storage.js @@ -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) { diff --git a/lib/package.json b/lib/package.json index c0c3a3c..dbcaa28 100644 --- a/lib/package.json +++ b/lib/package.json @@ -1,9 +1,9 @@ { - "author": "AJ ONeal (http://coolaj86.info)", + "author": "AJ ONeal (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": {} diff --git a/test/test.js b/test/test.js index e871a76..e5b6f8e 100644 --- a/test/test.js +++ b/test/test.js @@ -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') ; diff --git a/test/test2.js b/test/test2.js index f1b46b1..fa68380 100644 --- a/test/test2.js +++ b/test/test2.js @@ -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'));