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')
// 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
---

View File

@ -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;

View File

@ -2,7 +2,7 @@
"author": "AJ ONeal <coolaj86@gmail.com> (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"

View File

@ -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);