Skip to content

Commit

Permalink
Adding control of expiration time in localstorage. Issue #133.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigo-morais committed Oct 23, 2014
1 parent d85bdb5 commit e92355a
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 66 deletions.
57 changes: 44 additions & 13 deletions dist/angular-local-storage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* An Angular module that gives you access to the browsers local storage
* @version v0.1.3 - 2014-10-14
* @version v0.1.3 - 2014-10-22
* @link https://github.com/grevory/angular-local-storage
* @author grevory <greg@gregpike.ca>
* @license MIT License, http://www.opensource.org/licenses/MIT
Expand All @@ -14,6 +14,7 @@ var isDefined = angular.isDefined,
isNumber = angular.isNumber,
isObject = angular.isObject,
isArray = angular.isArray,
isBoolean = isBoolean,
extend = angular.extend,
toJson = angular.toJson,
fromJson = angular.fromJson;
Expand All @@ -25,6 +26,10 @@ function isStringNumber(num) {
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
}


function isBoolean(value) {
return typeof value === 'boolean';
}
var angularLocalStorage = angular.module('LocalStorageModule', []);

angularLocalStorage.provider('localStorageService', function() {
Expand Down Expand Up @@ -138,13 +143,22 @@ angularLocalStorage.provider('localStorageService', function() {
// Directly adds a value to local storage
// If local storage is not available in the browser use cookies
// Example use: localStorageService.add('library','angular');
var addToLocalStorage = function (key, value) {
var addToLocalStorage = function (key, value, compareDateExpiration) {
var lsValue = {};
// Let's convert undefined values to null to get the value consistent
if (isUndefined(value)) {
value = null;
value = null;
} else if (isObject(value) || isArray(value) || isNumber(+value || value)) {
value = toJson(value);

value = toJson(value);
} else if(isBoolean(value)){
value = value.toString();
}

lsValue = {
"date": compareDateExpiration || Date.now(),
"data": value
};

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

try {
if (isObject(value) || isArray(value)) {
value = toJson(value);
value = toJson(value);
lsValue = {
date: compareDateExpiration || Date.now(),
data: value
};
}
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
if (webStorage) { webStorage.setItem(deriveQualifiedKey(key), JSON.stringify(lsValue)) };
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
$rootScope.$broadcast('LocalStorageModule.notification.setitem', { key: key, newvalue: JSON.stringify(lsValue), storageType: self.storageType });
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
Expand All @@ -175,7 +193,9 @@ angularLocalStorage.provider('localStorageService', function() {

// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
var getFromLocalStorage = function (key) {
var getFromLocalStorage = function (key, expiration) {

var data, date, saved;

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

return getFromCookies(key);
}

var item = webStorage ? webStorage.getItem(deriveQualifiedKey(key)) : null;
// 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') {
return null;
}

if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) {
return fromJson(item);
item = JSON.parse(item);
data = item.data;
saved = item.date;

if (expiration) {
var dateExpiration = new Date(saved).getTime() + expiration;
if (dateExpiration < Date.now()) {
return null;
}
}

if (data.charAt(0) === "{" || data.charAt(0) === "[" || isStringNumber(data)) {
return fromJson(data);
}

return item;
return data;
};

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

return scope.$watch(key, function(newVal) {
addToLocalStorage(lsKey, newVal);
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
};

Expand Down
4 changes: 2 additions & 2 deletions dist/angular-local-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 38 additions & 12 deletions src/angular-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,22 @@ angularLocalStorage.provider('localStorageService', function() {
// Directly adds a value to local storage
// If local storage is not available in the browser use cookies
// Example use: localStorageService.add('library','angular');
var addToLocalStorage = function (key, value) {
var addToLocalStorage = function (key, value, compareDateExpiration) {
var lsValue = {};
// Let's convert undefined values to null to get the value consistent
if (isUndefined(value)) {
value = null;
value = null;
} else if (isObject(value) || isArray(value) || isNumber(+value || value)) {
value = toJson(value);

value = toJson(value);
} else if(isBoolean(value)){
value = value.toString();
}

lsValue = {
"date": compareDateExpiration || Date.now(),
"data": value
};

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

try {
if (isObject(value) || isArray(value)) {
value = toJson(value);
value = toJson(value);
lsValue = {
date: compareDateExpiration || Date.now(),
data: value
};
}
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
if (webStorage) { webStorage.setItem(deriveQualifiedKey(key), JSON.stringify(lsValue)) };
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
$rootScope.$broadcast('LocalStorageModule.notification.setitem', { key: key, newvalue: JSON.stringify(lsValue), storageType: self.storageType });
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
Expand All @@ -148,7 +161,9 @@ angularLocalStorage.provider('localStorageService', function() {

// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
var getFromLocalStorage = function (key) {
var getFromLocalStorage = function (key, expiration) {

var data, date, saved;

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

return getFromCookies(key);
}

var item = webStorage ? webStorage.getItem(deriveQualifiedKey(key)) : null;
// 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') {
return null;
}

if (item.charAt(0) === "{" || item.charAt(0) === "[" || isStringNumber(item)) {
return fromJson(item);
item = JSON.parse(item);
data = item.data;
saved = item.date;

if (expiration) {
var dateExpiration = new Date(saved).getTime() + expiration;
if (dateExpiration < Date.now()) {
return null;
}
}

if (data.charAt(0) === "{" || data.charAt(0) === "[" || isStringNumber(data)) {
return fromJson(data);
}

return item;
return data;
};

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

return scope.$watch(key, function(newVal) {
addToLocalStorage(lsKey, newVal);
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
};

Expand Down
6 changes: 6 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var isDefined = angular.isDefined,
isNumber = angular.isNumber,
isObject = angular.isObject,
isArray = angular.isArray,
isBoolean = isBoolean,
extend = angular.extend,
toJson = angular.toJson,
fromJson = angular.fromJson;
Expand All @@ -16,3 +17,8 @@ var isDefined = angular.isDefined,
function isStringNumber(num) {
return /^-?\d+\.?\d*$/.test(num.replace(/["']/g, ''));
}


function isBoolean(value) {
return typeof value === 'boolean';
}
10 changes: 8 additions & 2 deletions test/mock/localStorageMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ function localStorageMock() {
writable: true
},
getItem: {
value: function(key) {
return storage[key];
value: function (key) {
var data = storage[key];

if (data) {
data = storage[key].data || storage[key];
}

return data;
},
enumerable: false,
writable: true
Expand Down

0 comments on commit e92355a

Please sign in to comment.