1
1
/**
2
2
* An Angular module that gives you access to the browsers local storage
3
- * @version v0.1.3 - 2014-10-14
3
+ * @version v0.1.3 - 2014-10-22
4
4
* @link https://github.com/grevory/angular-local-storage
5
5
* @author grevory <greg@gregpike.ca>
6
6
* @license MIT License, http://www.opensource.org/licenses/MIT
@@ -14,6 +14,7 @@ var isDefined = angular.isDefined,
14
14
isNumber = angular . isNumber ,
15
15
isObject = angular . isObject ,
16
16
isArray = angular . isArray ,
17
+ isBoolean = isBoolean ,
17
18
extend = angular . extend ,
18
19
toJson = angular . toJson ,
19
20
fromJson = angular . fromJson ;
@@ -25,6 +26,10 @@ function isStringNumber(num) {
25
26
return / ^ - ? \d + \. ? \d * $ / . test ( num . replace ( / [ " ' ] / g, '' ) ) ;
26
27
}
27
28
29
+
30
+ function isBoolean ( value ) {
31
+ return typeof value === 'boolean' ;
32
+ }
28
33
var angularLocalStorage = angular . module ( 'LocalStorageModule' , [ ] ) ;
29
34
30
35
angularLocalStorage . provider ( 'localStorageService' , function ( ) {
@@ -138,13 +143,22 @@ angularLocalStorage.provider('localStorageService', function() {
138
143
// Directly adds a value to local storage
139
144
// If local storage is not available in the browser use cookies
140
145
// Example use: localStorageService.add('library','angular');
141
- var addToLocalStorage = function ( key , value ) {
146
+ var addToLocalStorage = function ( key , value , compareDateExpiration ) {
147
+ var lsValue = { } ;
142
148
// Let's convert undefined values to null to get the value consistent
143
149
if ( isUndefined ( value ) ) {
144
- value = null ;
150
+ value = null ;
145
151
} else if ( isObject ( value ) || isArray ( value ) || isNumber ( + value || value ) ) {
146
- value = toJson ( value ) ;
152
+
153
+ value = toJson ( value ) ;
154
+ } else if ( isBoolean ( value ) ) {
155
+ value = value . toString ( ) ;
147
156
}
157
+
158
+ lsValue = {
159
+ "date" : compareDateExpiration || Date . now ( ) ,
160
+ "data" : value
161
+ } ;
148
162
149
163
// If this browser does not support local storage use cookies
150
164
if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
@@ -160,11 +174,15 @@ angularLocalStorage.provider('localStorageService', function() {
160
174
161
175
try {
162
176
if ( isObject ( value ) || isArray ( value ) ) {
163
- value = toJson ( value ) ;
177
+ value = toJson ( value ) ;
178
+ lsValue = {
179
+ date : compareDateExpiration || Date . now ( ) ,
180
+ data : value
181
+ } ;
164
182
}
165
- if ( webStorage ) { webStorage . setItem ( deriveQualifiedKey ( key ) , value ) } ;
183
+ if ( webStorage ) { webStorage . setItem ( deriveQualifiedKey ( key ) , JSON . stringify ( lsValue ) ) } ;
166
184
if ( notify . setItem ) {
167
- $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : value , storageType : self . storageType } ) ;
185
+ $rootScope . $broadcast ( 'LocalStorageModule.notification.setitem' , { key : key , newvalue : JSON . stringify ( lsValue ) , storageType : self . storageType } ) ;
168
186
}
169
187
} catch ( e ) {
170
188
$rootScope . $broadcast ( 'LocalStorageModule.notification.error' , e . message ) ;
@@ -175,7 +193,9 @@ angularLocalStorage.provider('localStorageService', function() {
175
193
176
194
// Directly get a value from local storage
177
195
// Example use: localStorageService.get('library'); // returns 'angular'
178
- var getFromLocalStorage = function ( key ) {
196
+ var getFromLocalStorage = function ( key , expiration ) {
197
+
198
+ var data , date , saved ;
179
199
180
200
if ( ! browserSupportsLocalStorage || self . storageType === 'cookie' ) {
181
201
if ( ! browserSupportsLocalStorage ) {
@@ -184,19 +204,30 @@ angularLocalStorage.provider('localStorageService', function() {
184
204
185
205
return getFromCookies ( key ) ;
186
206
}
187
-
207
+
188
208
var item = webStorage ? webStorage . getItem ( deriveQualifiedKey ( key ) ) : null ;
189
209
// angular.toJson will convert null to 'null', so a proper conversion is needed
190
210
// FIXME not a perfect solution, since a valid 'null' string can't be stored
191
211
if ( ! item || item === 'null' ) {
192
212
return null ;
193
213
}
194
214
195
- if ( item . charAt ( 0 ) === "{" || item . charAt ( 0 ) === "[" || isStringNumber ( item ) ) {
196
- return fromJson ( item ) ;
215
+ item = JSON . parse ( item ) ;
216
+ data = item . data ;
217
+ saved = item . date ;
218
+
219
+ if ( expiration ) {
220
+ var dateExpiration = new Date ( saved ) . getTime ( ) + expiration ;
221
+ if ( dateExpiration < Date . now ( ) ) {
222
+ return null ;
223
+ }
224
+ }
225
+
226
+ if ( data . charAt ( 0 ) === "{" || data . charAt ( 0 ) === "[" || isStringNumber ( data ) ) {
227
+ return fromJson ( data ) ;
197
228
}
198
229
199
- return item ;
230
+ return data ;
200
231
} ;
201
232
202
233
// Remove an item from local storage
@@ -407,7 +438,7 @@ angularLocalStorage.provider('localStorageService', function() {
407
438
$parse ( key ) . assign ( scope , value ) ;
408
439
409
440
return scope . $watch ( key , function ( newVal ) {
410
- addToLocalStorage ( lsKey , newVal ) ;
441
+ addToLocalStorage ( lsKey , newVal ) ;
411
442
} , isObject ( scope [ key ] ) ) ;
412
443
} ;
413
444
0 commit comments