diff --git a/README.md b/README.md index e462b2a..f62628a 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,11 @@ -# memorystorage v0.9.10 +# memorystorage v0.10.0 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.umd.js](https://cdn.rawgit.com/download/memorystorage/0.9.10/dist/memorystorage.umd.js) (~4kB, commented) -* [memorystorage.min.js](https://cdn.rawgit.com/download/memorystorage/0.9.10/dist/memorystorage.min.js) (~2kB, minified) -* [memorystorage.min.js.map](https://cdn.rawgit.com/download/memorystorage/0.9.10/dist/memorystorage.min.js.map) (~2kB, debug map file) +* [memorystorage.umd.js](https://cdn.rawgit.com/download/memorystorage/0.10.0/dist/memorystorage.umd.js) (~4kB, commented) +* [memorystorage.min.js](https://cdn.rawgit.com/download/memorystorage/0.10.0/dist/memorystorage.min.js) (~2kB, minified) +* [memorystorage.min.js.map](https://cdn.rawgit.com/download/memorystorage/0.10.0/dist/memorystorage.min.js.map) (~2kB, debug map file) ## Include on your page `memorystorage` can be used directly from CDN, from a local script file, or from a module loader. @@ -13,7 +13,7 @@ Memory-backed storage that implements the [Web Storage API](http://www.w3.org/TR ### 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 @@ -44,7 +44,7 @@ To be able to load MemoryStorage from CDN as an AMD module, configure the CDN ur ```javascript require.config({ paths: { - 'memorystorage': 'https://cdn.rawgit.com/download/memorystorage/0.9.10/dist/memorystorage.min' + 'memorystorage': 'https://cdn.rawgit.com/download/memorystorage/0.10.0/dist/memorystorage.min' } }); ``` @@ -94,6 +94,43 @@ store.clear(); alert(store.length); // alerts '0' ``` +## Staying within the Web Storage API +The Web Storage API is pretty small. For discovering which key-value pairs are available within +the storage object, you basically only have the `length` property and the `key(idx)` function. +The same applies to reading, writing and removing keys. You have the functions `getItem`, `setItem` +and `removeItem` and there is `clear` but that pretty much sums it up. + +In practice there are many other ways to interact with storage objects, such as `store[myKey] = myValue`, +or `delete store[myKey]` or `Object.keys(store)` etc, but please remember that when you use these +constructs, you venture outside the interface provided by the Web Storage API and run the risk of +incompatibility. + +This project is committed to be as compatible as possible with the `localStorage` object present in +real-life browsers, but due to inherent limitations to the Javascript language, it's impossible to +guarantee the same behavior in all instances if you go beyond the Web Storage API. + +### Example of going outside of the API +Here is some code to print all the keys and values in the `store` object that does not limit itself +to the Web Storage API: +```js +var keys = Object.keys(store); +for (var i=0; i=0&&aClasses Documentation generated by JSDoc 3.3.2 - on Tue Sep 15th 2015 using the DocStrap template. diff --git a/doc/index.html b/doc/index.html index de55e01..50861d3 100644 --- a/doc/index.html +++ b/doc/index.html @@ -107,7 +107,7 @@

Index

Documentation generated by JSDoc 3.3.2 - on Tue Sep 15th 2015 using the DocStrap template. diff --git a/doc/memorystorage.js.html b/doc/memorystorage.js.html index 0a8b53c..f4546c9 100644 --- a/doc/memorystorage.js.html +++ b/doc/memorystorage.js.html @@ -67,7 +67,6 @@

Source: memorystorage.js

*/ // 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 @@ -108,18 +107,30 @@

Source: memorystorage.js

var cloaked = {}; Object.defineProperty(result, CLOAK, { enumerable: false, + configurable: true, get: function(){return cloaked;} }); + /** + * private method to find all enumerable keys + * @returns {Array.<String>} + */ + function enumerableKeys(){ + var keys = Object.keys(result).filter(function(x){return !(x in API);}); + return keys.concat(Object.keys(cloaked)); + } + // Allow client code to read the id Object.defineProperty(result, 'id', { enumerable: true, + configurable: true, get: function(){return id;} }); // Create the length property Object.defineProperty(result, 'length', { enumerable: true, + configurable: true, get: function(){ - return Object.keys(this).length + Object.keys(this[CLOAK]).length - API_LENGTH; + return enumerableKeys().length; } }); // Create API methods @@ -134,9 +145,13 @@

Source: memorystorage.js

if (key in API) {delete this[CLOAK][key];} else {delete this[key];} }; + /** + * Needed to enumerate over all items in the collection + * @param {Number} idx - the index + * @returns {null|string} - the name of the nth key in the storage + */ 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);}); + var keys = enumerableKeys(); return idx >= 0 && idx < keys.length ? keys[idx] : null; }; result.clear = function MemoryStorage_clear() { @@ -149,7 +164,17 @@

Source: memorystorage.js

delete this[CLOAK][key]; } }; - return result; + + if (typeof Proxy === 'undefined') + { + return result; + } + // ES6 Proxy to support Object.keys() on a MemoryStorage object + return new Proxy(result, { + ownKeys: function() { + return enumerableKeys(); + } + }); } @@ -180,7 +205,7 @@

Source: memorystorage.js

Documentation generated by JSDoc 3.3.2 - on Tue Sep 15th 2015 using the DocStrap template. diff --git a/doc/module-memorystorage.MemoryStorage.html b/doc/module-memorystorage.MemoryStorage.html index 27cbada..1759064 100644 --- a/doc/module-memorystorage.MemoryStorage.html +++ b/doc/module-memorystorage.MemoryStorage.html @@ -179,7 +179,7 @@
Parameters:
@@ -224,6 +224,116 @@
Parameters:
+

Methods

+ +
+ +
+

<inner> enumerableKeys() → {Array.<String>}

+ + +
+
+ + +
+

private method to find all enumerable keys

+
+ + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Source:
+
+ +
+ + + + + + + +
+ + + + + + + + + + + + + +
Returns:
+ + + + +
+
+ Type +
+
+ +Array.<String> + + +
+
+ + + + + +
+ +
+ @@ -260,7 +370,7 @@
Parameters:
Documentation generated by JSDoc 3.3.2 - on Tue Sep 15th 2015 using the DocStrap template. diff --git a/doc/module-memorystorage.html b/doc/module-memorystorage.html index 145d4e3..7355d63 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 Tue Sep 15th 2015 using the DocStrap template. diff --git a/doc/modules.list.html b/doc/modules.list.html index 0d8d729..89c2757 100644 --- a/doc/modules.list.html +++ b/doc/modules.list.html @@ -172,7 +172,7 @@

Classes

Documentation generated by JSDoc 3.3.2 - on Tue Sep 15th 2015 using the DocStrap template. diff --git a/package.json b/package.json index 6fdefc7..2212ea7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "memorystorage", - "version": "0.9.12", + "version": "0.10.0", "description": "Memory-backed implementation of the Web Storage API", "src": "src/memorystorage.js", "main": "dist/memorystorage.umd.js", @@ -31,7 +31,12 @@ "email": "StijnDeWitt@hotmail.com", "url": "http://StijnDeWitt.com" }, - "contributors": [], + "contributors": [ + { + "name": "Matthias Seemann", + "url": "https://github.com/semmel" + } + ], "copyright": "Copyright 2015 by [Stijn de Witt](http://StijnDeWitt.com). Some rights reserved.", "license": "CC-BY-4.0", "licenseUrl": "https://creativecommons.org/licenses/by/4.0/",