diff --git a/README.md b/README.md index cc028e1..c02a358 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,19 @@ -# memorystorage v0.9.7 +# memorystorage v0.9.8 Memory-backed storage that implements the [Web Storage API](http://www.w3.org/TR/webstorage/), making it a drop-in replacement for `localStorage` and `sessionStorage` in environments where these are not available. [Project website](http://download.github.io/memorystorage) ## Download -* [memorystorage.js](https://cdn.rawgit.com/download/memorystorage/0.9.7/src/memorystorage.js) (~3kB, commented) -* [memorystorage.min.js](https://cdn.rawgit.com/download/memorystorage/0.9.7/dist/memorystorage.min.js) (~2kB, minified) -* [memorystorage.min.js.map](https://cdn.rawgit.com/download/memorystorage/0.9.7/dist/memorystorage.min.js.map) (~2kB, debug map file) +* [memorystorage.umd.js](https://cdn.rawgit.com/download/memorystorage/0.9.8/src/memorystorage.umd.js) (~4kB, commented) +* [memorystorage.min.js](https://cdn.rawgit.com/download/memorystorage/0.9.8/dist/memorystorage.min.js) (~2kB, minified) +* [memorystorage.min.js.map](https://cdn.rawgit.com/download/memorystorage/0.9.8/dist/memorystorage.min.js.map) (~2kB, debug map file) ## Include on your page -`memorystorage` can be used directly from CDN, or from a local script file. +`memorystorage` can be used directly from CDN, from a local script file, or from a module loader. ### CDN +This is by far the easiest method and gives good performance to boost. Use this if you are in doubt. ```xml - + ``` ### Local script file @@ -21,6 +22,33 @@ Download memorystorage.min.js, place it in a folder `lib` in the root of your we ``` +### Module loaders +Memorystorage implements the Universal Module Pattern and as such, is available to be consumed +from Node modules as well as via an AMD loader such as RequireJS. + +#### Node +```javascript +var MemoryStorage = require('memorystorage'); +// here, the MemoryStorage function is available +var myStorage = new MemoryStorage('my-app'); +``` + +#### AMD +```javascript +define(['memorystorage'], function(MemoryStorage){ + // here, the MemoryStorage function is available + var myStorage = new MemoryStorage('my-app'); +}); +``` +To be able to load MemoryStorage from CDN as an AMD module, configure the CDN url like so (note the absence of `.js` in the url): +```javascript +require.config({ + paths: { + 'memorystorage': 'https://cdn.rawgit.com/download/memorystorage/0.9.8/dist/memorystorage.min' + } +}); +``` + ## Create a memory storage object The `MemoryStorage` function creates (or returns) a storage object implementing the W3C Web Storage API. By default, scripts share a `global` storage object, so scripts can access and mutate each other's store diff --git a/dist/memorystorage.min.js b/dist/memorystorage.min.js index 57ec285..dfdc531 100644 --- a/dist/memorystorage.min.js +++ b/dist/memorystorage.min.js @@ -1,3 +1,3 @@ -/*! [memorystorage 0.9.7](http://download.github.io/memorystorage) Copyright 2015 by [Stijn de Witt](http://StijnDeWitt.com). Some rights reserved. License: [CC-BY-4.0](https://creativecommons.org/licenses/by/4.0/) */ -!function(u,m,d){"function"==typeof define&&define.amd?define(d):"object"==typeof exports?module.exports=d():u[m]=d()}(this,"MemoryStorage",function(){"use strict";function MemoryStorage(f){f=f||"global";var g=e[f];if(g)return g;if(!(this instanceof MemoryStorage))return new MemoryStorage(f);g=e[f]=this;var h={};return Object.defineProperty(g,c,{enumerable:!1,get:function(){return h}}),Object.defineProperty(g,"id",{enumerable:!0,get:function(){return f}}),Object.defineProperty(g,"length",{enumerable:!0,get:function(){return Object.keys(this).length+Object.keys(this[c]).length-b}}),g.getItem=function(b){return b in a?this[c][b]:this[b]},g.setItem=function(b,e){b in a?this[c][b]=e:this[b]=e},g.removeItem=function(b){b in a?delete this[c][b]:delete this[b]},g.key=function(b){var e=Object.keys(this).concat(Object.keys(this[c]));return e=e.filter(function(b){return!(b in a)}),b>=0&&b=0&&bWeb Storage API using memory. + * + *

If no arguments are given, the created memory storage object will read from and write to the + * global memory storage. If a string argument is given, the new storage object + * will read from and write to it's own segment of memory. Any data written to such a memory + * storage object will only show up in other memory storage objects that have been created with + * the same id. This data will not show up in the global memory space. As such it + * is recommended to always construct a memory storage object with a unique string id as argument.

+ * + * @param id Optional string argument used to isolate this memory storage object from others. + * + * @alias module:memorystorage.MemoryStorage + * @class + */ +function MemoryStorage(id) {// jshint ignore:line + // make sure id is assigned + id = id || 'global'; + // try to get existing store + var result = storage[id]; + // return it if found + if (result) {return result;} + + // make sure there is no harm in leaving out new in invocations to MemoryStorage + if (! (this instanceof MemoryStorage)) {return new MemoryStorage(id);} + + // create a new store and save a ref to it so we can get it back later + result = storage[id] = this; + // create a space to store 'cloaked' key/values: items that have a key + // that collides with Web Storage API method names. + var cloaked = {}; + Object.defineProperty(result, CLOAK, { + enumerable: false, + get: function(){return cloaked;} + }); + // Allow client code to read the id + Object.defineProperty(result, 'id', { + enumerable: true, + get: function(){return id;} + }); + // Create the length property + Object.defineProperty(result, 'length', { + enumerable: true, + get: function(){ + return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH; + } + }); + // Create API methods + result.getItem = function MemoryStorage_getItem(key) { + return key in API ? this[CLOAK][key] : this[key]; + }; + result.setItem = function MemoryStorage_setItem(key, val) { + if (key in API) {this[CLOAK][key] = val;} + else {this[key] = val;} + }; + result.removeItem = function MemoryStorage_removeItem(key) { + if (key in API) {delete this[CLOAK][key];} + else {delete this[key];} + }; + result.key = function MemoryStorage_key(idx) { + var keys = Object.keys(this).concat(Object.keys(this[CLOAK])); + keys = keys.filter(function(x){return !(x in API);}); + return idx >= 0 && idx < keys.length ? keys[idx] : null; + }; + result.clear = function MemoryStorage_clear() { + var keys = Object.keys(this).filter(function(x){return !(x in API);}); + for (var i=0,key; key=keys[i]; i++) { + if (! (key in API)) {delete this[key];} + } + keys = Object.keys(this[CLOAK]); + for (var i=0,key; key=keys[i]; i++) { + delete this[CLOAK][key]; + } + }; + return result; +} + + +return MemoryStorage; +})); diff --git a/doc/classes.list.html b/doc/classes.list.html index 49bbda0..79adedf 100644 --- a/doc/classes.list.html +++ b/doc/classes.list.html @@ -172,7 +172,7 @@

Classes

Documentation generated by JSDoc 3.3.2 - on Wed Sep 2nd 2015 using the DocStrap template. diff --git a/doc/index.html b/doc/index.html index 016981b..16121b1 100644 --- a/doc/index.html +++ b/doc/index.html @@ -107,7 +107,7 @@

Index

Documentation generated by JSDoc 3.3.2 - on Wed Sep 2nd 2015 using the DocStrap template. diff --git a/doc/memorystorage.js.html b/doc/memorystorage.js.html index a170e47..c31b74c 100644 --- a/doc/memorystorage.js.html +++ b/doc/memorystorage.js.html @@ -65,103 +65,92 @@

Source: memorystorage.js

* @copyright Copyright 2015 by Stijn de Witt. Some rights reserved. * @license Licensed under [Creative Commons Attribution 4.0 International (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/). */ -(function (u,m,d) { - if (typeof define === 'function' && define.amd) {define(d);} - else if (typeof exports === 'object') {module.exports = d();} - else {u[m] = d();} -}(this, 'MemoryStorage', function(){ - 'use strict'; - - // API methods and properties will be cloaked - var API = {'clear':1, 'getItem':1, 'id':1, 'key':1, 'length':1, 'removeItem':1, 'setItem':1}, - API_LENGTH = Object.keys(API).length, - CLOAK = '__memorystorage_cloaked_items__'; - - // Used to store all memorystorage objects - var storage = {}; - - /** @module memorystorage */ +// API methods and properties will be cloaked +var API = {'clear':1, 'getItem':1, 'id':1, 'key':1, 'length':1, 'removeItem':1, 'setItem':1}, + API_LENGTH = Object.keys(API).length, + CLOAK = '__memorystorage_cloaked_items__'; + +// Used to store all memorystorage objects +var storage = {}; + +/** @module memorystorage */ + +/** + * Creates a new MemoryStorage object implementing the <a href="http://www.w3.org/TR/webstorage/">Web Storage API</a> using memory. + * + * <p>If no arguments are given, the created memory storage object will read from and write to the + * <code>global</code> memory storage. If a string argument is given, the new storage object + * will read from and write to it's own segment of memory. Any data written to such a memory + * storage object will only show up in other memory storage objects that have been created with + * the same id. This data will not show up in the <code>global</code> memory space. As such it + * is recommended to always construct a memory storage object with a unique string id as argument.</p> + * + * @param id Optional string argument used to isolate this memory storage object from others. + * + * @alias module:memorystorage.MemoryStorage + * @class + */ +function MemoryStorage(id) {// jshint ignore:line + // make sure id is assigned + id = id || 'global'; + // try to get existing store + var result = storage[id]; + // return it if found + if (result) {return result;} - /** - * Creates a new MemoryStorage object implementing the <a href="http://www.w3.org/TR/webstorage/">Web Storage API</a> using memory. - * - * <p>If no arguments are given, the created memory storage object will read from and write to the - * <code>global</code> memory storage. If a string argument is given, the new storage object - * will read from and write to it's own segment of memory. Any data written to such a memory - * storage object will only show up in other memory storage objects that have been created with - * the same id. This data will not show up in the <code>global</code> memory space. As such it - * is recommended to always construct a memory storage object with a unique string id as argument.</p> - * - * @param id Optional string argument used to isolate this memory storage object from others. - * - * @alias module:memorystorage.MemoryStorage - * @class - */ - function MemoryStorage(id) { - // make sure id is assigned - id = id || 'global'; - // try to get existing store - var result = storage[id]; - // return it if found - if (result) {return result;} - - // make sure there is no harm in leaving out new in invocations to MemoryStorage - if (! (this instanceof MemoryStorage)) {return new MemoryStorage(id);} - - // create a new store and save a ref to it so we can get it back later - result = storage[id] = this; - // create a space to store 'cloaked' key/values: items that have a key - // that collides with Web Storage API method names. - var cloaked = {}; - Object.defineProperty(result, CLOAK, { - enumerable: false, - get: function(){return cloaked;} - }); - // Allow client code to read the id - Object.defineProperty(result, 'id', { - enumerable: true, - get: function(){return id;} - }); - // Create the length property - Object.defineProperty(result, 'length', { - enumerable: true, - get: function(){ - return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH; - } - }); - // Create API methods - result.getItem = function MemoryStorage_getItem(key) { - return key in API ? this[CLOAK][key] : this[key]; - }; - result.setItem = function MemoryStorage_setItem(key, val) { - if (key in API) {this[CLOAK][key] = val;} - else {this[key] = val;} - }; - result.removeItem = function MemoryStorage_removeItem(key) { - if (key in API) {delete this[CLOAK][key];} - else {delete this[key];} - }; - result.key = function MemoryStorage_key(idx) { - var keys = Object.keys(this).concat(Object.keys(this[CLOAK])); - keys = keys.filter(function(x){return !(x in API);}); - return idx >= 0 && idx < keys.length ? keys[idx] : null; - }; - result.clear = function MemoryStorage_clear() { - var keys = Object.keys(this).filter(function(x){return !(x in API);}); - for (var i=0,key; key=keys[i]; i++) { - if (! (key in API)) {delete this[key];} - } - keys = Object.keys(this[CLOAK]); - for (var i=0,key; key=keys[i]; i++) { - delete this[CLOAK][key]; - } - }; - return result; - } + // make sure there is no harm in leaving out new in invocations to MemoryStorage + if (! (this instanceof MemoryStorage)) {return new MemoryStorage(id);} - // EXPOSE - return MemoryStorage; -})); + // create a new store and save a ref to it so we can get it back later + result = storage[id] = this; + // create a space to store 'cloaked' key/values: items that have a key + // that collides with Web Storage API method names. + var cloaked = {}; + Object.defineProperty(result, CLOAK, { + enumerable: false, + get: function(){return cloaked;} + }); + // Allow client code to read the id + Object.defineProperty(result, 'id', { + enumerable: true, + get: function(){return id;} + }); + // Create the length property + Object.defineProperty(result, 'length', { + enumerable: true, + get: function(){ + return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH; + } + }); + // Create API methods + result.getItem = function MemoryStorage_getItem(key) { + return key in API ? this[CLOAK][key] : this[key]; + }; + result.setItem = function MemoryStorage_setItem(key, val) { + if (key in API) {this[CLOAK][key] = val;} + else {this[key] = val;} + }; + result.removeItem = function MemoryStorage_removeItem(key) { + if (key in API) {delete this[CLOAK][key];} + else {delete this[key];} + }; + result.key = function MemoryStorage_key(idx) { + var keys = Object.keys(this).concat(Object.keys(this[CLOAK])); + keys = keys.filter(function(x){return !(x in API);}); + return idx >= 0 && idx < keys.length ? keys[idx] : null; + }; + result.clear = function MemoryStorage_clear() { + var keys = Object.keys(this).filter(function(x){return !(x in API);}); + for (var i=0,key; key=keys[i]; i++) { + if (! (key in API)) {delete this[key];} + } + keys = Object.keys(this[CLOAK]); + for (var i=0,key; key=keys[i]; i++) { + delete this[CLOAK][key]; + } + }; + return result; +} @@ -191,7 +180,7 @@

Source: memorystorage.js

Documentation generated by JSDoc 3.3.2 - on Wed Sep 2nd 2015 using the DocStrap template. diff --git a/doc/module-memorystorage.MemoryStorage.html b/doc/module-memorystorage.MemoryStorage.html index 3b238af..3b44dc6 100644 --- a/doc/module-memorystorage.MemoryStorage.html +++ b/doc/module-memorystorage.MemoryStorage.html @@ -179,7 +179,7 @@
Parameters:
@@ -260,7 +260,7 @@
Parameters:
Documentation generated by JSDoc 3.3.2 - on Wed Sep 2nd 2015 using the DocStrap template. diff --git a/doc/module-memorystorage.html b/doc/module-memorystorage.html index fdf337b..9199a3d 100644 --- a/doc/module-memorystorage.html +++ b/doc/module-memorystorage.html @@ -103,7 +103,7 @@

Module: memorystorage

@@ -177,7 +177,7 @@

Classes

Documentation generated by JSDoc 3.3.2 - on Wed Sep 2nd 2015 using the DocStrap template. diff --git a/doc/modules.list.html b/doc/modules.list.html index 324b589..274ff79 100644 --- a/doc/modules.list.html +++ b/doc/modules.list.html @@ -172,7 +172,7 @@

Classes

Documentation generated by JSDoc 3.3.2 - on Wed Sep 2nd 2015 using the DocStrap template. diff --git a/gruntfile.js b/gruntfile.js index 816d8b5..1ca078a 100644 --- a/gruntfile.js +++ b/gruntfile.js @@ -10,6 +10,17 @@ module.exports = function(grunt) { }, all: [ '<%= pkg.main %>' ] }, + umd: { + all: { + options: { + src: '<%= pkg.main %>', + dest: '<%= pkg.dist.umd %>', + template: 'umd-lite.hbs', + objectToExport: '<%= pkg.exports[0] %>', + amdModuleId: '<%= pkg.name %>' + } + } + }, uglify: { options:{ banner : '/*! [<%= pkg.name %> <%= pkg.version %>](<%= pkg.homepage %>) <%= pkg.copyright %> License: [<%= pkg.license %>](<%= pkg.licenseUrl %>) */', @@ -20,7 +31,7 @@ module.exports = function(grunt) { }, admin: { files: { - '<%= pkg.dist.min %>': ['<%= pkg.main %>'] + '<%= pkg.dist.min %>': ['<%= pkg.dist.umd %>'] } } }, @@ -38,6 +49,7 @@ module.exports = function(grunt) { grunt.registerTask('default', [ 'jshint', + 'umd', 'uglify', 'jsdoc', ]); diff --git a/package.json b/package.json index 521092f..9cc48d3 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,16 @@ { "name": "memorystorage", - "version": "0.9.7", + "version": "0.9.8", "description": "Memory-backed implementation of the Web Storage API", "main": "src/memorystorage.js", "dist": { + "umd": "dist/memorystorage.umd.js", "min": "dist/memorystorage.min.js", "map": "dist/memorystorage.min.js.map" }, - "exports": ["MemoryStorage"], + "exports": [ + "MemoryStorage" + ], "directories": { "test": "tests" }, @@ -37,11 +40,11 @@ "homepage": "http://download.github.io/memorystorage", "devDependencies": { "grunt": "~0.4.5", - "load-grunt-tasks": "~1.0.0", "grunt-contrib-jshint": "~0.10.0", "grunt-contrib-uglify": "~0.6.0", - "grunt-jsdoc": "~0.6.7" + "grunt-jsdoc": "~0.6.7", + "grunt-umd": "^2.3.3", + "load-grunt-tasks": "~1.0.0" }, - "dependencies": { - } + "dependencies": {} } diff --git a/src/memorystorage.js b/src/memorystorage.js index 070e981..13a044f 100644 --- a/src/memorystorage.js +++ b/src/memorystorage.js @@ -4,100 +4,89 @@ * @copyright Copyright 2015 by Stijn de Witt. Some rights reserved. * @license Licensed under [Creative Commons Attribution 4.0 International (CC BY 4.0)](https://creativecommons.org/licenses/by/4.0/). */ -(function (u,m,d) { - if (typeof define === 'function' && define.amd) {define(d);} - else if (typeof exports === 'object') {module.exports = d();} - else {u[m] = d();} -}(this, 'MemoryStorage', function(){ - 'use strict'; +// API methods and properties will be cloaked +var API = {'clear':1, 'getItem':1, 'id':1, 'key':1, 'length':1, 'removeItem':1, 'setItem':1}, + API_LENGTH = Object.keys(API).length, + CLOAK = '__memorystorage_cloaked_items__'; - // API methods and properties will be cloaked - var API = {'clear':1, 'getItem':1, 'id':1, 'key':1, 'length':1, 'removeItem':1, 'setItem':1}, - API_LENGTH = Object.keys(API).length, - CLOAK = '__memorystorage_cloaked_items__'; +// Used to store all memorystorage objects +var storage = {}; - // Used to store all memorystorage objects - var storage = {}; +/** @module memorystorage */ - /** @module memorystorage */ +/** + * Creates a new MemoryStorage object implementing the Web Storage API using memory. + * + *

If no arguments are given, the created memory storage object will read from and write to the + * global memory storage. If a string argument is given, the new storage object + * will read from and write to it's own segment of memory. Any data written to such a memory + * storage object will only show up in other memory storage objects that have been created with + * the same id. This data will not show up in the global memory space. As such it + * is recommended to always construct a memory storage object with a unique string id as argument.

+ * + * @param id Optional string argument used to isolate this memory storage object from others. + * + * @alias module:memorystorage.MemoryStorage + * @class + */ +function MemoryStorage(id) {// jshint ignore:line + // make sure id is assigned + id = id || 'global'; + // try to get existing store + var result = storage[id]; + // return it if found + if (result) {return result;} - /** - * Creates a new MemoryStorage object implementing the Web Storage API using memory. - * - *

If no arguments are given, the created memory storage object will read from and write to the - * global memory storage. If a string argument is given, the new storage object - * will read from and write to it's own segment of memory. Any data written to such a memory - * storage object will only show up in other memory storage objects that have been created with - * the same id. This data will not show up in the global memory space. As such it - * is recommended to always construct a memory storage object with a unique string id as argument.

- * - * @param id Optional string argument used to isolate this memory storage object from others. - * - * @alias module:memorystorage.MemoryStorage - * @class - */ - function MemoryStorage(id) { - // make sure id is assigned - id = id || 'global'; - // try to get existing store - var result = storage[id]; - // return it if found - if (result) {return result;} - - // make sure there is no harm in leaving out new in invocations to MemoryStorage - if (! (this instanceof MemoryStorage)) {return new MemoryStorage(id);} - - // create a new store and save a ref to it so we can get it back later - result = storage[id] = this; - // create a space to store 'cloaked' key/values: items that have a key - // that collides with Web Storage API method names. - var cloaked = {}; - Object.defineProperty(result, CLOAK, { - enumerable: false, - get: function(){return cloaked;} - }); - // Allow client code to read the id - Object.defineProperty(result, 'id', { - enumerable: true, - get: function(){return id;} - }); - // Create the length property - Object.defineProperty(result, 'length', { - enumerable: true, - get: function(){ - return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH; - } - }); - // Create API methods - result.getItem = function MemoryStorage_getItem(key) { - return key in API ? this[CLOAK][key] : this[key]; - }; - result.setItem = function MemoryStorage_setItem(key, val) { - if (key in API) {this[CLOAK][key] = val;} - else {this[key] = val;} - }; - result.removeItem = function MemoryStorage_removeItem(key) { - if (key in API) {delete this[CLOAK][key];} - else {delete this[key];} - }; - result.key = function MemoryStorage_key(idx) { - var keys = Object.keys(this).concat(Object.keys(this[CLOAK])); - keys = keys.filter(function(x){return !(x in API);}); - return idx >= 0 && idx < keys.length ? keys[idx] : null; - }; - result.clear = function MemoryStorage_clear() { - var keys = Object.keys(this).filter(function(x){return !(x in API);}); - for (var i=0,key; key=keys[i]; i++) { - if (! (key in API)) {delete this[key];} - } - keys = Object.keys(this[CLOAK]); - for (var i=0,key; key=keys[i]; i++) { - delete this[CLOAK][key]; - } - }; - return result; - } + // make sure there is no harm in leaving out new in invocations to MemoryStorage + if (! (this instanceof MemoryStorage)) {return new MemoryStorage(id);} - // EXPOSE - return MemoryStorage; -})); + // create a new store and save a ref to it so we can get it back later + result = storage[id] = this; + // create a space to store 'cloaked' key/values: items that have a key + // that collides with Web Storage API method names. + var cloaked = {}; + Object.defineProperty(result, CLOAK, { + enumerable: false, + get: function(){return cloaked;} + }); + // Allow client code to read the id + Object.defineProperty(result, 'id', { + enumerable: true, + get: function(){return id;} + }); + // Create the length property + Object.defineProperty(result, 'length', { + enumerable: true, + get: function(){ + return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH; + } + }); + // Create API methods + result.getItem = function MemoryStorage_getItem(key) { + return key in API ? this[CLOAK][key] : this[key]; + }; + result.setItem = function MemoryStorage_setItem(key, val) { + if (key in API) {this[CLOAK][key] = val;} + else {this[key] = val;} + }; + result.removeItem = function MemoryStorage_removeItem(key) { + if (key in API) {delete this[CLOAK][key];} + else {delete this[key];} + }; + result.key = function MemoryStorage_key(idx) { + var keys = Object.keys(this).concat(Object.keys(this[CLOAK])); + keys = keys.filter(function(x){return !(x in API);}); + return idx >= 0 && idx < keys.length ? keys[idx] : null; + }; + result.clear = function MemoryStorage_clear() { + var keys = Object.keys(this).filter(function(x){return !(x in API);}); + for (var i=0,key; key=keys[i]; i++) { + if (! (key in API)) {delete this[key];} + } + keys = Object.keys(this[CLOAK]); + for (var i=0,key; key=keys[i]; i++) { + delete this[CLOAK][key]; + } + }; + return result; +} diff --git a/tests/index.html b/tests/index.html index 5f21dea..92b52ee 100644 --- a/tests/index.html +++ b/tests/index.html @@ -2,10 +2,10 @@ MemoryStorage Tests - + - Restart + Restart | Next Test

MemoryStorage Tests

Test code diff --git a/tests/test-amd.html b/tests/test-amd.html new file mode 100644 index 0000000..625302e --- /dev/null +++ b/tests/test-amd.html @@ -0,0 +1,18 @@ + + + + MemoryStorage as AMD module + + + Restart +

MemoryStorage as AMD module

+ Test code + + +
+
+ + + + diff --git a/tests/test-amd.js b/tests/test-amd.js new file mode 100644 index 0000000..02835b9 --- /dev/null +++ b/tests/test-amd.js @@ -0,0 +1,112 @@ +require.config({ + baseUrl: '../dist', + paths: { + 'memorystorage': './memorystorage.min', + } +}); +define(['memorystorage'], function(MemoryStorage){ + QUnit.test("AMD Module Compliance Test", function( assert ) { + assert.ok(MemoryStorage !== undefined, 'MemoryStorage is defined'); + assert.ok(typeof MemoryStorage === 'function', 'MemoryStorage is a function'); + assert.ok(window.MemoryStorage === undefined, 'global MemoryStorage is NOT defined'); + }); + + QUnit.test("W3C Web Storage API Compliance Test", function( assert ) { + var store = new MemoryStorage('local'); + + store.clear(); + assert.ok(store.length===0, "store cleared"); + store.setItem('test0', 'data0'); + assert.ok(store.length===1, "first item added to store"); + assert.ok(store.key(0)==='test0', "key registered"); + assert.ok(store.key(99)===null, "key() should return null when index out of bounds") + assert.ok(store.getItem('test0')==='data0', "value retrieved with getItem matches stored value"); + assert.ok(store['test0']==='data0', "value retrieved with index operators matches stored value"); + store['test0'] = 'changed'; + assert.ok(store['test0']==='changed', "value updated correctly with index operators."); + store['test1'] = 'data1'; + assert.ok(store.length===2, 'value added correctly with index operators'); + store.setItem('test2', 'data2'); + assert.ok(store.length===3, 'three items added to store'); + assert.ok(Object.keys(store).length == (7+3), "store has 10 enumerable properties (id, 6 api methods + 3 stored items)"); + assert.ok(store.getItem('test1')==='data1' && store.getItem('test2')==='data2', "retrieved values matches stored values"); + var keyOrderBefore = ''; + for (var i=0; i