diff --git a/README.md b/README.md index d669520..d4144f2 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,10 @@ var memoryStorage = new MemoryStorage('my-app'); ## Use it ```javascript -memoryStorage.setItem('My string', 'Hello MemoryStorage!'); -memoryStorage.setItem('My object', JSON.stringify({my: 'object'})); +memoryStorage.setItem('myString', 'Hello MemoryStorage!'); +memoryStorage.myObject = JSON.stringify({my: 'object'})); alert(memoryStorage.getItem('My string')); // alerts 'Hello MemoryStorage!' +alert(memoryStorage['My string']); // alerts 'Hello MemoryStorage!' alert(memoryStorage.length); // alerts '2' alert(memoryStorage.key(1)); // alerts 'My object' memoryStorage.removeItem('My string'); @@ -39,4 +40,22 @@ memoryStorage.clear(); alert(memoryStorage.length); // alerts '0' ``` +## Beyond the Web Storage API +MemoryStorage is type-agnosic; it doesn't care about the type of data you store. +If you want to remain within the Web Storage API, you should only read and write strings, +however if you want you can store other types just as well: +```javascript +memoryStorage.myObject = {my: 'object'}; +alert(memoryStorage.myObject.my); // alerts 'object' +var tree = { + nested: { + objects: { + working: 'Sure!' + } + } +} +memoryStorage.setItem('tree', tree); +alert(memoryStorage.tree.nested.objects.working); // alerts 'Sure!' +``` + diff --git a/tests/index.html b/tests/index.html index a7ddede..ba95c03 100644 --- a/tests/index.html +++ b/tests/index.html @@ -7,9 +7,8 @@ Restart

MemoryStorage Tests

-

W3C Web Storage API Compliance Test

Test code - +
diff --git a/tests/test.js b/tests/test.js index f442bdf..1f83b22 100644 --- a/tests/test.js +++ b/tests/test.js @@ -56,7 +56,34 @@ QUnit.test("W3C Web Storage API Compliance Test", function( assert ) { assert.ok(store.getItem('test0')===undefined, "no values in cleared store"); }); +QUnit.test("Multiple Instances Test", function( assert ) { + var store1 = new MemoryStorage('local'); + var store2 = new MemoryStorage('local'); + store1.clear(); + store1.setItem('test0', 'data0'); + assert.ok(store2.getItem('test0') === store1.getItem('test0'), "Item added to store1 is also visible in store2"); + store1['test0'] = 'changed'; + assert.ok((store2.getItem('test0') === store1.getItem('test0')) && store1.getItem('test0') === 'changed', "Item changed in store1 is also changed in store2"); + store1['test1'] = 'data1'; + assert.ok(store2.length === store1.length, "Store lengths remain consistent"); + for (var i=0; i