From 76d682a2da51e21fea658a58b4f77adf54435599 Mon Sep 17 00:00:00 2001 From: Matthew Wickman Date: Sun, 22 Sep 2013 14:38:53 -0400 Subject: [PATCH 1/3] adding ability to clear local storage based on a regex --- localStorageModule.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/localStorageModule.js b/localStorageModule.js index 6f23c6a..77f905b 100644 --- a/localStorageModule.js +++ b/localStorageModule.js @@ -137,9 +137,16 @@ angularLocalStorage.service('localStorageService', [ }; // Remove all data for this app from local storage + // Also takes a regular expression string and removes the matching keys-value pairs // Example use: localStorageService.clearAll(); // Should be used mostly for development purposes - var clearAllFromLocalStorage = function () { + var clearAllFromLocalStorage = function (regular_expression) { + + regular_expression = regular_expression || ""; + //accounting for the '.' in the prefix + var temp_prefix = prefix.slice(0, -1) + "\."; + var testregex = RegExp(temp_prefix + regular_expression); + console.log('test regex: ', testregex); if (!browserSupportsLocalStorage()) { $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); @@ -149,10 +156,11 @@ angularLocalStorage.service('localStorageService', [ 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)); + removeFromLocalStorage(key.substr(prefixLength)); } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error',e.message); return clearAllFromCookies(); From b6e40ea79525f69f28ce5294e8c54c8d61f9a66d Mon Sep 17 00:00:00 2001 From: Matthew Wickman Date: Mon, 23 Sep 2013 09:34:31 -0400 Subject: [PATCH 2/3] fixing global variable --- localStorageModule.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/localStorageModule.js b/localStorageModule.js index 77f905b..9ad211e 100644 --- a/localStorageModule.js +++ b/localStorageModule.js @@ -142,11 +142,11 @@ angularLocalStorage.service('localStorageService', [ // Should be used mostly for development purposes var clearAllFromLocalStorage = function (regular_expression) { - regular_expression = regular_expression || ""; - //accounting for the '.' in the prefix + var regular_expression = regular_expression || ""; + //accounting for the '.' in the prefix when creating a regex var temp_prefix = prefix.slice(0, -1) + "\."; + //regex to test the local storage keys against var testregex = RegExp(temp_prefix + regular_expression); - console.log('test regex: ', testregex); if (!browserSupportsLocalStorage()) { $rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED'); @@ -157,10 +157,9 @@ angularLocalStorage.service('localStorageService', [ for (var key in localStorage) { // Only remove items that are for this app and match the regular expression - if (testregex.test(key)) { try { - removeFromLocalStorage(key.substr(prefixLength)); + removeFromLocalStorage(key.substr(prefixLength)); } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error',e.message); return clearAllFromCookies(); From 317f4dd9e68eddc8e287d30f387ebd51d59e2ea8 Mon Sep 17 00:00:00 2001 From: Matthew Wickman Date: Tue, 3 Dec 2013 11:30:07 -0500 Subject: [PATCH 3/3] updating docs on demo page --- demo/demo.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/demo.html b/demo/demo.html index d4e03c4..89fcbfb 100644 --- a/demo/demo.html +++ b/demo/demo.html @@ -112,7 +112,7 @@ var YourCtrl = function($scope, localStorageService, ...) { clearAll n/a - Warning Removes all local storage key-value pairs for this app. + Warning 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. Boolean for success