added whitespace, updated tests for non-strict mode, fixed undefined vs null and string vs number bugs

This commit is contained in:
AJ ONeal 2014-03-08 14:55:21 -07:00
parent 9b930a09aa
commit af4a0fb7e2
4 changed files with 49 additions and 26 deletions

View File

@ -15,7 +15,7 @@ Usage
var Storage = require('dom-storage') var Storage = require('dom-storage')
// in-file, doesn't call `String(val)` on values (default) // 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]'` // in-memory, does call `String(val)` on values (i.e. `{}` becomes `'[object Object]'`
, sessionStorage = new Storage(null, { strict: true }) , sessionStorage = new Storage(null, { strict: true })
@ -41,6 +41,11 @@ API
* key(n) * key(n)
* length * 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 Tests
--- ---

View File

@ -1,4 +1,3 @@
/*jshint node:true es5:true laxcomma:true laxbreak:true*/
// http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm // http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm
// NOTE: // NOTE:
@ -18,17 +17,23 @@
; ;
Object.defineProperty(this, '___priv_bk___', { Object.defineProperty(this, '___priv_bk___', {
value: { value: {
path: path path: path
} }
, writable: false , writable: false
, enumerable: false , enumerable: false
}); });
Object.defineProperty(this, '___priv_strict___', { Object.defineProperty(this, '___priv_strict___', {
value: !!opts.strict value: !!opts.strict
, writable: false , writable: false
, enumerable: false , enumerable: false
});
Object.defineProperty(this, '___priv_ws___', {
value: opts.ws || ' '
, writable: false
, enumerable: false
}); });
try { try {
@ -44,13 +49,23 @@
Storage.prototype.getItem = function (key) { Storage.prototype.getItem = function (key) {
if (this.hasOwnProperty(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; return null;
}; };
Storage.prototype.setItem = function (key, val) { 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___(); this.___save___();
}; };
@ -91,7 +106,11 @@
} }
this.___priv_bk___.lock = true; 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; self.___priv_bk___.lock = false;
if (e) { if (e) {
return; return;
@ -104,11 +123,11 @@
}; };
Object.defineProperty(Storage, 'create', { Object.defineProperty(Storage, 'create', {
value: function (path, opts) { value: function (path, opts) {
return new Storage(path, opts); return new Storage(path, opts);
} }
, writable: false , writable: false
, enumerable: false , enumerable: false
}); });
module.exports = Storage; module.exports = Storage;

View File

@ -2,7 +2,7 @@
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)", "author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "dom-storage", "name": "dom-storage",
"description": "W3C DOM Storage (localStorage and sessionStorage) for Node.JS", "description": "W3C DOM Storage (localStorage and sessionStorage) for Node.JS",
"version": "2.0.0", "version": "2.0.1",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/coolaj86/node-dom-storage.git" "url": "git://github.com/coolaj86/node-dom-storage.git"

View File

@ -1,16 +1,15 @@
/*jshint node:true es5:true laxcomma:true laxbreak:true*/
(function () { (function () {
"use strict"; "use strict";
var assert = require('assert') var assert = require('assert')
, fs = require('fs') , fs = require('fs')
, Storage = require('dom-storage') , Storage = require('../')
, dbPath = './db.json' , dbPath = './db.json'
; ;
function runTest(storage) { function runTest(storage) {
// should not return prototype properties // 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, Object.keys(storage).length);
assert.strictEqual(0, storage.length); assert.strictEqual(0, storage.length);
@ -20,7 +19,7 @@
assert.strictEqual(storage.key(0), 'a'); assert.strictEqual(storage.key(0), 'a');
storage.setItem('b', '2'); 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.getItem('b'), '2');
assert.strictEqual(storage.length, 2); assert.strictEqual(storage.length, 2);
@ -28,7 +27,7 @@
assert.strictEqual(storage.getItem('c'), null); assert.strictEqual(storage.getItem('c'), null);
storage.setItem('c'); storage.setItem('c');
assert.strictEqual(storage.getItem('c'), "undefined"); assert.strictEqual(storage.getItem('c'), null);
assert.strictEqual(storage.length, 3); assert.strictEqual(storage.length, 3);
storage.removeItem('c'); storage.removeItem('c');
@ -42,8 +41,8 @@
} }
function runAll() { function runAll() {
var localStorage = new Storage(dbPath) var localStorage = new Storage(dbPath, { strict: false, ws: ' ' })
, sessionStorage = new Storage() , sessionStorage = new Storage(null, { strict: false })
; ;
runTest(sessionStorage); runTest(sessionStorage);