Skip to content

Commit

Permalink
0.10.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Download committed Dec 4, 2015
1 parent e055257 commit ddda755
Show file tree
Hide file tree
Showing 10 changed files with 205 additions and 23 deletions.
54 changes: 48 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
# memorystorage <sub><sup>v0.9.10</sup></sub>
# memorystorage <sub><sup>v0.10.0</sup></sub>
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.

### CDN
This is by far the easiest method and gives good performance to boost. Use this if you are in doubt.
```xml
<script src="https://cdn.rawgit.com/download/memorystorage/0.9.10/dist/memorystorage.min.js"></script>
<script src="https://cdn.rawgit.com/download/memorystorage/0.10.0/dist/memorystorage.min.js"></script>
```

### Local script file
Expand Down Expand Up @@ -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'
}
});
```
Expand Down Expand Up @@ -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<keys.length; i++) {
var key = keys(i);
var value = store[key];
console.info(key + ': ' + value);
}
```

### Example of staying within the API
Here is the same code, rewritten to stay within the API:
```js
for (var i=0; i<store.length; i++) {
var key = store.key(i);
var value = store.getItem(key);
console.info(key + ': ' + value);
}
```

## Beyond the Web Storage API
MemoryStorage is type-agnostic; 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,
Expand All @@ -112,6 +149,11 @@ store.setItem('tree', tree);
alert(store.tree.nested.objects.working); // alerts 'Sure!'
```

## Contributors
I'd like to draw your attention to the people that contributed to this project with bug reports,
documentation, pull requests or other forms of support.
* [Matthias Seemann](https://github.com/semmel): [Items with store API key names are considered by key() #3](https://github.com/Download/memorystorage/pull/3)

## Copyright
Copyright 2015 by Stijn de Witt. Some rights reserved.

Expand Down
2 changes: 1 addition & 1 deletion dist/memorystorage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/memorystorage.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion doc/classes.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ <h3 class="subsection-title">Classes</h3>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Tue Sep 15th 2015 using the <a
on Fri Dec 4th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ <h1 class="page-title">Index</h1>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Tue Sep 15th 2015 using the <a
on Fri Dec 4th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
37 changes: 31 additions & 6 deletions doc/memorystorage.js.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ <h1 class="page-title">Source: memorystorage.js</h1>
*/
// 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
Expand Down Expand Up @@ -108,18 +107,30 @@ <h1 class="page-title">Source: memorystorage.js</h1>
var cloaked = {};
Object.defineProperty(result, CLOAK, {
enumerable: false,
configurable: true,
get: function(){return cloaked;}
});
/**
* private method to find all enumerable keys
* @returns {Array.&lt;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
Expand All @@ -134,9 +145,13 @@ <h1 class="page-title">Source: memorystorage.js</h1>
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 &amp;&amp; idx &lt; keys.length ? keys[idx] : null;
};
result.clear = function MemoryStorage_clear() {
Expand All @@ -149,7 +164,17 @@ <h1 class="page-title">Source: memorystorage.js</h1>
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();
}
});
}
</pre>
</article>
Expand Down Expand Up @@ -180,7 +205,7 @@ <h1 class="page-title">Source: memorystorage.js</h1>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Tue Sep 15th 2015 using the <a
on Fri Dec 4th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
114 changes: 112 additions & 2 deletions doc/module-memorystorage.MemoryStorage.html
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ <h5>Parameters:</h5>
<ul class="dummy">
<li>
<a href="memorystorage.js.html">memorystorage.js</a>,
<a href="memorystorage.js.html#sunlight-1-line-32">line 32</a>
<a href="memorystorage.js.html#sunlight-1-line-31">line 31</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -224,6 +224,116 @@ <h5>Parameters:</h5>



<h3 class="subsection-title">Methods</h3>

<dl>

<dt>
<h4 class="name" id="~enumerableKeys"><span class="type-signature">&lt;inner> </span>enumerableKeys<span class="signature">()</span><span class="type-signature"> &rarr; {Array.&lt;String>}</span></h4>


</dt>
<dd>


<div class="description">
<p>private method to find all enumerable keys</p>
</div>










<dl class="details">





























<dt class="tag-source">Source:</dt>
<dd class="tag-source">
<ul class="dummy">
<li>
<a href="memorystorage.js.html">memorystorage.js</a>,
<a href="memorystorage.js.html#sunlight-1-line-56">line 56</a>
</li>
</ul>
</dd>







</dl>













<h5>Returns:</h5>




<dl>
<dt>
Type
</dt>
<dd>

<span class="param-type">Array.&lt;String></span>


</dd>
</dl>





</dd>

</dl>




Expand Down Expand Up @@ -260,7 +370,7 @@ <h5>Parameters:</h5>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Tue Sep 15th 2015 using the <a
on Fri Dec 4th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
4 changes: 2 additions & 2 deletions doc/module-memorystorage.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ <h1 class="page-title">Module: memorystorage</h1>
<ul class="dummy">
<li>
<a href="memorystorage.js.html">memorystorage.js</a>,
<a href="memorystorage.js.html#sunlight-1-line-15">line 15</a>
<a href="memorystorage.js.html#sunlight-1-line-14">line 14</a>
</li>
</ul>
</dd>
Expand Down Expand Up @@ -177,7 +177,7 @@ <h3 class="subsection-title">Classes</h3>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Tue Sep 15th 2015 using the <a
on Fri Dec 4th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
2 changes: 1 addition & 1 deletion doc/modules.list.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ <h3 class="subsection-title">Classes</h3>

<span class="jsdoc-message">
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.3.2</a>
on Tue Sep 15th 2015 using the <a
on Fri Dec 4th 2015 using the <a
href="https://github.com/terryweiss/docstrap">DocStrap template</a>.
</span>
</footer>
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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/",
Expand Down

0 comments on commit ddda755

Please sign in to comment.