diff --git a/.gitignore b/.gitignore index 89009c0..338d9e5 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,4 @@ bower_components .tmp .DS_Store npm-debug.log - +*.swp diff --git a/angular-local-storage.js b/angular-local-storage.js index 147617e..f4166ba 100644 --- a/angular-local-storage.js +++ b/angular-local-storage.js @@ -62,6 +62,8 @@ angularLocalStorage.provider('localStorageService', function() { }; }; + + this.$get = ['$rootScope', '$window', '$document', function($rootScope, $window, $document) { var prefix = this.prefix; @@ -79,7 +81,9 @@ angularLocalStorage.provider('localStorageService', function() { if (prefix.substr(-1) !== '.') { prefix = !!prefix ? prefix + '.' : ''; } - + var deriveQualifiedKey = function(key) { + return prefix + key; + } // Checks the browser to see if local storage is supported var browserSupportsLocalStorage = (function () { try { @@ -90,7 +94,7 @@ angularLocalStorage.provider('localStorageService', function() { // // "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage // that exceeded the quota." - var key = prefix + '__' + Math.round(Math.random() * 1e7); + var key = deriveQualifiedKey('__' + Math.round(Math.random() * 1e7)); if (supported) { webStorage.setItem(key, ''); webStorage.removeItem(key); @@ -127,7 +131,7 @@ angularLocalStorage.provider('localStorageService', function() { if (angular.isObject(value) || angular.isArray(value)) { value = angular.toJson(value); } - webStorage.setItem(prefix + key, value); + webStorage.setItem(deriveQualifiedKey(key), value); if (notify.setItem) { $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: this.storageType}); } @@ -147,7 +151,7 @@ angularLocalStorage.provider('localStorageService', function() { return getFromCookies(key); } - var item = webStorage.getItem(prefix + key); + var item = webStorage.getItem(deriveQualifiedKey(key)); // 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 if (!item || item === 'null') { @@ -173,7 +177,7 @@ angularLocalStorage.provider('localStorageService', function() { } try { - webStorage.removeItem(prefix+key); + webStorage.removeItem(deriveQualifiedKey(key)); if (notify.removeItem) { $rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: this.storageType}); } @@ -286,7 +290,7 @@ angularLocalStorage.provider('localStorageService', function() { if(cookie.domain){ cookieDomain = "; domain=" + cookie.domain; } - $document.cookie = prefix + key + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain; + $document.cookie = deriveQualifiedKey(key) + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain; } } catch (e) { $rootScope.$broadcast('LocalStorageModule.notification.error',e.message); @@ -309,7 +313,7 @@ angularLocalStorage.provider('localStorageService', function() { while (thisCookie.charAt(0) === ' ') { thisCookie = thisCookie.substring(1,thisCookie.length); } - if (thisCookie.indexOf(prefix + key + '=') === 0) { + if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) { return decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length)); } } @@ -366,6 +370,7 @@ angularLocalStorage.provider('localStorageService', function() { remove: removeFromLocalStorage, clearAll: clearAllFromLocalStorage, bind: bindToScope, + deriveKey: deriveQualifiedKey, cookie: { set: addToCookies, add: addToCookies, //DEPRECATED diff --git a/test/spec/test.js b/test/spec/test.js index 2fb5411..418cf07 100644 --- a/test/spec/test.js +++ b/test/spec/test.js @@ -9,46 +9,74 @@ describe('Tests functionality of the localStorage module', function() { beforeEach(inject(function(_localStorageService_) { ls = _localStorageService_; - spyOn(ls, 'get').andCallFake(function(key) { - if (store[key].charAt(0) === '{' || store[key].charAt(0) === '[') { - return angular.fromJson(store[key]); - } else { - return store[key]; - } - }); - - spyOn(ls, 'set').andCallFake(function(key, val) { - if (angular.isObject(val) || angular.isArray(val)) { - val = angular.toJson(val); - } - if (angular.isNumber(val)){ - val = val.toString(); - } - store[key] = val; - return store[key]; - }); - - spyOn(ls, 'clearAll').andCallFake(function() { - store = {}; - return store; - }); + ls.clearAll(); + expect(ls.keys()).toEqual([]); })); - it('Should add a value to my local storage', function() { - var n = 234; - ls.set('test', n); + it('A key should be derived to .', function() { + var key = "foo"; + expect(ls.deriveKey(key)).toBe("ls." + key); + }); + + it('Should be able to replace a key multiple times', function() { + var key = "foo", + expectedValues = [ "bar", "zoo", "aoo" ]; + + for (var expectedValue in expectedValues) { + ls.set(key, expectedValue); + expect(ls.get(key)).toBe(expectedValue); + expect(ls.keys()).toEqual([key]); + } + }); + + it('Should delete a value from my local storage', function() { + var key = "foo", + expectedValue = "bar"; + + ls.set(key, expectedValue); + expect(ls.get(key)).toBe(expectedValue); + expect(ls.keys()).toEqual([key]); + + expect(ls.remove(key)).toBe(true); + expect(ls.get(key)).toBe(null); + expect(ls.keys()).toEqual([]); + }); + + it('Should add a integer value to my local storage', function() { + var key = "test", + expectedValue = 234; + ls.set(key, expectedValue); //Since localStorage makes the value a string, we look for the '234' and not 234 - expect(ls.get('test')).toBe('234'); + expect(ls.get(key)).toBe(expectedValue.toString()); + expect(ls.keys()).toEqual([key]); + }); + + it('Should add a String value to my local storage', function() { + var key = "foo", + expectedValue = "bar"; + ls.set(key, expectedValue); + expect(ls.get(key)).toBe(expectedValue); + expect(ls.keys()).toEqual([key]); + }); - var obj = { key: 'val' }; - ls.set('object', obj); - var res = ls.get('object'); + it('Should add a JSON value to my local storage', function() { + var key = "test", + expectedValue = { key: 'val' }; + ls.set(key, expectedValue); + + var res = ls.get(key); + expect(res).toEqual(expectedValue); expect(res.key).toBe('val'); + expect(ls.keys()).toEqual([key]); }); it('Should allow me to set a prefix', function() { - p.setPrefix('myPref'); - expect(p.prefix).toBe('myPref'); + + var expectedPrefix = "myPref"; + + p.setPrefix(expectedPrefix); + expect(p.prefix).toBe(expectedPrefix); + }); it('Should allow me to set the cookie values', function() { @@ -56,4 +84,4 @@ describe('Tests functionality of the localStorage module', function() { expect(p.cookie.expiry).toBe(60); expect(p.cookie.path).toBe('/path'); }); -}); \ No newline at end of file +});