put tests together

This commit is contained in:
AJ ONeal 2011-09-05 15:48:34 -06:00
parent 4a7c08d6f0
commit b75038ca44
7 changed files with 30 additions and 164 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

27
test/test.js Normal file
View File

@ -0,0 +1,27 @@
(function () {
"use strict";
var localStorage = require('localStorage')
, JsonStorage = require('json-storage')
, db = JsonStorage(localStorage)
, assert = require('assert')
;
assert.equal(null, db.get('x'));
assert.deepEqual([], db.keys());
db.clear();
assert.equal(null, db.get('x'));
db.set('a', 'b');
assert.deepEqual(['a'], db.keys());
assert.equal('b', db.get('a'));
db.remove('a');
assert.deepEqual([], db.keys());
db.set('a', 'b');
db.clear();
assert.deepEqual([], db.keys());
console.log("Done! All tests pass.");
}());

View File

@ -25,4 +25,6 @@
db.set('a', { 'a': [1] }); db.set('a', { 'a': [1] });
assert.deepEqual({'a': [1] }, db.get('a')); assert.deepEqual({'a': [1] }, db.get('a'));
console.log('[PASS] no assertions failed');
}()); }());

View File

@ -1,77 +0,0 @@
(function () {
"use strict";
var db;
function Stringify(obj) {
var str;
try {
str = JSON.stringify(obj);
} catch(e) {
str = "";
}
return str;
}
function Parse(str) {
var obj = null;
try {
obj = JSON.parse(str);
} catch(e) {}
return obj;
}
function JsonStorage(w3cStorage) {
this.db = w3cStorage;
this.keysAreDirty = true;
}
db = JsonStorage;
db.prototype.clear = function () {
this.keysAreDirty = true;
self.keys().forEach(function (uuid) {
this.remove(uuid);
}, this);
};
db.prototype.remove = function (uuid) {
this.keysAreDirty = true;
this.db.removeItem(uuid);
};
db.prototype.get = function (uuid) {
return Parse(this.db.getItem(uuid));
};
db.prototype.set = function (uuid, val) {
this.keysAreDirty = true;
return this.db.setItem(uuid, Stringify(val));
};
db.prototype.keys = function () {
var i;
if (!this.keysAreDirty) {
return this.keys;
}
this.keys = [];
for (i = 0; i < this.db.length; i += 1) {
this.keys.push(this.db.key(i));
}
this.keysAreDirty = false;
return this.keys.concat([]);
};
db.prototype.size = function () {
return this.db.length;
};
function create(w3cStorage) {
return new JsonStorage(w3cStorage);
}
module.exports = create;
}());

View File

@ -1,17 +0,0 @@
{
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "json-storage",
"description": "A wrapper for storage engines which use the W3C Storage API",
"keywords": ["ender", "localStorage", "sessionStorage", "globalStorage", "Storage"],
"version": "1.0.0",
"repository": {
"type": "git",
"url": "git://github.com/coolaj86/json-storage-js.git"
},
"engines": {
"node": ">= v0.2.0"
},
"main": "json-storage.js",
"dependencies": {},
"devDependencies": {}
}

View File

@ -1,54 +0,0 @@
// http://www.rajdeepd.com/articles/chrome/localstrg/LocalStorageSample.htm
// NOTE:
// this varies from actual localStorage in some subtle ways
// also, there is no persistence
// TODO persist
(function () {
"use strict";
var db;
function LocalStorage() {
}
db = LocalStorage;
db.prototype.getItem = function (key) {
if (key in this) {
return String(this[key]);
}
return null;
};
db.prototype.setItem = function (key, val) {
this[key] = String(val);
};
db.prototype.removeItem = function (key) {
delete this[key];
};
db.prototype.clear = function () {
var self = this;
Object.keys(self).forEach(function (key) {
self[key] = undefined;
delete self[key];
});
};
db.prototype.key = function (i) {
i = i || 0;
return Object.keys(this)[i];
};
db.prototype.__defineGetter__('length', function () {
return Object.keys(this).length;
});
if (global.localStorage) {
module.exports = localStorage;
} else {
module.exports = new LocalStorage();
}
}());

View File

@ -1,16 +0,0 @@
{
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
"name": "localStorage",
"description": "W3C localStorage for Node.JS",
"version": "1.0.0",
"repository": {
"type": "git",
"url": "git://github.com/coolaj86/node-localStorage.git"
},
"engines": {
"node": ">= v0.2.0"
},
"main": "localStorage.js",
"dependencies": {},
"devDependencies": {}
}