Skip to content

Commit e92355a

Browse files
Adding control of expiration time in localstorage. Issue #133.
1 parent d85bdb5 commit e92355a

File tree

6 files changed

+183
-66
lines changed

6 files changed

+183
-66
lines changed

dist/angular-local-storage.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* 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
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
@@ -14,6 +14,7 @@ var isDefined = angular.isDefined,
1414
isNumber = angular.isNumber,
1515
isObject = angular.isObject,
1616
isArray = angular.isArray,
17+
isBoolean = isBoolean,
1718
extend = angular.extend,
1819
toJson = angular.toJson,
1920
fromJson = angular.fromJson;
@@ -25,6 +26,10 @@ function isStringNumber(num) {
2526
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
2627
}
2728

29+
30+
function isBoolean(value) {
31+
return typeof value === 'boolean';
32+
}
2833
var angularLocalStorage = angular.module('LocalStorageModule', []);
2934

3035
angularLocalStorage.provider('localStorageService', function() {
@@ -138,13 +143,22 @@ angularLocalStorage.provider('localStorageService', function() {
138143
// Directly adds a value to local storage
139144
// If local storage is not available in the browser use cookies
140145
// Example use: localStorageService.add('library','angular');
141-
var addToLocalStorage = function (key, value) {
146+
var addToLocalStorage = function (key, value, compareDateExpiration) {
147+
var lsValue = {};
142148
// Let's convert undefined values to null to get the value consistent
143149
if (isUndefined(value)) {
144-
value = null;
150+
value = null;
145151
} 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();
147156
}
157+
158+
lsValue = {
159+
"date": compareDateExpiration || Date.now(),
160+
"data": value
161+
};
148162

149163
// If this browser does not support local storage use cookies
150164
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
@@ -160,11 +174,15 @@ angularLocalStorage.provider('localStorageService', function() {
160174

161175
try {
162176
if (isObject(value) || isArray(value)) {
163-
value = toJson(value);
177+
value = toJson(value);
178+
lsValue = {
179+
date: compareDateExpiration || Date.now(),
180+
data: value
181+
};
164182
}
165-
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
183+
if (webStorage) { webStorage.setItem(deriveQualifiedKey(key), JSON.stringify(lsValue)) };
166184
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 });
168186
}
169187
} catch (e) {
170188
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
@@ -175,7 +193,9 @@ angularLocalStorage.provider('localStorageService', function() {
175193

176194
// Directly get a value from local storage
177195
// Example use: localStorageService.get('library'); // returns 'angular'
178-
var getFromLocalStorage = function (key) {
196+
var getFromLocalStorage = function (key, expiration) {
197+
198+
var data, date, saved;
179199

180200
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
181201
if (!browserSupportsLocalStorage) {
@@ -184,19 +204,30 @@ angularLocalStorage.provider('localStorageService', function() {
184204

185205
return getFromCookies(key);
186206
}
187-
207+
188208
var item = webStorage ? webStorage.getItem(deriveQualifiedKey(key)) : null;
189209
// angular.toJson will convert null to 'null', so a proper conversion is needed
190210
// FIXME not a perfect solution, since a valid 'null' string can't be stored
191211
if (!item || item === 'null') {
192212
return null;
193213
}
194214

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);
197228
}
198229

199-
return item;
230+
return data;
200231
};
201232

202233
// Remove an item from local storage
@@ -407,7 +438,7 @@ angularLocalStorage.provider('localStorageService', function() {
407438
$parse(key).assign(scope, value);
408439

409440
return scope.$watch(key, function(newVal) {
410-
addToLocalStorage(lsKey, newVal);
441+
addToLocalStorage(lsKey, newVal);
411442
}, isObject(scope[key]));
412443
};
413444

dist/angular-local-storage.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/angular-local-storage.js

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,22 @@ angularLocalStorage.provider('localStorageService', function() {
111111
// Directly adds a value to local storage
112112
// If local storage is not available in the browser use cookies
113113
// Example use: localStorageService.add('library','angular');
114-
var addToLocalStorage = function (key, value) {
114+
var addToLocalStorage = function (key, value, compareDateExpiration) {
115+
var lsValue = {};
115116
// Let's convert undefined values to null to get the value consistent
116117
if (isUndefined(value)) {
117-
value = null;
118+
value = null;
118119
} else if (isObject(value) || isArray(value) || isNumber(+value || value)) {
119-
value = toJson(value);
120+
121+
value = toJson(value);
122+
} else if(isBoolean(value)){
123+
value = value.toString();
120124
}
125+
126+
lsValue = {
127+
"date": compareDateExpiration || Date.now(),
128+
"data": value
129+
};
121130

122131
// If this browser does not support local storage use cookies
123132
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
@@ -133,11 +142,15 @@ angularLocalStorage.provider('localStorageService', function() {
133142

134143
try {
135144
if (isObject(value) || isArray(value)) {
136-
value = toJson(value);
145+
value = toJson(value);
146+
lsValue = {
147+
date: compareDateExpiration || Date.now(),
148+
data: value
149+
};
137150
}
138-
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
151+
if (webStorage) { webStorage.setItem(deriveQualifiedKey(key), JSON.stringify(lsValue)) };
139152
if (notify.setItem) {
140-
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
153+
$rootScope.$broadcast('LocalStorageModule.notification.setitem', { key: key, newvalue: JSON.stringify(lsValue), storageType: self.storageType });
141154
}
142155
} catch (e) {
143156
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
@@ -148,7 +161,9 @@ angularLocalStorage.provider('localStorageService', function() {
148161

149162
// Directly get a value from local storage
150163
// Example use: localStorageService.get('library'); // returns 'angular'
151-
var getFromLocalStorage = function (key) {
164+
var getFromLocalStorage = function (key, expiration) {
165+
166+
var data, date, saved;
152167

153168
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
154169
if (!browserSupportsLocalStorage) {
@@ -157,19 +172,30 @@ angularLocalStorage.provider('localStorageService', function() {
157172

158173
return getFromCookies(key);
159174
}
160-
175+
161176
var item = webStorage ? webStorage.getItem(deriveQualifiedKey(key)) : null;
162177
// angular.toJson will convert null to 'null', so a proper conversion is needed
163178
// FIXME not a perfect solution, since a valid 'null' string can't be stored
164179
if (!item || item === 'null') {
165180
return null;
166181
}
167182

168-
if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) {
169-
return fromJson(item);
183+
item = JSON.parse(item);
184+
data = item.data;
185+
saved = item.date;
186+
187+
if (expiration) {
188+
var dateExpiration = new Date(saved).getTime() + expiration;
189+
if (dateExpiration < Date.now()) {
190+
return null;
191+
}
192+
}
193+
194+
if (data.charAt(0) === "{" || data.charAt(0) === "[" || isStringNumber(data)) {
195+
return fromJson(data);
170196
}
171197

172-
return item;
198+
return data;
173199
};
174200

175201
// Remove an item from local storage
@@ -380,7 +406,7 @@ angularLocalStorage.provider('localStorageService', function() {
380406
$parse(key).assign(scope, value);
381407

382408
return scope.$watch(key, function(newVal) {
383-
addToLocalStorage(lsKey, newVal);
409+
addToLocalStorage(lsKey, newVal);
384410
}, isObject(scope[key]));
385411
};
386412

src/common.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var isDefined = angular.isDefined,
66
isNumber = angular.isNumber,
77
isObject = angular.isObject,
88
isArray = angular.isArray,
9+
isBoolean = isBoolean,
910
extend = angular.extend,
1011
toJson = angular.toJson,
1112
fromJson = angular.fromJson;
@@ -16,3 +17,8 @@ var isDefined = angular.isDefined,
1617
function isStringNumber(num) {
1718
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
1819
}
20+
21+
22+
function isBoolean(value) {
23+
return typeof value === 'boolean';
24+
}

test/mock/localStorageMock.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ function localStorageMock() {
1111
writable: true
1212
},
1313
getItem: {
14-
value: function(key) {
15-
return storage[key];
14+
value: function (key) {
15+
var data = storage[key];
16+
17+
if (data) {
18+
data = storage[key].data || storage[key];
19+
}
20+
21+
return data;
1622
},
1723
enumerable: false,
1824
writable: true

0 commit comments

Comments
 (0)