Skip to content
Browse files

Rename to Depot

  • Loading branch information...
1 parent bdab63e commit b6f78b4f11c17bb93490d81dce41b30b9f998416 @banterability committed Apr 18, 2012
View
6 Cakefile
@@ -1,11 +1,11 @@
{spawn, exec} = require 'child_process'
-task 'build', 'build localStorageWrapper (lib, tests & docs)', ->
+task 'build', 'build depot (lib, tests & docs)', ->
invoke 'build:lib'
invoke 'build:tests'
invoke 'build:docs'
-task 'build:lib', 'build the localStorageWrapper library', ->
+task 'build:lib', 'build the depot library', ->
console.log "Building app..."
runner 'coffee', ['-c', '-o', 'lib', 'src']
@@ -15,7 +15,7 @@ task 'build:tests', 'build Jasmine tests', ->
task 'build:docs', 'rebuild documentation with Docco', ->
console.log "Building documentation..."
- runner 'docco', ['src/localStorage.coffee']
+ runner 'docco', ['src/depot.coffee']
task 'test', 'run Jasmine tests', ->
runner 'open', ['spec/SpecRunner.html']
View
4 README.markdown
@@ -1,4 +1,4 @@
-# localStorageWrapper
+# Depot
A wrapper around the [window.localStorage API][1].
@@ -36,7 +36,7 @@ A wrapper around the [window.localStorage API][1].
- Key prefixing:
```javascript
- var store = new LocalStorageWrapper('myapp');
+ var store = new Depot('myapp');
value = store.get('username');
// same as: window.localStorage.getItem('myapp:username');
```
View
6 docs/localStorage.html → docs/depot.html
@@ -1,6 +1,6 @@
-<!DOCTYPE html> <html> <head> <title>localStorage.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> localStorage.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="nv">root = </span><span class="nx">exports</span> <span class="o">?</span> <span class="k">this</span>
+<!DOCTYPE html> <html> <head> <title>depot.coffee</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <link rel="stylesheet" media="all" href="docco.css" /> </head> <body> <div id="container"> <div id="background"></div> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th class="docs"> <h1> depot.coffee </h1> </th> <th class="code"> </th> </tr> </thead> <tbody> <tr id="section-1"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-1">&#182;</a> </div> </td> <td class="code"> <div class="highlight"><pre><span class="nv">root = </span><span class="nx">exports</span> <span class="o">?</span> <span class="k">this</span>
-<span class="k">class</span> <span class="nx">LocalStorageWrapper</span>
+<span class="k">class</span> <span class="nx">Depot</span>
<span class="nv">constructor: </span><span class="nf">(prefix = null)-&gt;</span>
<span class="vi">@store = </span><span class="nb">window</span><span class="p">.</span><span class="nx">localStorage</span>
<span class="vi">@prefix = </span><span class="nx">prefix</span></pre></div> </td> </tr> <tr id="section-2"> <td class="docs"> <div class="pilwrap"> <a class="pilcrow" href="#section-2">&#182;</a> </div> <p>Wraps window.localStorage.getItem
@@ -43,6 +43,6 @@
<span class="k">else</span>
<span class="nx">key</span>
-<span class="nv">root.LocalStorageWrapper = </span><span class="nx">LocalStorageWrapper</span>
+<span class="nv">root.Depot = </span><span class="nx">Depot</span>
</pre></div> </td> </tr> </tbody> </table> </div> </body> </html>
View
28 lib/localStorage.js → lib/depot.js
@@ -1,34 +1,34 @@
(function() {
- var LocalStorageWrapper, root;
+ var Depot, root;
root = typeof exports !== "undefined" && exports !== null ? exports : this;
- LocalStorageWrapper = (function() {
- function LocalStorageWrapper(prefix) {
+ Depot = (function() {
+ function Depot(prefix) {
if (prefix == null) {
prefix = null;
}
this.store = window.localStorage;
this.prefix = prefix;
}
- LocalStorageWrapper.prototype.get = function(key) {
+ Depot.prototype.get = function(key) {
return JSON.parse(this.store.getItem(this._buildKey(key)));
};
- LocalStorageWrapper.prototype.set = function(key, obj) {
+ Depot.prototype.set = function(key, obj) {
return this.store.setItem(this._buildKey(key), JSON.stringify(obj));
};
- LocalStorageWrapper.prototype.push = function(key, obj) {
+ Depot.prototype.push = function(key, obj) {
var val;
val = this.get(key) || [];
val.push(obj);
return this.set(key, val);
};
- LocalStorageWrapper.prototype.pop = function(key) {
+ Depot.prototype.pop = function(key) {
var poppedObj, val;
val = this.get(key) || [];
poppedObj = val.pop();
this.set(key, val);
return poppedObj;
};
- LocalStorageWrapper.prototype._changeCounter = function(key, changeFunc) {
+ Depot.prototype._changeCounter = function(key, changeFunc) {
var newVal, val;
val = this.get(key) || 0;
if (typeof val === "number") {
@@ -39,33 +39,33 @@
throw "Cannot perform counter operation on a non-number";
}
};
- LocalStorageWrapper.prototype.incr = function(key, incrBy) {
+ Depot.prototype.incr = function(key, incrBy) {
if (incrBy == null) {
incrBy = 1;
}
return this._changeCounter(key, function(val) {
return val + incrBy;
});
};
- LocalStorageWrapper.prototype.decr = function(key, decrBy) {
+ Depot.prototype.decr = function(key, decrBy) {
if (decrBy == null) {
decrBy = 1;
}
return this._changeCounter(key, function(val) {
return val - decrBy;
});
};
- LocalStorageWrapper.prototype.del = function(key) {
+ Depot.prototype.del = function(key) {
return this.store.removeItem(this._buildKey(key));
};
- LocalStorageWrapper.prototype._buildKey = function(key) {
+ Depot.prototype._buildKey = function(key) {
if (this.prefix) {
return "" + this.prefix + ":" + key;
} else {
return key;
}
};
- return LocalStorageWrapper;
+ return Depot;
})();
- root.LocalStorageWrapper = LocalStorageWrapper;
+ root.Depot = Depot;
}).call(this);
View
4 spec/SpecRunner.html
@@ -11,10 +11,10 @@
<script type="text/javascript" src="support/jasmine-1.1.0/jasmine-html.js"></script>
<!-- include source files here... -->
- <script type="text/javascript" src="../lib/localStorage.js"></script>
+ <script type="text/javascript" src="../lib/depot.js"></script>
<!-- include compiled spec files here... -->
- <script type="text/javascript" src="compiled/localStorageSpec.js"></script>
+ <script type="text/javascript" src="compiled/depotSpec.js"></script>
</head>
<body>
View
162 spec/compiled/depotSpec.js
@@ -0,0 +1,162 @@
+(function() {
+ var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
+ beforeEach(function() {
+ this.store = new Depot;
+ return window.localStorage.clear();
+ });
+ describe("Depot", function() {
+ describe("wraps window.localStorage methods", function() {
+ describe("get()", function() {
+ return it("calls through to the getItem method", function() {
+ spyOn(localStorage, 'getItem');
+ spyOn(JSON, 'parse').andReturn("foo");
+ this.store.get('key');
+ return expect(localStorage.getItem).toHaveBeenCalledWith('key');
+ });
+ });
+ describe("set()", function() {
+ return it("calls through to the setItem method", function() {
+ spyOn(localStorage, 'setItem');
+ spyOn(JSON, 'stringify').andReturn("value");
+ this.store.set('key', 'value');
+ return expect(localStorage.setItem).toHaveBeenCalledWith('key', 'value');
+ });
+ });
+ return describe("del()", function() {
+ return it("calls through to the removeItem method", function() {
+ spyOn(localStorage, 'removeItem');
+ this.store.del('key');
+ return expect(localStorage.removeItem).toHaveBeenCalledWith('key');
+ });
+ });
+ });
+ describe("converts complex types to & from JSON", function() {
+ beforeEach(function() {
+ this.nativeObj = {
+ album: "Mass Romantic",
+ band: "The New Pornographers",
+ year: 2000
+ };
+ return this.jsonObj = '{"album":"Mass Romantic","band":"The New Pornographers","year":2000}';
+ });
+ it("stores objects as JSON", function() {
+ spyOn(localStorage, 'setItem');
+ spyOn(JSON, 'stringify').andCallThrough();
+ this.store.set('album', this.nativeObj);
+ expect(JSON.stringify).toHaveBeenCalled();
+ return expect(localStorage.setItem).toHaveBeenCalledWith('album', this.jsonObj);
+ });
+ return it("gets JSON as objects", function() {
+ var response;
+ spyOn(JSON, 'parse').andCallThrough();
+ localStorage.setItem('album', this.jsonObj);
+ response = this.store.get('album');
+ expect(JSON.parse).toHaveBeenCalled();
+ return expect(response).toEqual(this.nativeObj);
+ });
+ });
+ describe("array convenience functions", function() {
+ beforeEach(function() {
+ return this.store.set('sampleArray', [1, 2, 3, 4, 5]);
+ });
+ describe("pop()", function() {
+ return it("pops the last element off the array", function() {
+ var poppedObj, remainingObj;
+ poppedObj = this.store.pop('sampleArray');
+ remainingObj = this.store.get('sampleArray');
+ expect(poppedObj).toEqual(5);
+ return expect(remainingObj).toEqual([1, 2, 3, 4]);
+ });
+ });
+ return describe("push()", function() {
+ it("appends an element to the end of an array", function() {
+ var obj;
+ this.store.push('sampleArray', 6);
+ obj = this.store.get('sampleArray');
+ return expect(obj).toEqual([1, 2, 3, 4, 5, 6]);
+ });
+ return it("creates a new array if the key is empty", function() {
+ var obj;
+ this.store.push('newArray', 6);
+ obj = this.store.get('newArray');
+ return expect(obj).toEqual([6]);
+ });
+ });
+ });
+ describe("key prefixing", function() {
+ it("prepends a string to keys if initalized with a prefix", function() {
+ this.store = new LocalStorageWrapper("prefix");
+ spyOn(localStorage, 'getItem');
+ spyOn(JSON, 'parse').andReturn("foo");
+ this.store.get('key');
+ return expect(localStorage.getItem).toHaveBeenCalledWith('prefix:key');
+ });
+ return it("does nothing if a prefix is not provided", function() {
+ spyOn(localStorage, 'getItem');
+ spyOn(JSON, 'parse').andReturn("foo");
+ this.store.get('key');
+ return expect(localStorage.getItem).toHaveBeenCalledWith('key');
+ });
+ });
+ return describe("counters", function() {
+ describe("incr()", function() {
+ it("increments a counter and returns value", function() {
+ var resp;
+ this.store.set('existingCounter', 1);
+ resp = this.store.incr('existingCounter');
+ expect(resp).toEqual(2);
+ return expect(this.store.get('existingCounter')).toEqual(2);
+ });
+ it("creates a counter if key is not defined and returns", function() {
+ var resp;
+ resp = this.store.incr('undefinedCounter');
+ expect(resp).toEqual(1);
+ return expect(this.store.get('undefinedCounter')).toEqual(1);
+ });
+ it("throws an error if a non-integer value exists for key", function() {
+ this.store.set('nonCounter', "foo");
+ expect(__bind(function() {
+ return this.store.incr('nonCounter');
+ }, this)).toThrow('Cannot perform counter operation on a non-number');
+ return expect(this.store.get('nonCounter')).toEqual("foo");
+ });
+ return it("increments by a specified value", function() {
+ var resp;
+ this.store.set('existingCounter', 1);
+ resp = this.store.incr('existingCounter', 9);
+ expect(resp).toEqual(10);
+ return expect(this.store.get('existingCounter')).toEqual(10);
+ });
+ });
+ return describe("decr()", function() {
+ it("decrement a counter and returns value", function() {
+ var resp;
+ this.store.set('existingCounter', 2);
+ resp = this.store.decr('existingCounter');
+ expect(resp).toEqual(1);
+ return expect(this.store.get('existingCounter')).toEqual(1);
+ });
+ it("creates a counter if key is not defined and returns", function() {
+ var resp;
+ resp = this.store.decr('undefinedCounter');
+ expect(resp).toEqual(-1);
+ return expect(this.store.get('undefinedCounter')).toEqual(-1);
+ });
+ it("throws an error if a non-integer value exists for key", function() {
+ this.store.set('nonCounter', "foo");
+ expect(__bind(function() {
+ return this.store.decr('nonCounter');
+ }, this)).toThrow('Cannot perform counter operation on a non-number');
+ return expect(this.store.get('nonCounter')).toEqual("foo");
+ });
+ return it("decrement by a specified value", function() {
+ var resp;
+ this.store.set('existingCounter', 10);
+ resp = this.store.decr('existingCounter', 9);
+ expect(resp).toEqual(1);
+ return expect(this.store.get('existingCounter')).toEqual(1);
+ });
+ });
+ });
+ });
+}).call(this);
View
4 spec/localStorageSpec.coffee → spec/depotSpec.coffee
@@ -1,8 +1,8 @@
beforeEach ->
- @store = new LocalStorageWrapper
+ @store = new Depot
window.localStorage.clear()
-describe "LocalStorageWrapper", ->
+describe "Depot", ->
describe "wraps window.localStorage methods", ->
describe "get()", ->
it "calls through to the getItem method", ->
View
4 src/localStorage.coffee → src/depot.coffee
@@ -1,6 +1,6 @@
root = exports ? this
-class LocalStorageWrapper
+class Depot
constructor: (prefix = null)->
@store = window.localStorage
@prefix = prefix
@@ -53,4 +53,4 @@ class LocalStorageWrapper
else
key
-root.LocalStorageWrapper = LocalStorageWrapper
+root.Depot = Depot

0 comments on commit b6f78b4

Please sign in to comment.
Something went wrong with that request. Please try again.