diff --git a/dist/angular-local-storage.js b/dist/angular-local-storage.js index 755b378..7bfc060 100644 --- a/dist/angular-local-storage.js +++ b/dist/angular-local-storage.js @@ -1,6 +1,6 @@ /** * An Angular module that gives you access to the browsers local storage - * @version v0.1.3 - 2014-10-14 + * @version v0.1.3 - 2014-10-22 * @link https://github.com/grevory/angular-local-storage * @author grevory * @license MIT License, http://www.opensource.org/licenses/MIT @@ -14,6 +14,7 @@ var isDefined = angular.isDefined, isNumber = angular.isNumber, isObject = angular.isObject, isArray = angular.isArray, + isBoolean = isBoolean, extend = angular.extend, toJson = angular.toJson, fromJson = angular.fromJson; @@ -25,6 +26,10 @@ function isStringNumber(num) { return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, '')); } + +function isBoolean(value) { + return typeof value === 'boolean'; +} var angularLocalStorage = angular.module('LocalStorageModule', []); angularLocalStorage.provider('localStorageService', function() { @@ -138,13 +143,22 @@ angularLocalStorage.provider('localStorageService', function() { // Directly adds a value to local storage // If local storage is not available in the browser use cookies // Example use: localStorageService.add('library','angular'); - var addToLocalStorage = function (key, value) { + var addToLocalStorage = function (key, value, compareDateExpiration) { + var lsValue = {}; // Let's convert undefined values to null to get the value consistent if (isUndefined(value)) { - value = null; + value = null; } else if (isObject(value) || isArray(value) || isNumber(+value || value)) { - value = toJson(value); + + value = toJson(value); + } else if(isBoolean(value)){ + value = value.toString(); } + + lsValue = { + "date": compareDateExpiration || Date.now(), + "data": value + }; // If this browser does not support local storage use cookies if (!browserSupportsLocalStorage || self.storageType === 'cookie') { @@ -160,11 +174,15 @@ angularLocalStorage.provider('localStorageService', function() { try { if (isObject(value) || isArray(value)) { - value = toJson(value); + value = toJson(value); + lsValue = { + date: compareDateExpiration || Date.now(), + data: value + }; } - if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)}; + if (webStorage) { webStorage.setItem(deriveQualifiedKey(key), JSON.stringify(lsValue)) }; if (notify.setItem) { - $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType}); + $rootScope.$broadcast('LocalStorageModule.notification.setitem', { key: key, newvalue: JSON.stringify(lsValue), storageType: self.storageType }); } } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error', e.message); @@ -175,7 +193,9 @@ angularLocalStorage.provider('localStorageService', function() { // Directly get a value from local storage // Example use: localStorageService.get('library'); // returns 'angular' - var getFromLocalStorage = function (key) { + var getFromLocalStorage = function (key, expiration) { + + var data, date, saved; if (!browserSupportsLocalStorage || self.storageType === 'cookie') { if (!browserSupportsLocalStorage) { @@ -184,7 +204,7 @@ angularLocalStorage.provider('localStorageService', function() { return getFromCookies(key); } - + var item = webStorage ? webStorage.getItem(deriveQualifiedKey(key)) : null; // angular.toJson will convert null to 'null', so a proper conversion is needed // FIXME not a perfect solution, since a valid 'null' string can't be stored @@ -192,11 +212,22 @@ angularLocalStorage.provider('localStorageService', function() { return null; } - if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) { - return fromJson(item); + item = JSON.parse(item); + data = item.data; + saved = item.date; + + if (expiration) { + var dateExpiration = new Date(saved).getTime() + expiration; + if (dateExpiration < Date.now()) { + return null; + } + } + + if (data.charAt(0) === "{" || data.charAt(0) === "[" || isStringNumber(data)) { + return fromJson(data); } - return item; + return data; }; // Remove an item from local storage @@ -407,7 +438,7 @@ angularLocalStorage.provider('localStorageService', function() { $parse(key).assign(scope, value); return scope.$watch(key, function(newVal) { - addToLocalStorage(lsKey, newVal); + addToLocalStorage(lsKey, newVal); }, isObject(scope[key])); }; diff --git a/dist/angular-local-storage.min.js b/dist/angular-local-storage.min.js index 491d469..2c4029c 100644 --- a/dist/angular-local-storage.min.js +++ b/dist/angular-local-storage.min.js @@ -1,7 +1,7 @@ /** * An Angular module that gives you access to the browsers local storage - * @version v0.1.3 - 2014-10-14 + * @version v0.1.3 - 2014-10-22 * @link https://github.com/grevory/angular-local-storage * @author grevory * @license MIT License, http://www.opensource.org/licenses/MIT - */!function(a,b){"use strict";function c(a){return/^-?\d+\.?\d*$/.test(a.replace(/["']/g,""))}var d=b.isDefined,e=b.isUndefined,f=b.isNumber,g=b.isObject,h=b.isArray,i=b.extend,j=b.toJson,k=b.fromJson,l=b.module("LocalStorageModule",[]);l.provider("localStorageService",function(){this.prefix="ls",this.storageType="localStorage",this.cookie={expiry:30,path:"/"},this.notify={setItem:!0,removeItem:!1},this.setPrefix=function(a){this.prefix=a},this.setStorageType=function(a){this.storageType=a},this.setStorageCookie=function(a,b){this.cookie={expiry:a,path:b}},this.setStorageCookieDomain=function(a){this.cookie.domain=a},this.setNotify=function(a,b){this.notify={setItem:a,removeItem:b}},this.$get=["$rootScope","$window","$document","$parse",function(a,b,l,m){var n,o=this,p=o.prefix,q=o.cookie,r=o.notify,s=o.storageType;l?l[0]&&(l=l[0]):l=document,"."!==p.substr(-1)&&(p=p?p+".":"");var t=function(a){return p+a},u=function(){try{var c=s in b&&null!==b[s],d=t("__"+Math.round(1e7*Math.random()));return c&&(n=b[s],n.setItem(d,""),n.removeItem(d)),c}catch(e){return s="cookie",a.$broadcast("LocalStorageModule.notification.error",e.message),!1}}(),v=function(b,c){if(e(c)?c=null:(g(c)||h(c)||f(+c||c))&&(c=j(c)),!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),r.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:"cookie"}),B(b,c);try{(g(c)||h(c))&&(c=j(c)),n&&n.setItem(t(b),c),r.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:b,newvalue:c,storageType:o.storageType})}catch(d){return a.$broadcast("LocalStorageModule.notification.error",d.message),B(b,c)}return!0},w=function(b){if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),C(b);var d=n?n.getItem(t(b)):null;return d&&"null"!==d?"{"===d.charAt(0)||"["===d.charAt(0)||c(d)?k(d):d:null},x=function(b){if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),r.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:"cookie"}),D(b);try{n.removeItem(t(b)),r.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:b,storageType:o.storageType})}catch(c){return a.$broadcast("LocalStorageModule.notification.error",c.message),D(b)}return!0},y=function(){if(!u)return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),!1;var b=p.length,c=[];for(var d in n)if(d.substr(0,b)===p)try{c.push(d.substr(b))}catch(e){return a.$broadcast("LocalStorageModule.notification.error",e.Description),[]}return c},z=function(b){b=b||"";var c=p.slice(0,-1),d=new RegExp(c+"."+b);if(!u||"cookie"===o.storageType)return u||a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),E();var e=p.length;for(var f in n)if(d.test(f))try{x(f.substr(e))}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.message),E()}return!0},A=function(){try{return navigator.cookieEnabled||"cookie"in l&&(l.cookie.length>0||(l.cookie="test").indexOf.call(l.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},B=function(b,c){if(e(c))return!1;if((h(c)||g(c))&&(c=j(c)),!A())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var d="",f=new Date,i="";if(null===c?(f.setTime(f.getTime()+-864e5),d="; expires="+f.toGMTString(),c=""):0!==q.expiry&&(f.setTime(f.getTime()+24*q.expiry*60*60*1e3),d="; expires="+f.toGMTString()),b){var k="; path="+q.path;q.domain&&(i="; domain="+q.domain),l.cookie=t(b)+"="+encodeURIComponent(c)+d+k+i}}catch(m){return a.$broadcast("LocalStorageModule.notification.error",m.message),!1}return!0},C=function(b){if(!A())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var c=l.cookie&&l.cookie.split(";")||[],d=0;d0||(m.cookie="test").indexOf.call(m.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},C=function(b,c){if(f(c))return!1;if((i(c)||h(c))&&(c=k(c)),!B())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var d="",e=new Date,g="";if(null===c?(e.setTime(e.getTime()+-864e5),d="; expires="+e.toGMTString(),c=""):0!==r.expiry&&(e.setTime(e.getTime()+24*r.expiry*60*60*1e3),d="; expires="+e.toGMTString()),b){var j="; path="+r.path;r.domain&&(g="; domain="+r.domain),m.cookie=u(b)+"="+encodeURIComponent(c)+d+j+g}}catch(l){return a.$broadcast("LocalStorageModule.notification.error",l.message),!1}return!0},D=function(b){if(!B())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var c=m.cookie&&m.cookie.split(";")||[],d=0;d