11/**
22 * An Angular module that gives you access to the browsers local storage
3- * @version v0.0.8 - 2014-08-15
3+ * @version v0.1.1 - 2014-10-07
44 * @link https://github.com/grevory/angular-local-storage
55 * @author grevory <greg@gregpike.ca>
66 * @license MIT License, http://www.opensource.org/licenses/MIT
77 */
88( function ( window , angular , undefined ) {
9+ /*jshint globalstrict:true*/
910'use strict' ;
11+
12+ var isDefined = angular . isDefined ,
13+ isUndefined = angular . isUndefined ,
14+ isNumber = angular . isNumber ,
15+ isObject = angular . isObject ,
16+ isArray = angular . isArray ,
17+ extend = angular . extend ,
18+ toJson = angular . toJson ,
19+ fromJson = angular . fromJson ;
20+
21+
22+ // Test if string is only contains numbers
23+ // e.g '1' => true, "'1'" => true
24+ function isStringNumber ( num ) {
25+ return / ^ - ? \d + \. ? \d * $ / . test ( num . replace ( / [ " ' ] / g, '' ) ) ;
26+ }
27+
1028var angularLocalStorage = angular . module ( 'LocalStorageModule' , [ ] ) ;
1129
1230angularLocalStorage . provider ( 'localStorageService' , function ( ) {
@@ -68,9 +86,7 @@ angularLocalStorage.provider('localStorageService', function() {
6886 } ;
6987 } ;
7088
71-
72-
73- this . $get = [ '$rootScope' , '$window' , '$document' , function ( $rootScope , $window , $document ) {
89+ this . $get = [ '$rootScope' , '$window' , '$document' , '$parse' , function ( $rootScope , $window , $document , $parse ) {
7490 var self = this ;
7591 var prefix = self . prefix ;
7692 var cookie = self . cookie ;
@@ -91,7 +107,7 @@ angularLocalStorage.provider('localStorageService', function() {
91107 }
92108 var deriveQualifiedKey = function ( key ) {
93109 return prefix + key ;
94- }
110+ } ;
95111 // Checks the browser to see if local storage is supported
96112 var browserSupportsLocalStorage = ( function ( ) {
97113 try {
@@ -123,24 +139,28 @@ angularLocalStorage.provider('localStorageService', function() {
123139 // If local storage is not available in the browser use cookies
124140 // Example use: localStorageService.add('library','angular');
125141 var addToLocalStorage = function ( key , value ) {
142+ // Let's convert undefined values to null to get the value consistent
143+ if ( isUndefined ( value ) ) {
144+ value = null ;
145+ } else if ( isObject ( value ) || isArray ( value ) || isNumber ( + value || value ) ) {
146+ value = toJson ( value ) ;
147+ }
126148
127149 // If this browser does not support local storage use cookies
128150 if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
129- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
151+ if ( ! browserSupportsLocalStorage ) {
152+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
153+ }
154+
130155 if ( notify . setItem ) {
131156 $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : 'cookie' } ) ;
132157 }
133158 return addToCookies ( key , value ) ;
134159 }
135160
136- // Let's convert undefined values to null to get the value consistent
137- if ( typeof value === "undefined" ) {
138- value = null ;
139- }
140-
141161 try {
142- if ( angular . isObject ( value ) || angular . isArray ( value ) ) {
143- value = angular . toJson ( value ) ;
162+ if ( isObject ( value ) || isArray ( value ) ) {
163+ value = toJson ( value ) ;
144164 }
145165 if ( webStorage ) { webStorage . setItem ( deriveQualifiedKey ( key ) , value ) } ;
146166 if ( notify . setItem ) {
@@ -158,7 +178,10 @@ angularLocalStorage.provider('localStorageService', function() {
158178 var getFromLocalStorage = function ( key ) {
159179
160180 if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
161- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
181+ if ( ! browserSupportsLocalStorage ) {
182+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
183+ }
184+
162185 return getFromCookies ( key ) ;
163186 }
164187
@@ -169,8 +192,8 @@ angularLocalStorage.provider('localStorageService', function() {
169192 return null ;
170193 }
171194
172- if ( item . charAt ( 0 ) === "{" || item . charAt ( 0 ) === "[" ) {
173- return angular . fromJson ( item ) ;
195+ if ( item . charAt ( 0 ) === "{" || item . charAt ( 0 ) === "[" || isStringNumber ( item ) ) {
196+ return fromJson ( item ) ;
174197 }
175198
176199 return item ;
@@ -179,8 +202,11 @@ angularLocalStorage.provider('localStorageService', function() {
179202 // Remove an item from local storage
180203 // Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular'
181204 var removeFromLocalStorage = function ( key ) {
182- if ( ! browserSupportsLocalStorage ) {
183- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
205+ if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
206+ if ( ! browserSupportsLocalStorage ) {
207+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
208+ }
209+
184210 if ( notify . removeItem ) {
185211 $rootScope . $broadcast ( 'LocalStorageModule.notification.removeitem' , { key : key , storageType : 'cookie' } ) ;
186212 }
@@ -235,8 +261,11 @@ angularLocalStorage.provider('localStorageService', function() {
235261 var tempPrefix = prefix . slice ( 0 , - 1 ) ;
236262 var testRegex = new RegExp ( tempPrefix + '.' + regularExpression ) ;
237263
238- if ( ! browserSupportsLocalStorage ) {
239- $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
264+ if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
265+ if ( ! browserSupportsLocalStorage ) {
266+ $rootScope . $broadcast ( 'LocalStorageModule.notification.warning' , 'LOCAL_STORAGE_NOT_SUPPORTED' ) ;
267+ }
268+
240269 return clearAllFromCookies ( ) ;
241270 }
242271
@@ -273,8 +302,10 @@ angularLocalStorage.provider('localStorageService', function() {
273302 // Example use: localStorageService.cookie.add('library','angular');
274303 var addToCookies = function ( key , value ) {
275304
276- if ( typeof value === "undefined" ) {
305+ if ( isUndefined ( value ) ) {
277306 return false ;
307+ } else if ( isArray ( value ) || isObject ( value ) ) {
308+ value = toJson ( value ) ;
278309 }
279310
280311 if ( ! browserSupportsCookies ( ) ) {
@@ -325,7 +356,13 @@ angularLocalStorage.provider('localStorageService', function() {
325356 thisCookie = thisCookie . substring ( 1 , thisCookie . length ) ;
326357 }
327358 if ( thisCookie . indexOf ( deriveQualifiedKey ( key ) + '=' ) === 0 ) {
328- return decodeURIComponent ( thisCookie . substring ( prefix . length + key . length + 1 , thisCookie . length ) ) ;
359+ var storedValues = decodeURIComponent ( thisCookie . substring ( prefix . length + key . length + 1 , thisCookie . length ) )
360+ try {
361+ var obj = JSON . parse ( storedValues ) ;
362+ return fromJson ( obj )
363+ } catch ( e ) {
364+ return storedValues
365+ }
329366 }
330367 }
331368 return null ;
@@ -355,22 +392,39 @@ angularLocalStorage.provider('localStorageService', function() {
355392 return storageType ;
356393 } ;
357394
358- var bindToScope = function ( scope , key , def ) {
359- var value = getFromLocalStorage ( key ) ;
395+ var bindToScope = function ( scope , scopeKey , def , lsKey ) {
396+ if ( ! lsKey ) {
397+ lsKey = scopeKey ;
398+ }
399+
400+ var value = getFromLocalStorage ( lsKey ) ;
360401
361- if ( value === null && angular . isDefined ( def ) ) {
402+ if ( value === null && isDefined ( def ) ) {
362403 value = def ;
363- } else if ( angular . isObject ( value ) && angular . isObject ( def ) ) {
364- value = angular . extend ( def , value ) ;
404+ } else if ( isObject ( value ) && isObject ( def ) ) {
405+ value = extend ( def , value ) ;
365406 }
366407
367- scope [ key ] = value ;
408+ $parse ( scopeKey ) . assign ( scope , value ) ;
368409
369- scope . $watchCollection ( key , function ( newVal ) {
370- addToLocalStorage ( key , newVal ) ;
410+ scope . $watchCollection ( scopeKey , function ( newVal ) {
411+ addToLocalStorage ( lsKey , newVal ) ;
371412 } ) ;
372413 } ;
373414
415+ // Return localStorageService.length
416+ // ignore keys that not owned
417+ var lengthOfLocalStorage = function ( ) {
418+ var count = 0 ;
419+ var storage = $window [ storageType ] ;
420+ for ( var i = 0 ; i < storage . length ; i ++ ) {
421+ if ( storage . key ( i ) . indexOf ( prefix ) === 0 ) {
422+ count ++ ;
423+ }
424+ }
425+ return count ;
426+ } ;
427+
374428 return {
375429 isSupported : browserSupportsLocalStorage ,
376430 getStorageType : getStorageType ,
@@ -382,6 +436,7 @@ angularLocalStorage.provider('localStorageService', function() {
382436 clearAll : clearAllFromLocalStorage ,
383437 bind : bindToScope ,
384438 deriveKey : deriveQualifiedKey ,
439+ length : lengthOfLocalStorage ,
385440 cookie : {
386441 set : addToCookies ,
387442 add : addToCookies , //DEPRECATED
0 commit comments