-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// 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(); | ||
} | ||
}()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"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": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
(function () { | ||
"use strict"; | ||
|
||
var assert = require('assert') | ||
, localStorage = require('localStorage') | ||
; | ||
|
||
// can't make assuptions about key positioning | ||
localStorage.setItem('a', 1); | ||
assert.strictEqual(localStorage.key(0), 'a'); | ||
|
||
localStorage.setItem('b', '2'); | ||
assert.strictEqual(localStorage.getItem('a'), '1'); | ||
assert.strictEqual(localStorage.getItem('b'), '2'); | ||
assert.strictEqual(localStorage.length, 2); | ||
|
||
assert.strictEqual(localStorage['c'], undefined); | ||
assert.strictEqual(localStorage.getItem('c'), null); | ||
|
||
localStorage.setItem('c'); | ||
assert.strictEqual(localStorage.getItem('c'), "undefined"); | ||
assert.strictEqual(localStorage.length, 3); | ||
|
||
localStorage.removeItem('c'); | ||
assert.strictEqual(localStorage.getItem('c'), null); | ||
assert.strictEqual(localStorage.length, 2); | ||
|
||
localStorage.clear(); | ||
assert.strictEqual(localStorage.getItem('a'), null); | ||
assert.strictEqual(localStorage.getItem('b'), null); | ||
assert.strictEqual(localStorage.length, 0); | ||
|
||
console.log('All tests passed'); | ||
}()); |