Merge branch 'master' of github.com:grevory/angular-local-storage into regex_clear
Conflicts: angular-local-storage.jsrevert-117-master
commit
04b3c77257
@ -0,0 +1 @@
|
||||
(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);
|
@ -1,12 +1,55 @@
|
||||
describe('Module: LocalStorageModule', function() {
|
||||
'use strict';
|
||||
describe('Tests functionality of the localStorage module', function(){
|
||||
beforeEach(module('LocalStorageModule', function(localStorageServiceProvider){
|
||||
p = localStorageServiceProvider;
|
||||
}));
|
||||
var ls, store = [];
|
||||
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];
|
||||
}
|
||||
});
|
||||
|
||||
// Load the Angular module
|
||||
beforeEach(module('LocalStorageModule'));
|
||||
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();
|
||||
}
|
||||
return store[key] = val;
|
||||
});
|
||||
|
||||
describe('constants', function() {
|
||||
it('reads the constants', function() {
|
||||
expect(true).toBe(true);
|
||||
spyOn(ls, 'clearAll').andCallFake(function(){
|
||||
store = {};
|
||||
return store;
|
||||
});
|
||||
}));
|
||||
|
||||
it("Should add a value to my local storage", function(){
|
||||
var n = 234;
|
||||
ls.set('test', n);
|
||||
//Since localStorage makes the value a string, we look for the '234' and not 234
|
||||
expect(ls.get('test')).toBe('234');
|
||||
|
||||
var obj = { key: 'val' };
|
||||
ls.set('object', obj);
|
||||
var res = ls.get('object');
|
||||
expect(res.key).toBe('val');
|
||||
|
||||
});
|
||||
|
||||
it('Should allow me to set a prefix', function(){
|
||||
p.setPrefix("myPref");
|
||||
expect(p.prefix).toBe("myPref");
|
||||
});
|
||||
|
||||
it('Should allow me to set the cookie values', function(){
|
||||
p.setStorageCookie(60, '/path');
|
||||
expect(p.cookie.expiry).toBe(60);
|
||||
expect(p.cookie.path).toBe('/path');
|
||||
});
|
||||
});
|
Reference in New Issue