diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..c6313a1 --- /dev/null +++ b/test/test.js @@ -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."); +}()); diff --git a/tests/test.js b/test/test2.js similarity index 93% rename from tests/test.js rename to test/test2.js index 9184908..f1b46b1 100644 --- a/tests/test.js +++ b/test/test2.js @@ -25,4 +25,6 @@ db.set('a', { 'a': [1] }); assert.deepEqual({'a': [1] }, db.get('a')); + + console.log('[PASS] no assertions failed'); }()); diff --git a/tests/node_modules/json-storage/json-storage.js b/tests/node_modules/json-storage/json-storage.js deleted file mode 100644 index 29c414c..0000000 --- a/tests/node_modules/json-storage/json-storage.js +++ /dev/null @@ -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; -}()); diff --git a/tests/node_modules/json-storage/package.json b/tests/node_modules/json-storage/package.json deleted file mode 100644 index fe90600..0000000 --- a/tests/node_modules/json-storage/package.json +++ /dev/null @@ -1,17 +0,0 @@ -{ - "author": "AJ ONeal (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": {} -} diff --git a/tests/node_modules/localStorage/localStorage.js b/tests/node_modules/localStorage/localStorage.js deleted file mode 100644 index 4a74dff..0000000 --- a/tests/node_modules/localStorage/localStorage.js +++ /dev/null @@ -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(); - } -}()); diff --git a/tests/node_modules/localStorage/package.json b/tests/node_modules/localStorage/package.json deleted file mode 100644 index 2d96f56..0000000 --- a/tests/node_modules/localStorage/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "author": "AJ ONeal (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": {} -}