From af4a0fb7e281bba0e86f4a8cde54d222cea20e7a Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Sat, 8 Mar 2014 14:55:21 -0700 Subject: [PATCH] added whitespace, updated tests for non-strict mode, fixed undefined vs null and string vs number bugs --- README.md | 7 ++++++- lib/index.js | 53 ++++++++++++++++++++++++++++++++++----------------- package.json | 2 +- tests/test.js | 13 ++++++------- 4 files changed, 49 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 121603c..0fc3ea5 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Usage var Storage = require('dom-storage') // in-file, doesn't call `String(val)` on values (default) - , localStorage = new Storage('./db.json', { strict: false }) + , localStorage = new Storage('./db.json', { strict: false, ws: ' ' }) // in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'` , sessionStorage = new Storage(null, { strict: true }) @@ -41,6 +41,11 @@ API * key(n) * length +### Options + + * strict - whether to stringify strictly as text `[Object object]` or as json `{ foo: bar }`. + * ws - the whitespace to use saving json to disk. Defaults to `' '`. + Tests --- diff --git a/lib/index.js b/lib/index.js index b4c3945..b28c284 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,4 +1,3 @@ -/*jshint node:true es5:true laxcomma:true laxbreak:true*/ // http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm // NOTE: @@ -18,17 +17,23 @@ ; Object.defineProperty(this, '___priv_bk___', { - value: { - path: path - } - , writable: false - , enumerable: false + value: { + path: path + } + , writable: false + , enumerable: false }); Object.defineProperty(this, '___priv_strict___', { - value: !!opts.strict - , writable: false - , enumerable: false + value: !!opts.strict + , writable: false + , enumerable: false + }); + + Object.defineProperty(this, '___priv_ws___', { + value: opts.ws || ' ' + , writable: false + , enumerable: false }); try { @@ -44,13 +49,23 @@ Storage.prototype.getItem = function (key) { if (this.hasOwnProperty(key)) { - return this.___priv_strict___ && String(this[key]) || this[key]; + if (this.___priv_strict___) { + return String(this[key]); + } else { + return this[key]; + } } return null; }; Storage.prototype.setItem = function (key, val) { - this[key] = this.___priv_strict___ && String(val) || val; + if (val === undefined) { + this[key] = null; + } else if (this.___priv_strict___) { + this[key] = String(val); + } else { + this[key] = val; + } this.___save___(); }; @@ -91,7 +106,11 @@ } this.___priv_bk___.lock = true; - fs.writeFile(this.___priv_bk___.path, JSON.stringify(this), 'utf8', function (e) { + fs.writeFile( + this.___priv_bk___.path + , JSON.stringify(this, null, this.___priv_ws___) + , 'utf8' + , function (e) { self.___priv_bk___.lock = false; if (e) { return; @@ -104,11 +123,11 @@ }; Object.defineProperty(Storage, 'create', { - value: function (path, opts) { - return new Storage(path, opts); - } - , writable: false - , enumerable: false + value: function (path, opts) { + return new Storage(path, opts); + } + , writable: false + , enumerable: false }); module.exports = Storage; diff --git a/package.json b/package.json index 0224354..92548d4 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "AJ ONeal (http://coolaj86.info)", "name": "dom-storage", "description": "W3C DOM Storage (localStorage and sessionStorage) for Node.JS", - "version": "2.0.0", + "version": "2.0.1", "repository": { "type": "git", "url": "git://github.com/coolaj86/node-dom-storage.git" diff --git a/tests/test.js b/tests/test.js index 9e02494..1653c51 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,16 +1,15 @@ -/*jshint node:true es5:true laxcomma:true laxbreak:true*/ (function () { "use strict"; var assert = require('assert') , fs = require('fs') - , Storage = require('dom-storage') + , Storage = require('../') , dbPath = './db.json' ; function runTest(storage) { // should not return prototype properties - assert.strictEqual(null, Object.getItem('key')); + assert.strictEqual(null, storage.getItem('key')); assert.strictEqual(0, Object.keys(storage).length); assert.strictEqual(0, storage.length); @@ -20,7 +19,7 @@ assert.strictEqual(storage.key(0), 'a'); storage.setItem('b', '2'); - assert.strictEqual(storage.getItem('a'), '1'); + assert.strictEqual(storage.getItem('a'), 1); assert.strictEqual(storage.getItem('b'), '2'); assert.strictEqual(storage.length, 2); @@ -28,7 +27,7 @@ assert.strictEqual(storage.getItem('c'), null); storage.setItem('c'); - assert.strictEqual(storage.getItem('c'), "undefined"); + assert.strictEqual(storage.getItem('c'), null); assert.strictEqual(storage.length, 3); storage.removeItem('c'); @@ -42,8 +41,8 @@ } function runAll() { - var localStorage = new Storage(dbPath) - , sessionStorage = new Storage() + var localStorage = new Storage(dbPath, { strict: false, ws: ' ' }) + , sessionStorage = new Storage(null, { strict: false }) ; runTest(sessionStorage);