Permalink
Browse files

minor tweaks

  • Loading branch information...
1 parent 6610810 commit 13742863256478d0004e16c4431bee98f338b675 Andris Reinman committed Aug 23, 2012
Showing with 44 additions and 30 deletions.
  1. +4 −7 README.md
  2. +26 −10 jstorage.js
  3. +13 −12 jstorage.min.js
  4. +1 −1 test.html
View
@@ -1,15 +1,12 @@
# jStorage
-jStorage is a simple wrapper plugin for Prototype, MooTools and jQuery to store data on
-browser side and to replace the use of cookies for storing data.
+**jStorage** is a cross-browser key-value store database to store data locally in the browser - jStorage supports all major browsers, both in **desktop** (yes - even Internet Explorer 6) and in **mobile**.
-jStorage is able to save almost any kind of data (strings, numbers, objects, even XML nodes)
-only object methods and functions are left out.
+Additionally jStorage is library agnostic, it works well with any other JavaScript library on the same webpage, be it jQuery, Prototype, MooTools or something else. Though you still need to have either a third party library (Prototype, MooTools) or [JSON2](https://github.com/douglascrockford/JSON-js/blob/master/json2.js) on the page to support older IE versions.
-jStorage makes use of HTML5 local storage where available and userData behavior in
-Internet Explorer older versions. Webkit SQLite is not supported.
+jStorage supports storing Strings, Numbers, JavaScript objects, Arrays and even native XML nodes which kind of makes it a JSON storage. jStorage also supports setting TTL values for auto expiring stored keys and - best of all - notifying other tabs/windows when a key has been changed, which makes jStorage also a local PubSub platform for web applications.
-jStorage is really small, just 2 kB when minified!
+jStorage is pretty small, about 8kB when minified.
## Donate
View
@@ -27,7 +27,7 @@
(function(){
var
/* jStorage version */
- JSTORAGE_VERSION = "0.2.0",
+ JSTORAGE_VERSION = "0.2.1",
/* detect a dollar object or create one if not found */
$ = window.jQuery || window.$ || (window.$ = {}),
@@ -37,6 +37,7 @@
parse:
window.JSON && (window.JSON.parse || window.JSON.decode) ||
String.prototype.evalJSON && function(str){return String(str).evalJSON();} ||
+ $.parseJSON ||
$.evalJSON,
stringify:
window.JSON && (window.JSON.stringify || window.JSON.encode) ||
@@ -311,12 +312,11 @@
function _checkUpdatedKeys(){
var oldCrc32List = JSON.parse(JSON.stringify(_storage.__jstorage_meta.CRC32)),
newCrc32List;
+
_reloadData();
newCrc32List = JSON.parse(JSON.stringify(_storage.__jstorage_meta.CRC32));
var key,
- crc32,
- added = [],
updated = [],
removed = [];
@@ -519,9 +519,14 @@
options = options || {};
+ // undefined values are deleted automatically
+ if(typeof value == "undefined"){
+ this.deleteKey(key);
+ return value;
+ }
+
if(_XMLService.isXML(value)){
value = {_is_xml:true,xml:_XMLService.encode(value)};
-
}else if(typeof value == "function"){
return undefined; // functions can't be saved!
}else if(value && typeof value == "object"){
@@ -532,12 +537,7 @@
_storage.__jstorage_meta.CRC32[key] = _crc32(JSON.stringify(value));
- if(!isNaN(options.TTL)){
- this.setTTL(key, options.TTL);
- // also handles saving
- }else{
- _save();
- }
+ this.setTTL(key, options.TTL || 0); // also handles saving
_publishChange();
_fireObservers(key, "updated");
@@ -624,6 +624,22 @@
},
/**
+ * Gets remaining TTL (in milliseconds) for a key or 0 when no TTL has been set
+ *
+ * @param {String} key Key to check
+ * @return {Number} Remaining TTL in milliseconds
+ */
+ getTTL: function(key){
+ var curtime = +new Date(), ttl;
+ _checkKey(key);
+ if(key in _storage && _storage.__jstorage_meta.TTL && _storage.__jstorage_meta.TTL[key]){
+ ttl = _storage.__jstorage_meta.TTL[key] - curtime;
+ return ttl || 0;
+ }
+ return 0;
+ },
+
+ /**
* Deletes everything in cache.
*
* @return {Boolean} Always true
Oops, something went wrong.

0 comments on commit 1374286

Please sign in to comment.