Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

angular-local-storage-ci-dev

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

angular-local-storage-ci-dev - npm Package Compare versions

Comparing version 0.2.4 to 0.3.0

dist/angular-local-storage_bac.js

835

dist/angular-local-storage.js

@@ -0,44 +1,25 @@

var isDefined = angular.isDefined,
isUndefined = angular.isUndefined,
isNumber = angular.isNumber,
isObject = angular.isObject,
isArray = angular.isArray,
extend = angular.extend,
toJson = angular.toJson,
ONE_DAY_MILLISECONDS = 24 * 60 * 60 * 1000,
isPrimitive = function(sth) {
var type = Object.prototype.toString.call(sth).toLowerCase();
return '[object string],[object undefined],[object null],[object number],[object boolean]'.split(',').some(function (typeStr) {
return typeStr == type;
});
};
/**
* An Angular module that gives you access to the browsers local storage
* @version v0.2.2 - 2015-05-29
* @link https://github.com/grevory/angular-local-storage
* @author grevory <greg@gregpike.ca>
* @license MIT License, http://www.opensource.org/licenses/MIT
* 改动:
* 1. 统一过期时间设置,换为时间戳: 涉及cookie.expiry的写和读相应逻辑要做兼容,增加setExpiry方法
* 2. 对localStorage的读写操作增加过期时间的判断,四种数据结构做兼容: 非primitive类型,无__ts; primitive类型; 有自定义过期时间设置;
* 3. 增加统一的入口设置配置
*/
(function (window, angular, undefined) {
/*jshint globalstrict:true*/
'use strict';
var isDefined = angular.isDefined,
isUndefined = angular.isUndefined,
isNumber = angular.isNumber,
isObject = angular.isObject,
isArray = angular.isArray,
extend = angular.extend,
toJson = angular.toJson;
/**
* 判断是否为undefined/null/NaN/String/Boolean/Number之一,是,返回true,否则返回false
* @param {any} sth
* @return {Boolean}
*/
function isPrimitive(sth) {
var type = isTypeOf(sth);
return type == 'string' || type == 'undefined' || type == 'null' || type == 'number' || type == 'boolean';
}
var objectStrPrefixReg = /^\[object\s/,
objectStrSuffixReg = /]$/;
function isTypeOf(sth) {
var typeStr = Object.prototype.toString.call(sth);
return typeStr.replace(objectStrPrefixReg, '').replace(objectStrSuffixReg, '').toLowerCase();
}
var angularLocalStorage = angular.module('LocalStorageModule', []);
angularLocalStorage.provider('localStorageService', function () {
angular
.module('LocalStorageModule', [])
.provider('localStorageService', function() {
// You should set a prefix to avoid overwriting any local storage variables from the rest of your app

@@ -52,3 +33,3 @@ // e.g. localStorageServiceProvider.setPrefix('yourAppName');

this.expirePrefix = '__ts';
this.expiryPrefix = '__expiry';

@@ -58,2 +39,7 @@ // You could change web storage type localstorage or sessionStorage

this.expiry = {
value: 2 * 60 * 60 * 1000,
alwaysExpire: false
};
// Cookie options (usually in case of fallback)

@@ -63,15 +49,5 @@ // expiry = Number of days before cookies expire // 0 = Does not expire

this.cookie = {
expiry: 30,
path: '/'
};
// default expire time 2h
this.expires = 120 * 60 * 1000;
// expire time setter
this.setExpires = function (expires) {
this.expires = expires;
return this;
};
// Send signals for each of the following actions?

@@ -83,4 +59,14 @@ this.notify = {

this.setOptions = function(options) {
};
// Setter for expiry
this.setExpiry = function(exp, prefix) {
this.expiry.value = exp; // millisecond
prefix && (this.expiry.prefix = prefix);
};
// Setter for the prefix
this.setPrefix = function (prefix) {
this.setPrefix = function(prefix) {
this.prefix = prefix;

@@ -91,3 +77,3 @@ return this;

// Setter for the storageType
this.setStorageType = function (storageType) {
this.setStorageType = function(storageType) {
this.storageType = storageType;

@@ -98,4 +84,8 @@ return this;

// Setter for cookie config
this.setStorageCookie = function (exp, path) {
this.cookie.expiry = exp;
this.setStorageCookie = function(exp, path) {
if (exp) {
console.warn('Set expiry for cookie is deprecated, use setExpiry instead.');
// this.cookie.expiry = exp;
this.expiry.value = exp * ONE_DAY_MILLISECONDS; // trans days to millisecond
}
this.cookie.path = path;

@@ -106,3 +96,3 @@ return this;

// Setter for cookie domain
this.setStorageCookieDomain = function (domain) {
this.setStorageCookieDomain = function(domain) {
this.cookie.domain = domain;

@@ -114,3 +104,3 @@ return this;

// itemSet & itemRemove should be booleans
this.setNotify = function (itemSet, itemRemove) {
this.setNotify = function(itemSet, itemRemove) {
this.notify = {

@@ -123,411 +113,460 @@ setItem: itemSet,

this.$get = [
'$rootScope', '$window', '$document', '$parse', function ($rootScope, $window, $document, $parse) {
var self = this;
var prefix = self.prefix;
var cookie = self.cookie;
var notify = self.notify;
var storageType = self.storageType;
var webStorage;
var expires = self.expires;
this.$get = ['$rootScope', '$window', '$document', '$parse', function($rootScope, $window, $document, $parse) {
var self = this;
var prefix = self.prefix;
var cookie = self.cookie;
var expiry = self.expiry;
var notify = self.notify;
var storageType = self.storageType;
var webStorage;
// When Angular's $document is not available
if (!$document) {
$document = document;
} else if ($document[0]) {
$document = $document[0];
// When Angular's $document is not available
if (!$document) {
$document = document;
} else if ($document[0]) {
$document = $document[0];
}
// If there is a prefix set in the config lets use that with an appended period for readability
if (prefix.substr(-1) !== '.') {
prefix = !!prefix ? prefix + '.' : '';
}
var deriveQualifiedKey = function(key) {
return prefix + key;
};
// Checks the browser to see if local storage is supported
var browserSupportsLocalStorage = (function () {
try {
var supported = (storageType in $window && $window[storageType] !== null);
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
// is available, but trying to call .setItem throws an exception.
//
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
// that exceeded the quota."
var key = deriveQualifiedKey('__' + Math.round(Math.random() * 1e7));
if (supported) {
webStorage = $window[storageType];
webStorage.setItem(key, '');
webStorage.removeItem(key);
}
return supported;
} catch (e) {
storageType = 'cookie';
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}
}());
// If there is a prefix set in the config lets use that with an appended period for readability
if (prefix.substr(-1) !== '.') {
prefix = !!prefix ? prefix + '.' : '';
/**
* Directly adds a value to local storage
* If local storage is not available in the browser use cookies
* Example use: localStorageService.add('library','angular');
*
* @param {String} key The key of your value. (Required)
* @param {Any} value The value you want to storage. (Required)
* @param {Number/Boolean} expiry When it's a `Number` that greater than 0 means this value should be expired,
* and this number is the expiry time in millisecond.
* When it's a `Boolean`, means this value should be expired, and use the global expiry time.
* Otherwise, this value won't be expired.
* @param {Number} expireTimeStamp The exactly expire timeStamp. (Optional)
* @returns {boolean}
*/
var addToLocalStorage = function (key, value, expiry, expireTimeStamp) {
var daysToExpire = 0;
var wrappedData = {};
// If need to be expired, wrapper the data and add the expire timestamp
// NEW: 改成无论如何都设置expiry, 不缓存的话expiry为0
if (self.expiry.alwaysExpire || expiry) {
// 如果expiry为数字且大于0,不变;否则设置为默认的过期时间
expiry = (isNumber(expiry) && expiry > 0) ? expiry : self.expiry.value;
// 如果设置了指定过期的时间戳,直接使用;否则设置为now + expiry
expireTimeStamp = expireTimeStamp || (Date.now() + expiry);
daysToExpire = expiry / ONE_DAY_MILLISECONDS;
} else {
expireTimeStamp = 0;
}
var deriveQualifiedKey = function (key) {
return prefix + key;
};
// Checks the browser to see if local storage is supported
var browserSupportsLocalStorage = (function () {
try {
var supported = (storageType in $window && $window[storageType] !== null);
// When Safari (OS X or iOS) is in private browsing mode, it appears as though localStorage
// is available, but trying to call .setItem throws an exception.
//
// "QUOTA_EXCEEDED_ERR: DOM Exception 22: An attempt was made to add something to storage
// that exceeded the quota."
var key = deriveQualifiedKey('__' + Math.round(Math.random() * 1e7));
if (supported) {
webStorage = $window[storageType];
webStorage.setItem(key, '');
webStorage.removeItem(key);
}
// By Xiao Yuze, As we now wrap the data into another object, there's no necessary to convert undefined.
// Let's convert undefined values to null to get the value consistent
// if (isUndefined(value)) {
// value = null;
// }
return supported;
} catch (e) {
storageType = 'cookie';
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}
}());
wrappedData.value = value;
// 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, needExpired) {
wrappedData[self.expiryPrefix] = expireTimeStamp;
// Let's convert undefined values to null to get the value consistent
if (isUndefined(value)) {
value = null;
} else {
// If need Expired, wrapper the data and add ts for timeStamp to record the current timeStamp
if (needExpired) {
value = {
value: value
};
value[self.expirePrefix] = Date.now();
}
var wrappedDataJSON = toJson(wrappedData);
value = toJson(value);
// If this browser does not support local storage use cookies
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
}
// If this browser does not support local storage use cookies
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
}
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'});
}
return addToCookies(key, value, daysToExpire);
}
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem',
{ key: key, newvalue: value, storageType: 'cookie' });
}
return addToCookies(key, value);
try {
if (webStorage) {
webStorage.setItem(deriveQualifiedKey(key), wrappedDataJSON);
}
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
try {
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)}
;
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem',
{ key: key, newvalue: value, storageType: self.storageType });
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return addToCookies(key, value);
return addToCookies(key, value, daysToExpire);
}
return true;
};
// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
/**
* 从localStorage中获取缓存
*
* 现在所有数据都会带上expiryPrefix,如果没带,兼容;
* 如果有老数据__ts, 表示存的起始时间的时间戳,则兼容后判断过期时间
*
* @param {String} key
* @param {Boolean/Number} forceNeedExpire See DOC on `addToLocalStorage`
* @returns {*}
*/
var getFromLocalStorage = function (key, forceNeedExpire) {
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
}
return true;
};
// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
/**
*
* @param {String} key key
* @param {Boolean} forceNeedExpired 是否强制需要缓存,用于数据结构的兼容(如原来的数据无过期,现在需要变为带过期)
* @returns {*}
*/
var getFromLocalStorage = function (key, forceNeedExpired) {
return getFromCookies(key);
}
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
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;
}
try {
item = JSON.parse(item);
// 有很多种case,:
// 1. 原始类型
// 看forceNeedExpire,如无直接返回,有的话加上再返回
// 2. 非原始类型
// 1)老的数据结构,带__ts,兼容后判断是否过期
// 2)不带__ts或expiryPrefix,看forceNeedExpire,有则包上新的过期时间,然后返回;否则包上expiry为0,并返回
// 3)有expiryPrefix,判断过期时间后返回
// 现在无论如何都要包expiry,所以如果判断为Primitive,直接包一层然后返回
if (isPrimitive(item)) {
addToLocalStorage(key, item, forceNeedExpire);
return item;
} else {
// 老数据结构兼容,或者是完全没有初始化的值
if (('__ts' in item)) {
addToLocalStorage(key, item.value, true, item.__ts);
// 做完兼容后再取一次
item = webStorage.getItem(deriveQualifiedKey(key));
}
// 如果没有__ts且没有expiry, 说明未初始化时间,做兼容处理
else if (!(self.expiryPrefix in item)) {
addToLocalStorage(key, item, forceNeedExpire);
return getFromCookies(key);
}
// 做完兼容后再取一次
item = webStorage.getItem(deriveQualifiedKey(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;
}
// 到这里,可以保证item必定有expiry
try {
item = JSON.parse(item);
if (item[self.expiryPrefix] === 0) {
return item.value;
}
// Expired
else if (Date.now() - item[self.expiryPrefix] > 0) {
if (isPrimitive(item)) {
if (forceNeedExpired) {
addToLocalStorage(key, item, forceNeedExpired);
return item;
} else {
return item;
}
webStorage.removeItem(deriveQualifiedKey(key));
return undefined;
} else {
if (self.expirePrefix in item) {
// Expired
if (Date.now() - item[self.expirePrefix] > expires) {
webStorage.removeItem(deriveQualifiedKey(key));
return undefined;
} else {
return item.value;
}
} else {
return item;
}
return item.value;
}
} catch (e) {
return item;
}
};
} catch (e) {
return item;
}
};
// Remove an item from local storage
// Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular'
var removeFromLocalStorage = function () {
var i, key;
for (i = 0; i < arguments.length; i++) {
key = arguments[i];
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
}
// Remove an item from local storage
// Example use: localStorageService.remove('library'); // removes the key/value pair of library='angular'
var removeFromLocalStorage = function () {
var i, key;
for (i=0; i<arguments.length; i++) {
key = arguments[i];
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
}
if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {key: key, storageType: 'cookie'});
}
removeFromCookies(key);
}
else {
try {
webStorage.removeItem(deriveQualifiedKey(key));
if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeitem',
{ key: key, storageType: 'cookie' });
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {
key: key,
storageType: self.storageType
});
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
removeFromCookies(key);
}
else {
try {
webStorage.removeItem(deriveQualifiedKey(key));
if (notify.removeItem) {
$rootScope.$broadcast('LocalStorageModule.notification.removeitem', {
key: key,
storageType: self.storageType
});
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
removeFromCookies(key);
}
}
}
};
}
};
// Return array of keys for local storage
// Example use: var keys = localStorageService.keys()
var getKeysForLocalStorage = function () {
// Return array of keys for local storage
// Example use: var keys = localStorageService.keys()
var getKeysForLocalStorage = function () {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
return false;
}
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
return [];
}
var prefixLength = prefix.length;
var keys = [];
for (var key in webStorage) {
// Only return keys that are for this app
if (key.substr(0, prefixLength) === prefix) {
try {
keys.push(key.substr(prefixLength));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.Description);
return [];
}
var prefixLength = prefix.length;
var keys = [];
for (var key in webStorage) {
// Only return keys that are for this app
if (key.substr(0, prefixLength) === prefix) {
try {
keys.push(key.substr(prefixLength));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.Description);
return [];
}
}
return keys;
};
}
return keys;
};
// Remove all data for this app from local storage
// Also optionally takes a regular expression string and removes the matching key-value pairs
// Example use: localStorageService.clearAll();
// Should be used mostly for development purposes
var clearAllFromLocalStorage = function (regularExpression) {
// Remove all data for this app from local storage
// Also optionally takes a regular expression string and removes the matching key-value pairs
// Example use: localStorageService.clearAll();
// Should be used mostly for development purposes
var clearAllFromLocalStorage = function (regularExpression) {
// Setting both regular expressions independently
// Empty strings result in catchall RegExp
var prefixRegex = !!prefix ? new RegExp('^' + prefix) : new RegExp();
var testRegex = !!regularExpression ? new RegExp(regularExpression) : new RegExp();
// Setting both regular expressions independently
// Empty strings result in catchall RegExp
var prefixRegex = !!prefix ? new RegExp('^' + prefix) : new RegExp();
var testRegex = !!regularExpression ? new RegExp(regularExpression) : new RegExp();
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
}
return clearAllFromCookies();
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning', 'LOCAL_STORAGE_NOT_SUPPORTED');
}
return clearAllFromCookies();
}
var prefixLength = prefix.length;
var prefixLength = prefix.length;
for (var key in webStorage) {
// Only remove items that are for this app and match the regular expression
if (prefixRegex.test(key) && testRegex.test(key.substr(prefixLength))) {
try {
removeFromLocalStorage(key.substr(prefixLength));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return clearAllFromCookies();
}
for (var key in webStorage) {
// Only remove items that are for this app and match the regular expression
if (prefixRegex.test(key) && testRegex.test(key.substr(prefixLength))) {
try {
removeFromLocalStorage(key.substr(prefixLength));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return clearAllFromCookies();
}
}
return true;
};
}
return true;
};
// Checks the browser to see if cookies are supported
var browserSupportsCookies = (function () {
try {
return $window.navigator.cookieEnabled ||
("cookie" in $document && ($document.cookie.length > 0 ||
($document.cookie = "test").indexOf.call($document.cookie, "test") > -1));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}
}());
// Checks the browser to see if cookies are supported
var browserSupportsCookies = (function() {
try {
return $window.navigator.cookieEnabled ||
("cookie" in $document && ($document.cookie.length > 0 ||
($document.cookie = "test").indexOf.call($document.cookie, "test") > -1));
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}
}());
// Directly adds a value to cookies
// Typically used as a fallback is local storage is not available in the browser
// Example use: localStorageService.cookie.add('library','angular');
var addToCookies = function (key, value, daysToExpiry) {
// Directly adds a value to cookies
// Typically used as a fallback is local storage is not available in the browser
// Example use: localStorageService.cookie.add('library','angular');
var addToCookies = function (key, value, daysToExpiry) {
if (isUndefined(value)) {
return false;
} else if (isArray(value) || isObject(value)) {
value = toJson(value);
}
if (isUndefined(value)) {
return false;
} else if(isArray(value) || isObject(value)) {
value = toJson(value);
}
if (!browserSupportsCookies) {
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
}
if (!browserSupportsCookies) {
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
}
try {
var expiry = '',
expiryDate = new Date(),
cookieDomain = '';
try {
var expiry = '',
expiryDate = new Date(),
cookieDomain = '';
if (value === null) {
// Mark that the cookie has expired one day ago
expiryDate.setTime(expiryDate.getTime() + (-1 * 24 * 60 * 60 * 1000));
expiry = "; expires=" + expiryDate.toGMTString();
value = '';
} else if (isNumber(daysToExpiry) && daysToExpiry !== 0) {
expiryDate.setTime(expiryDate.getTime() + (daysToExpiry * 24 * 60 * 60 * 1000));
expiry = "; expires=" + expiryDate.toGMTString();
} else if (cookie.expiry !== 0) {
expiryDate.setTime(expiryDate.getTime() + (cookie.expiry * 24 * 60 * 60 * 1000));
expiry = "; expires=" + expiryDate.toGMTString();
if (value === null) {
// Mark that the cookie has expired one day ago
expiryDate.setTime(expiryDate.getTime() + (-1 * ONE_DAY_MILLISECONDS));
expiry = "; expires=" + expiryDate.toGMTString();
value = '';
} else if (isNumber(daysToExpiry) && daysToExpiry !== 0) {
expiryDate.setTime(expiryDate.getTime() + (daysToExpiry * ONE_DAY_MILLISECONDS));
expiry = "; expires=" + expiryDate.toGMTString();
} else if (expiry.value !== 0) { // read expiry setting from self.expiry.value instead of cookie.expiry
expiryDate.setTime(expiryDate.getTime() + expiry.value);
expiry = "; expires=" + expiryDate.toGMTString();
}
if (!!key) {
var cookiePath = "; path=" + cookie.path;
if(cookie.domain){
cookieDomain = "; domain=" + cookie.domain;
}
if (!!key) {
var cookiePath = "; path=" + cookie.path;
if (cookie.domain) {
cookieDomain = "; domain=" + cookie.domain;
}
$document.cookie = deriveQualifiedKey(key) + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain;
}
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
$document.cookie = deriveQualifiedKey(key) + "=" + encodeURIComponent(value) + expiry + cookiePath + cookieDomain;
}
return true;
};
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return false;
}
return true;
};
// Directly get a value from a cookie
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
var getFromCookies = function (key) {
if (!browserSupportsCookies) {
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
// Directly get a value from a cookie
// Example use: localStorageService.cookie.get('library'); // returns 'angular'
var getFromCookies = function (key) {
if (!browserSupportsCookies) {
$rootScope.$broadcast('LocalStorageModule.notification.error', 'COOKIES_NOT_SUPPORTED');
return false;
}
var cookies = $document.cookie && $document.cookie.split(';') || [];
for(var i=0; i < cookies.length; i++) {
var thisCookie = cookies[i];
while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1,thisCookie.length);
}
var cookies = $document.cookie && $document.cookie.split(';') || [];
for (var i = 0; i < cookies.length; i++) {
var thisCookie = cookies[i];
while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1, thisCookie.length);
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
var storedValues = decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length));
try {
return JSON.parse(storedValues);
} catch(e) {
return storedValues;
}
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
var storedValues = decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1,
thisCookie.length))
try {
return JSON.parse(storedValues);
} catch (e) {
return storedValues
}
}
}
return null;
};
}
return null;
};
var removeFromCookies = function (key) {
addToCookies(key, null);
};
var removeFromCookies = function (key) {
addToCookies(key,null);
};
var clearAllFromCookies = function () {
var thisCookie = null, thisKey = null;
var prefixLength = prefix.length;
var cookies = $document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
thisCookie = cookies[i];
var clearAllFromCookies = function () {
var thisCookie = null, thisKey = null;
var prefixLength = prefix.length;
var cookies = $document.cookie.split(';');
for(var i = 0; i < cookies.length; i++) {
thisCookie = cookies[i];
while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1, thisCookie.length);
}
var key = thisCookie.substring(prefixLength, thisCookie.indexOf('='));
removeFromCookies(key);
while (thisCookie.charAt(0) === ' ') {
thisCookie = thisCookie.substring(1, thisCookie.length);
}
};
var getStorageType = function () {
return storageType;
};
var key = thisCookie.substring(prefixLength, thisCookie.indexOf('='));
removeFromCookies(key);
}
};
// Add a listener on scope variable to save its changes to local storage
// Return a function which when called cancels binding
var bindToScope = function (scope, key, def, lsKey) {
lsKey = lsKey || key;
var value = getFromLocalStorage(lsKey);
var getStorageType = function() {
return storageType;
};
if (value === null && isDefined(def)) {
value = def;
} else if (isObject(value) && isObject(def)) {
value = extend(def, value);
}
// Add a listener on scope variable to save its changes to local storage
// Return a function which when called cancels binding
var bindToScope = function(scope, key, def, lsKey) {
lsKey = lsKey || key;
var value = getFromLocalStorage(lsKey);
$parse(key).assign(scope, value);
if (value === null && isDefined(def)) {
value = def;
} else if (isObject(value) && isObject(def)) {
value = extend(value, def);
}
return scope.$watch(key, function (newVal) {
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
};
$parse(key).assign(scope, value);
// Return localStorageService.length
// ignore keys that not owned
var lengthOfLocalStorage = function () {
var count = 0;
var storage = $window[storageType];
for (var i = 0; i < storage.length; i++) {
if (storage.key(i).indexOf(prefix) === 0) {
count++;
}
return scope.$watch(key, function(newVal) {
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
};
// Return localStorageService.length
// ignore keys that not owned
var lengthOfLocalStorage = function() {
var count = 0;
var storage = $window[storageType];
for(var i = 0; i < storage.length; i++) {
if(storage.key(i).indexOf(prefix) === 0 ) {
count++;
}
return count;
};
}
return count;
};
return {
isSupported: browserSupportsLocalStorage,
getStorageType: getStorageType,
set: addToLocalStorage,
add: addToLocalStorage, //DEPRECATED
get: getFromLocalStorage,
keys: getKeysForLocalStorage,
remove: removeFromLocalStorage,
clearAll: clearAllFromLocalStorage,
bind: bindToScope,
deriveKey: deriveQualifiedKey,
length: lengthOfLocalStorage,
cookie: {
isSupported: browserSupportsCookies,
set: addToCookies,
add: addToCookies, //DEPRECATED
get: getFromCookies,
remove: removeFromCookies,
clearAll: clearAllFromCookies
}
};
}
];
return {
isSupported: browserSupportsLocalStorage,
getStorageType: getStorageType,
set: addToLocalStorage,
add: addToLocalStorage, //DEPRECATED
get: getFromLocalStorage,
keys: getKeysForLocalStorage,
remove: removeFromLocalStorage,
clearAll: clearAllFromLocalStorage,
bind: bindToScope,
deriveKey: deriveQualifiedKey,
length: lengthOfLocalStorage,
cookie: {
isSupported: browserSupportsCookies,
set: addToCookies,
add: addToCookies, //DEPRECATED
get: getFromCookies,
remove: removeFromCookies,
clearAll: clearAllFromCookies
}
};
}];
});
})(window, window.angular);
{
"name": "angular-local-storage-ci-dev",
"version": "0.2.4",
"version": "0.3.0",
"description": "An Angular module that gives you access to the browsers local storage",

@@ -5,0 +5,0 @@ "main": "./dist/angular-local-storage.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc