Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding control of expiration time in localstorage. Issue #133. #153

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 <[email protected]>
* @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.

55 changes: 43 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,11 @@ 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, remove) {

remove = remove === undefined ? false : remove;

var data, date, saved;

if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
Expand All @@ -157,19 +174,33 @@ 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()) {
if (remove) {
webStorage.removeItem(deriveQualifiedKey(key))
}
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 +411,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';
}
2 changes: 1 addition & 1 deletion test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = function(config) {
bower + 'angular/angular.js',
bower + 'angular-mocks/angular-mocks.js',
'src/*.js',
'test/mock/*.js',
'test/mock/**/*.js',
'test/spec/**/*.js'
],

Expand Down
Loading