Permalink
Browse files

swapped dom-storage for submodule

  • Loading branch information...
coolaj86 committed Jul 28, 2013
1 parent 2ac9550 commit d7d78feee99a27630659bfc93dd0bf57c4f620d7
Showing with 4 additions and 237 deletions.
  1. +3 −0 .gitmodules
  2. +1 −0 dom-storage
  3. +0 −56 dom-storage/README.md
  4. +0 −108 dom-storage/lib/index.js
  5. +0 −16 dom-storage/lib/package.json
  6. +0 −57 dom-storage/tests/test.js
View
@@ -16,3 +16,6 @@
[submodule "inotify++"]
path = inotify++
url = https://github.com/coolaj86/node-inotify-plusplus.git
+[submodule "dom-storage"]
+ path = dom-storage
+ url = https://github.com/coolaj86/node-dom-storage.git
Submodule dom-storage added at e3415e
View
@@ -1,56 +0,0 @@
-localStorage
-===
-
-An inefficient, but as W3C-compliant as possible using only pure JavaScript, `localStorage` implementation.
-
-Purpose
-----
-
-This is meant for the purpose of being able to run unit-tests and such for browser-y modules in node.
-
-Usage
-----
-
- var localStorage = require('localStorage')
- , myValue = { foo: 'bar', baz: 'quux' }
- ;
-
- localStorage.setItem('myKey', JSON.stringify(myValue));
- myValue = localStorage.getItem('myKey');
-
-API
----
-
- * getItem(key)
- * setItem(key, value)
- * removeItem(key)
- * clear()
- * key(n)
- * length
-
-Tests
----
-
- 0 === localStorage.length;
- null === localStorage.getItem('doesn't exist');
- undefined === localStorage['doesn't exist'];
-
- localStorage.setItem('myItem');
- "undefined" === localStorage.getItem('myItem');
- 1 === localStorage.length;
-
- localStorage.setItem('myItem', 0);
- "0" === localStorage.getItem('myItem');
-
- localStorage.removeItem('myItem', 0);
- 0 === localStorage.length;
-
- localStorage.clear();
- 0 === localStorage.length;
-
-TODO / Bugs
----
-
- * Does not persist.
- * could use `fs.readFileSync` at load and an occasional `fs.writeFile` to write-out localStorage.json
- * Doesn't not emit `Storage` events
View
@@ -1,108 +0,0 @@
-/*jshint node:true es5:true laxcomma:true laxbreak:true*/
-// 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 fs = require('fs')
- ;
-
- function Storage(path) {
- var db
- ;
-
- Object.defineProperty(this, '___priv_bk___', {
- value: {
- path: path
- }
- , writable: false
- , enumerable: false
- });
-
- try {
- db = JSON.parse(fs.readFileSync(path));
- } catch(e) {
- db = {};
- }
-
- Object.keys(db).forEach(function (key) {
- this[key] = db[key];
- }, this);
- }
-
- Storage.prototype.getItem = function (key) {
- if (key in this) {
- return String(this[key]);
- }
- return null;
- };
-
- Storage.prototype.setItem = function (key, val) {
- this[key] = String(val);
- this.___save___();
- };
-
- Storage.prototype.removeItem = function (key) {
- delete this[key];
- this.___save___();
- };
-
- Storage.prototype.clear = function () {
- var self = this;
- // filters out prototype keys
- Object.keys(self).forEach(function (key) {
- self[key] = undefined;
- delete self[key];
- });
- };
-
- Storage.prototype.key = function (i) {
- i = i || 0;
- return Object.keys(this)[i];
- };
-
- Storage.prototype.__defineGetter__('length', function () {
- return Object.keys(this).length;
- });
-
- Storage.prototype.___save___ = function () {
- var self = this
- ;
-
- if (!this.___priv_bk___.path) {
- return;
- }
-
- if (this.___priv_bk___.lock) {
- this.___priv_bk___.wait = true;
- return;
- }
-
- this.___priv_bk___.lock = true;
- fs.writeFile(this.___priv_bk___.path, JSON.stringify(this), 'utf8', function (e) {
- self.___priv_bk___.lock = false;
- if (e) {
- return;
- }
- if (self.___priv_bk___.wait) {
- self.___priv_bk___.wait = false;
- self.___save___();
- }
- });
- };
-
- Object.defineProperty(Storage, 'create', {
- value: function (path) {
- return new Storage(path);
- }
- , writable: false
- , enumerable: false
- });
-
- module.exports = Storage;
-}());
@@ -1,16 +0,0 @@
-{
- "author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.info)",
- "name": "dom-storage",
- "description": "W3C DOM Storage (localStorage and sessionStorage) for Node.JS",
- "version": "1.0.0",
- "repository": {
- "type": "git",
- "url": "git://github.com/coolaj86/node-browser-compat.git"
- },
- "engines": {
- "node": "*"
- },
- "main": "index.js",
- "dependencies": {},
- "devDependencies": {}
-}
View
@@ -1,57 +0,0 @@
-/*jshint node:true es5:true laxcomma:true laxbreak:true*/
-(function () {
- "use strict";
-
- var assert = require('assert')
- , fs = require('fs')
- , Storage = require('dom-storage')
- , dbPath = './db.json'
- ;
-
- function runTest(storage) {
- assert.strictEqual(0, Object.keys(storage).length);
- assert.strictEqual(0, storage.length);
-
- // can't make assuptions about key positioning
- storage.setItem('a', 1);
- assert.strictEqual(storage.key(0), 'a');
-
- storage.setItem('b', '2');
- assert.strictEqual(storage.getItem('a'), '1');
- assert.strictEqual(storage.getItem('b'), '2');
- assert.strictEqual(storage.length, 2);
-
- assert.strictEqual(storage['c'], undefined);
- assert.strictEqual(storage.getItem('c'), null);
-
- storage.setItem('c');
- assert.strictEqual(storage.getItem('c'), "undefined");
- assert.strictEqual(storage.length, 3);
-
- storage.removeItem('c');
- assert.strictEqual(storage.getItem('c'), null);
- assert.strictEqual(storage.length, 2);
-
- storage.clear();
- assert.strictEqual(storage.getItem('a'), null);
- assert.strictEqual(storage.getItem('b'), null);
- assert.strictEqual(storage.length, 0);
- }
-
- function runAll() {
- var localStorage = new Storage(dbPath)
- , sessionStorage = new Storage()
- ;
-
- runTest(sessionStorage);
- runTest(localStorage);
-
- localStorage.setItem('a', 1);
- setTimeout(function () {
- assert.deepEqual({ a: 1 }, JSON.parse(fs.readFileSync(dbPath)));
- console.log('All tests passed');
- }, 100);
- }
-
- fs.unlink(dbPath, runAll);
-}());

0 comments on commit d7d78fe

Please sign in to comment.