Merge pull request #29 from mwickman/regex_clear

Adding ability to clear local storage based on a regex
revert-117-master
Gregory Pike 11 years ago
commit 2933a6f5fd

@ -171,9 +171,15 @@ angularLocalStorage.provider('localStorageService', function(){
};
// Remove all data for this app from local storage
// Also optionally takes a regular expression string and removes the matching key-value pairs
// Example use: localStorageService.clearAll();
// Should be used mostly for development purposes
var clearAllFromLocalStorage = function () {
var clearAllFromLocalStorage = function (regularExpression) {
var regularExpression = regularExpression || "";
//accounting for the '.' in the prefix when creating a regex
var tempPrefix = prefix.slice(0, -1) + "\.";
var testRegex = RegExp(tempPrefix + regularExpression);
if (!browserSupportsLocalStorage()) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
@ -183,12 +189,12 @@ angularLocalStorage.provider('localStorageService', function(){
var prefixLength = prefix.length;
for (var key in localStorage) {
// Only remove items that are for this app
if (key.substr(0,prefixLength) === prefix) {
// Only remove items that are for this app and match the regular expression
if (testRegex.test(key)) {
try {
removeFromLocalStorage(key.substr(prefixLength));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
$rootScope.$broadcast('LocalStorageModule.notification.error',e.message);
return clearAllFromCookies();
}
}
@ -304,4 +310,4 @@ angularLocalStorage.provider('localStorageService', function(){
};
}]
});
}).call(this);
}).call(this);

@ -1 +1 @@
(function(){var a=angular.module("LocalStorageModule",[]);a.value("prefix","ls"),a.constant("cookie",{expiry:30,path:"/"}),a.constant("notify",{setItem:!0,removeItem:!1}),a.service("localStorageService",["$rootScope","prefix","cookie","notify",function(a,b,c,d){"."!==b.substr(-1)&&(b=b?b+".":"");var e=function(){try{return"localStorage"in window&&null!==window.localStorage}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},f=function(c,f){if(!e())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),d.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:c,newvalue:f,storageType:"cookie"}),l(c,f);"undefined"==typeof f&&(f=null);try{(angular.isObject(f)||angular.isArray(f))&&(f=angular.toJson(f)),localStorage.setItem(b+c,f),d.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:c,newvalue:f,storageType:"localStorage"})}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.message),l(c,f)}return!0},g=function(c){if(!e())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),m(c);var d=localStorage.getItem(b+c);return d&&"null"!==d?"{"===d.charAt(0)||"["===d.charAt(0)?angular.fromJson(d):d:null},h=function(c){if(!e())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),d.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:c,storageType:"cookie"}),n(c);try{localStorage.removeItem(b+c),d.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:c,storageType:"localStorage"})}catch(f){return a.$broadcast("LocalStorageModule.notification.error",f.message),n(c)}return!0},i=function(){if(!e())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),!1;var c=b.length,d=[];for(var f in localStorage)if(f.substr(0,c)===b)try{d.push(f.substr(c))}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.Description),[]}return d},j=function(){if(!e())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),o();var c=b.length;for(var d in localStorage)if(d.substr(0,c)===b)try{h(d.substr(c))}catch(f){return a.$broadcast("LocalStorageModule.notification.error",f.message),o()}return!0},k=function(){try{return navigator.cookieEnabled||"cookie"in document&&(document.cookie.length>0||(document.cookie="test").indexOf.call(document.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},l=function(d,e){if("undefined"==typeof e)return!1;if(!k())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var f="",g=new Date;null===e?(g.setTime(g.getTime()+-864e5),f="; expires="+g.toGMTString(),e=""):0!==c.expiry&&(g.setTime(g.getTime()+24*c.expiry*60*60*1e3),f="; expires="+g.toGMTString()),d&&(document.cookie=b+d+"="+encodeURIComponent(e)+f+"; path="+c.path)}catch(h){return a.$broadcast("LocalStorageModule.notification.error",h.message),!1}return!0},m=function(c){if(!k())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var d=document.cookie.split(";"),e=0;e<d.length;e++){for(var f=d[e];" "==f.charAt(0);)f=f.substring(1,f.length);if(0===f.indexOf(b+c+"="))return decodeURIComponent(f.substring(b.length+c.length+1,f.length))}return null},n=function(a){l(a,null)},o=function(){for(var a=null,c=b.length,d=document.cookie.split(";"),e=0;e<d.length;e++){for(a=d[e];" "==a.charAt(0);)a=a.substring(1,a.length);key=a.substring(c,a.indexOf("=")),n(key)}};return{isSupported:e,set:f,add:f,get:g,keys:i,remove:h,clearAll:j,cookie:{set:l,add:l,get:m,remove:n,clearAll:o}}}])}).call(this);
(function(){"use strict";var a=angular.module("LocalStorageModule",[]);a.provider("localStorageService",function(){this.prefix="ls",this.cookie={expiry:30,path:"/"},this.notify={setItem:!0,removeItem:!1},this.setPrefix=function(a){this.prefix=a},this.setStorageCookie=function(a,b){this.cookie={expiry:a,path:b}},this.setNotify=function(a,b){this.notify={setItem:a,removeItem:b}},this.$get=["$rootScope",function(a){var b=this.prefix;"."!==b.substr(-1)&&(b=b?b+".":"");var c=function(){try{var c="localStorage"in window&&null!==window.localStorage,d=b+"__"+Math.round(1e7*Math.random());return c&&(localStorage.setItem(d,""),localStorage.removeItem(d)),!0}catch(e){return a.$broadcast("LocalStorageModule.notification.error",e.message),!1}},d=function(d,e){if(!c())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),notify.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:d,newvalue:e,storageType:"cookie"}),j(d,e);"undefined"==typeof e&&(e=null);try{(angular.isObject(e)||angular.isArray(e))&&(e=angular.toJson(e)),localStorage.setItem(b+d,e),notify.setItem&&a.$broadcast("LocalStorageModule.notification.setitem",{key:d,newvalue:e,storageType:"localStorage"})}catch(f){return a.$broadcast("LocalStorageModule.notification.error",f.message),j(d,e)}return!0},e=function(d){if(!c())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),k(d);var e=localStorage.getItem(b+d);return e&&"null"!==e?"{"===e.charAt(0)||"["===e.charAt(0)?angular.fromJson(e):e:null},f=function(d){if(!c())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),notify.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:d,storageType:"cookie"}),l(d);try{localStorage.removeItem(b+d),notify.removeItem&&a.$broadcast("LocalStorageModule.notification.removeitem",{key:d,storageType:"localStorage"})}catch(e){return a.$broadcast("LocalStorageModule.notification.error",e.message),l(d)}return!0},g=function(){if(!c())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),!1;var d=b.length,e=[];for(var f in localStorage)if(f.substr(0,d)===b)try{e.push(f.substr(d))}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.Description),[]}return e},h=function(d){var d=d||"",e=b.slice(0,-1)+".",g=RegExp(e+d);if(!c())return a.$broadcast("LocalStorageModule.notification.warning","LOCAL_STORAGE_NOT_SUPPORTED"),m();var h=b.length;for(var i in localStorage)if(g.test(i))try{f(i.substr(h))}catch(j){return a.$broadcast("LocalStorageModule.notification.error",j.message),m()}return!0},i=function(){try{return navigator.cookieEnabled||"cookie"in document&&(document.cookie.length>0||(document.cookie="test").indexOf.call(document.cookie,"test")>-1)}catch(b){return a.$broadcast("LocalStorageModule.notification.error",b.message),!1}},j=function(c,d){if("undefined"==typeof d)return!1;if(!i())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;try{var e="",f=new Date;null===d?(f.setTime(f.getTime()+-864e5),e="; expires="+f.toGMTString(),d=""):0!==cookie.expiry&&(f.setTime(f.getTime()+24*cookie.expiry*60*60*1e3),e="; expires="+f.toGMTString()),c&&(document.cookie=b+c+"="+encodeURIComponent(d)+e+"; path="+cookie.path)}catch(g){return a.$broadcast("LocalStorageModule.notification.error",g.message),!1}return!0},k=function(c){if(!i())return a.$broadcast("LocalStorageModule.notification.error","COOKIES_NOT_SUPPORTED"),!1;for(var d=document.cookie.split(";"),e=0;e<d.length;e++){for(var f=d[e];" "===f.charAt(0);)f=f.substring(1,f.length);if(0===f.indexOf(b+c+"="))return decodeURIComponent(f.substring(b.length+c.length+1,f.length))}return null},l=function(a){j(a,null)},m=function(){for(var a=null,c=b.length,d=document.cookie.split(";"),e=0;e<d.length;e++){for(a=d[e];" "===a.charAt(0);)a=a.substring(1,a.length);key=a.substring(c,a.indexOf("=")),l(key)}};return{isSupported:c,set:d,add:d,get:e,keys:g,remove:f,clearAll:h,cookie:{set:j,add:j,get:k,remove:l,clearAll:m}}}]})}).call(this);

@ -112,7 +112,7 @@ var YourCtrl = function($scope, localStorageService, ...) {
<tr>
<td><code>clearAll</code></td>
<td class="muted">n/a</td>
<td><span class="label label-warning">Warning</span> Removes all local storage key-value pairs for this app.</td>
<td><span class="label label-warning">Warning</span> Removes all local storage key-value pairs for this app. It will optionally take a string, which is converted to a regular expression, and then clears keys based on that regular expression.</td>
<td>Boolean for success</td>
</tr>
<tr>