Socket
Socket
Sign inDemoInstall

@analytics/storage-utils

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@analytics/storage-utils - npm Package Compare versions

Comparing version 0.2.4 to 0.2.5

package-lock.json

8

CHANGELOG.md

@@ -6,2 +6,10 @@ # Change Log

## [0.2.5](https://github.com/DavidWells/analytics/compare/@analytics/storage-utils@0.2.4...@analytics/storage-utils@0.2.5) (2021-03-11)
**Note:** Version bump only for package @analytics/storage-utils
## [0.2.4](https://github.com/DavidWells/analytics/compare/@analytics/storage-utils@0.2.3...@analytics/storage-utils@0.2.4) (2020-07-14)

@@ -8,0 +16,0 @@

181

dist/@analytics/storage-utils.js
var analyticsUtilStorage = (function (exports) {
'use strict';
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(source, true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function cookie(name, value, ttl, path, domain, secure) {

@@ -76,16 +139,2 @@ if (typeof window === 'undefined') return;

function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function parse(input) {

@@ -121,2 +170,3 @@ var value;

var ALL = '*';
var LOCAL_STORAGE = 'localStorage';

@@ -141,3 +191,3 @@ var COOKIE = 'cookie';

if (storageType === 'all') return getAll(key);
if (storageType === ALL) return getAll(key);
/* 1. Try localStorage */

@@ -182,5 +232,11 @@

var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!key || !value) return false;
if (!key || typeof value === 'undefined') {
return;
}
var data = {};
var storageType = getStorageType(options);
var saveValue = JSON.stringify(value);
var setAll = storageType === ALL;
/* 1. Try localStorage */

@@ -190,10 +246,17 @@

// console.log('SET as localstorage', saveValue)
var _oldValue = parse(localStorage.getItem(key));
var values = {
current: value,
previous: parse(localStorage.getItem(key)) // Set LocalStorage item
};
localStorage.setItem(key, saveValue);
return {
value: value,
oldValue: _oldValue,
location: LOCAL_STORAGE
};
if (!setAll) {
return _objectSpread2({
location: LOCAL_STORAGE
}, values);
} // Set object
data[LOCAL_STORAGE] = values;
}

@@ -205,10 +268,17 @@ /* 2. Fallback to cookie */

// console.log('SET as cookie', saveValue)
var _oldValue2 = parse(getCookie(key));
var cookieValues = {
current: value,
previous: parse(getCookie(key)) // Set Cookie
};
setCookie(key, saveValue);
return {
value: value,
oldValue: _oldValue2,
location: COOKIE
};
if (!setAll) {
return _objectSpread2({
location: COOKIE
}, cookieValues);
} // Set object
data[COOKIE] = cookieValues;
}

@@ -218,9 +288,18 @@ /* 3. Fallback to window/global */

var oldValue = globalContext[key];
var globalValues = {
current: value,
previous: globalContext[key] // Set global value
};
globalContext[key] = value;
return {
value: value,
oldValue: oldValue,
location: GLOBAL
};
if (!setAll) {
return _objectSpread2({
location: GLOBAL
}, globalValues);
} // Set object
data[GLOBAL] = globalValues;
return data;
}

@@ -238,11 +317,15 @@ /**

var storageType = getStorageType(options);
var removeAll = storageType === ALL;
var locations = [];
if (useLocal(storageType)) {
if (removeAll || useLocal(storageType)) {
/* 1. Try localStorage */
localStorage.removeItem(key);
return LOCAL_STORAGE;
} else if (useCookie(storageType)) {
locations.push(LOCAL_STORAGE);
}
if (removeAll || useCookie(storageType)) {
/* 2. Fallback to cookie */
removeCookie(key);
return COOKIE;
locations.push(COOKIE);
}

@@ -252,4 +335,8 @@ /* 3. Fallback to window/global */

globalContext[key] = undefined;
return GLOBAL;
if (removeAll || useGlobal(storageType)) {
globalContext[key] = undefined;
locations.push(GLOBAL);
}
return locations;
}

@@ -261,8 +348,14 @@

function useGlobal(storage) {
return !storage || storage === GLOBAL;
}
function useLocal(storage) {
return hasStorage && (!storage || storage === LOCAL_STORAGE);
// If has localStorage and storage option not defined, or is set to 'localStorage' or '*'
return hasStorage && (!storage || storage === LOCAL_STORAGE || storage === ALL);
}
function useCookie(storage) {
return hasCookies && (!storage || storage === COOKIE);
// If has cookies and storage option not defined, or is set to 'cookies' or '*'
return hasCookies && (!storage || storage === COOKIE || storage === ALL);
}

@@ -275,2 +368,6 @@ var index = {

exports.ALL = ALL;
exports.LOCAL_STORAGE = LOCAL_STORAGE;
exports.COOKIE = COOKIE;
exports.GLOBAL = GLOBAL;
exports.getItem = getItem;

@@ -277,0 +374,0 @@ exports.setItem = setItem;

@@ -1,1 +0,1 @@

var analyticsUtilStorage=function(e){"use strict";function o(e,t,o,r,n,l){if("undefined"!=typeof window)return 1<arguments.length?document.cookie=e+"="+encodeURIComponent(t)+(o?"; expires="+new Date(+new Date+1e3*o).toUTCString()+(r?"; path="+r:"")+(n?"; domain="+n:"")+(l?"; secure":""):""):decodeURIComponent((("; "+document.cookie).split("; "+e+"=")[1]||"").split(";")[0])}function t(){try{var e="_c_";o(e,"1");var t=-1!==document.cookie.indexOf(e);return o(e,"",-1),t}catch(e){return!1}}var i=o,f=o;function r(e){return o(e,"",-1)}function n(){try{if("undefined"==typeof localStorage||"undefined"==typeof JSON)return!1;localStorage.setItem("_t_","1"),localStorage.removeItem("_t_")}catch(e){return!1}return!0}function l(e){return(l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function c(t){var o;try{void 0===(o=JSON.parse(t))&&(o=t),"true"===o&&(o=!0),"false"===o&&(o=!1),parseFloat(o)===o&&"object"!==l(o)&&(o=parseFloat(o))}catch(e){o=t}return o}var g="object"===("undefined"==typeof self?"undefined":l(self))&&self.self===self&&self||"object"===("undefined"==typeof global?"undefined":l(global))&&global.global===global&&global||void 0,d="localStorage",v="cookie",m="global",a=n(),u=t();function s(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(!e)return null;var o,r=S(t);if("all"===r)return{cookie:c(i(o=e)),localStorage:c(localStorage.getItem(o)),global:g[o]||null};if(b(r)){var n=localStorage.getItem(e);if(n||r===d)return c(n)}if(I(r)){var l=i(e);if(l||r===v)return c(l)}return g[e]||null}function p(e,t){var o=2<arguments.length&&void 0!==arguments[2]?arguments[2]:{};if(!e||!t)return!1;var r=S(o),n=JSON.stringify(t);if(b(r)){var l=c(localStorage.getItem(e));return localStorage.setItem(e,n),{value:t,oldValue:l,location:d}}if(I(r)){var a=c(i(e));return f(e,n),{value:t,oldValue:a,location:v}}var u=g[e];return{value:g[e]=t,oldValue:u,location:m}}function y(e){var t=1<arguments.length&&void 0!==arguments[1]?arguments[1]:{};if(!e)return!1;var o=S(t);return b(o)?(localStorage.removeItem(e),d):I(o)?(r(e),v):(g[e]=void 0,m)}function S(e){return"string"==typeof e?e:e.storage}function b(e){return a&&(!e||e===d)}function I(e){return u&&(!e||e===v)}var h={getItem:s,setItem:p,removeItem:y};return e.getItem=s,e.setItem=p,e.removeItem=y,e.getCookie=i,e.setCookie=f,e.removeCookie=r,e.globalContext=g,e.hasLocalStorageSupport=n,e.hasCookieSupport=t,e.default=h,e}({});
var analyticsUtilStorage=function(e){"use strict";function t(e){return(t="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e})(e)}function r(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function o(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,o)}return r}function n(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(n,!0).forEach(function(t){r(e,t,n[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(n).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))})}return e}function i(e,t,r,o,n,i){if("undefined"!=typeof window)return arguments.length>1?document.cookie=e+"="+encodeURIComponent(t)+(r?"; expires="+new Date(+new Date+1e3*r).toUTCString()+(o?"; path="+o:"")+(n?"; domain="+n:"")+(i?"; secure":""):""):decodeURIComponent((("; "+document.cookie).split("; "+e+"=")[1]||"").split(";")[0])}function u(){try{i("_c_","1");var e=-1!==document.cookie.indexOf("_c_");return i("_c_","",-1),e}catch(e){return!1}}var c=i,l=i;function a(e){return i(e,"",-1)}function f(){try{if("undefined"==typeof localStorage||"undefined"==typeof JSON)return!1;localStorage.setItem("_t_","1"),localStorage.removeItem("_t_")}catch(e){return!1}return!0}function p(e){var r;try{void 0===(r=JSON.parse(e))&&(r=e),"true"===r&&(r=!0),"false"===r&&(r=!1),parseFloat(r)===r&&"object"!==t(r)&&(r=parseFloat(r))}catch(t){r=e}return r}var s="object"===("undefined"==typeof self?"undefined":t(self))&&self.self===self&&self||"object"===("undefined"==typeof global?"undefined":t(global))&&global.global===global&&global||void 0,g="*",v="localStorage",y="cookie",b="global",m=f(),d=u();function S(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!e)return null;var r=I(t);if(r===g)return function(e){return{cookie:p(c(e)),localStorage:p(localStorage.getItem(e)),global:s[e]||null}}(e);if(j(r)){var o=localStorage.getItem(e);if(o||r===v)return p(o)}if(w(r)){var n=c(e);if(n||r===y)return p(n)}return s[e]||null}function O(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if(e&&void 0!==t){var o={},i=I(r),u=JSON.stringify(t),a=i===g;if(j(i)){var f={current:t,previous:p(localStorage.getItem(e))};if(localStorage.setItem(e,u),!a)return n({location:v},f);o[v]=f}if(w(i)){var m={current:t,previous:p(c(e))};if(l(e,u),!a)return n({location:y},m);o[y]=m}var d={current:t,previous:s[e]};return s[e]=t,a?(o[b]=d,o):n({location:b},d)}}function h(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};if(!e)return!1;var r,o=I(t),n=o===g,i=[];return(n||j(o))&&(localStorage.removeItem(e),i.push(v)),(n||w(o))&&(a(e),i.push(y)),!n&&((r=o)&&r!==b)||(s[e]=void 0,i.push(b)),i}function I(e){return"string"==typeof e?e:e.storage}function j(e){return m&&(!e||e===v||e===g)}function w(e){return d&&(!e||e===y||e===g)}var _={getItem:S,setItem:O,removeItem:h};return e.ALL=g,e.LOCAL_STORAGE=v,e.COOKIE=y,e.GLOBAL=b,e.getItem=S,e.setItem=O,e.removeItem=h,e.getCookie=c,e.setCookie=l,e.removeCookie=a,e.globalContext=s,e.hasLocalStorageSupport=f,e.hasCookieSupport=u,e.default=_,e}({});

@@ -7,2 +7,65 @@ 'use strict';

function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(source, true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function hasLocalStorage() {

@@ -25,16 +88,2 @@

function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function parse(input) {

@@ -70,2 +119,3 @@ var value;

var ALL = '*';
var LOCAL_STORAGE = 'localStorage';

@@ -90,3 +140,3 @@ var COOKIE = 'cookie';

if (storageType === 'all') return getAll(key);
if (storageType === ALL) return getAll(key);
/* 1. Try localStorage */

@@ -131,5 +181,11 @@

var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!key || !value) return false;
if (!key || typeof value === 'undefined') {
return;
}
var data = {};
var storageType = getStorageType(options);
var saveValue = JSON.stringify(value);
var setAll = storageType === ALL;
/* 1. Try localStorage */

@@ -139,10 +195,17 @@

// console.log('SET as localstorage', saveValue)
var _oldValue = parse(localStorage.getItem(key));
var values = {
current: value,
previous: parse(localStorage.getItem(key)) // Set LocalStorage item
};
localStorage.setItem(key, saveValue);
return {
value: value,
oldValue: _oldValue,
location: LOCAL_STORAGE
};
if (!setAll) {
return _objectSpread2({
location: LOCAL_STORAGE
}, values);
} // Set object
data[LOCAL_STORAGE] = values;
}

@@ -154,10 +217,17 @@ /* 2. Fallback to cookie */

// console.log('SET as cookie', saveValue)
var _oldValue2 = parse(cookieUtils.getCookie(key));
var cookieValues = {
current: value,
previous: parse(cookieUtils.getCookie(key)) // Set Cookie
};
cookieUtils.setCookie(key, saveValue);
return {
value: value,
oldValue: _oldValue2,
location: COOKIE
};
if (!setAll) {
return _objectSpread2({
location: COOKIE
}, cookieValues);
} // Set object
data[COOKIE] = cookieValues;
}

@@ -167,9 +237,18 @@ /* 3. Fallback to window/global */

var oldValue = globalContext[key];
var globalValues = {
current: value,
previous: globalContext[key] // Set global value
};
globalContext[key] = value;
return {
value: value,
oldValue: oldValue,
location: GLOBAL
};
if (!setAll) {
return _objectSpread2({
location: GLOBAL
}, globalValues);
} // Set object
data[GLOBAL] = globalValues;
return data;
}

@@ -187,11 +266,15 @@ /**

var storageType = getStorageType(options);
var removeAll = storageType === ALL;
var locations = [];
if (useLocal(storageType)) {
if (removeAll || useLocal(storageType)) {
/* 1. Try localStorage */
localStorage.removeItem(key);
return LOCAL_STORAGE;
} else if (useCookie(storageType)) {
locations.push(LOCAL_STORAGE);
}
if (removeAll || useCookie(storageType)) {
/* 2. Fallback to cookie */
cookieUtils.removeCookie(key);
return COOKIE;
locations.push(COOKIE);
}

@@ -201,4 +284,8 @@ /* 3. Fallback to window/global */

globalContext[key] = undefined;
return GLOBAL;
if (removeAll || useGlobal(storageType)) {
globalContext[key] = undefined;
locations.push(GLOBAL);
}
return locations;
}

@@ -210,8 +297,14 @@

function useGlobal(storage) {
return !storage || storage === GLOBAL;
}
function useLocal(storage) {
return hasStorage && (!storage || storage === LOCAL_STORAGE);
// If has localStorage and storage option not defined, or is set to 'localStorage' or '*'
return hasStorage && (!storage || storage === LOCAL_STORAGE || storage === ALL);
}
function useCookie(storage) {
return hasCookies && (!storage || storage === COOKIE);
// If has cookies and storage option not defined, or is set to 'cookies' or '*'
return hasCookies && (!storage || storage === COOKIE || storage === ALL);
}

@@ -228,2 +321,6 @@ var index = {

exports.hasCookieSupport = cookieUtils.hasCookieSupport;
exports.ALL = ALL;
exports.LOCAL_STORAGE = LOCAL_STORAGE;
exports.COOKIE = COOKIE;
exports.GLOBAL = GLOBAL;
exports.getItem = getItem;

@@ -230,0 +327,0 @@ exports.setItem = setItem;

import { hasCookieSupport, getCookie, setCookie, removeCookie } from '@analytics/cookie-utils';
export { getCookie, setCookie, removeCookie, hasCookieSupport } from '@analytics/cookie-utils';
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(source, true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function hasLocalStorage() {

@@ -21,16 +84,2 @@

function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function parse(input) {

@@ -66,2 +115,3 @@ var value;

var ALL = '*';
var LOCAL_STORAGE = 'localStorage';

@@ -86,3 +136,3 @@ var COOKIE = 'cookie';

if (storageType === 'all') return getAll(key);
if (storageType === ALL) return getAll(key);
/* 1. Try localStorage */

@@ -127,5 +177,11 @@

var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!key || !value) return false;
if (!key || typeof value === 'undefined') {
return;
}
var data = {};
var storageType = getStorageType(options);
var saveValue = JSON.stringify(value);
var setAll = storageType === ALL;
/* 1. Try localStorage */

@@ -135,10 +191,17 @@

// console.log('SET as localstorage', saveValue)
var _oldValue = parse(localStorage.getItem(key));
var values = {
current: value,
previous: parse(localStorage.getItem(key)) // Set LocalStorage item
};
localStorage.setItem(key, saveValue);
return {
value: value,
oldValue: _oldValue,
location: LOCAL_STORAGE
};
if (!setAll) {
return _objectSpread2({
location: LOCAL_STORAGE
}, values);
} // Set object
data[LOCAL_STORAGE] = values;
}

@@ -150,10 +213,17 @@ /* 2. Fallback to cookie */

// console.log('SET as cookie', saveValue)
var _oldValue2 = parse(getCookie(key));
var cookieValues = {
current: value,
previous: parse(getCookie(key)) // Set Cookie
};
setCookie(key, saveValue);
return {
value: value,
oldValue: _oldValue2,
location: COOKIE
};
if (!setAll) {
return _objectSpread2({
location: COOKIE
}, cookieValues);
} // Set object
data[COOKIE] = cookieValues;
}

@@ -163,9 +233,18 @@ /* 3. Fallback to window/global */

var oldValue = globalContext[key];
var globalValues = {
current: value,
previous: globalContext[key] // Set global value
};
globalContext[key] = value;
return {
value: value,
oldValue: oldValue,
location: GLOBAL
};
if (!setAll) {
return _objectSpread2({
location: GLOBAL
}, globalValues);
} // Set object
data[GLOBAL] = globalValues;
return data;
}

@@ -183,11 +262,15 @@ /**

var storageType = getStorageType(options);
var removeAll = storageType === ALL;
var locations = [];
if (useLocal(storageType)) {
if (removeAll || useLocal(storageType)) {
/* 1. Try localStorage */
localStorage.removeItem(key);
return LOCAL_STORAGE;
} else if (useCookie(storageType)) {
locations.push(LOCAL_STORAGE);
}
if (removeAll || useCookie(storageType)) {
/* 2. Fallback to cookie */
removeCookie(key);
return COOKIE;
locations.push(COOKIE);
}

@@ -197,4 +280,8 @@ /* 3. Fallback to window/global */

globalContext[key] = undefined;
return GLOBAL;
if (removeAll || useGlobal(storageType)) {
globalContext[key] = undefined;
locations.push(GLOBAL);
}
return locations;
}

@@ -206,8 +293,14 @@

function useGlobal(storage) {
return !storage || storage === GLOBAL;
}
function useLocal(storage) {
return hasStorage && (!storage || storage === LOCAL_STORAGE);
// If has localStorage and storage option not defined, or is set to 'localStorage' or '*'
return hasStorage && (!storage || storage === LOCAL_STORAGE || storage === ALL);
}
function useCookie(storage) {
return hasCookies && (!storage || storage === COOKIE);
// If has cookies and storage option not defined, or is set to 'cookies' or '*'
return hasCookies && (!storage || storage === COOKIE || storage === ALL);
}

@@ -221,2 +314,2 @@ var index = {

export default index;
export { getItem, setItem, removeItem, globalContext, hasLocalStorage as hasLocalStorageSupport };
export { ALL, LOCAL_STORAGE, COOKIE, GLOBAL, getItem, setItem, removeItem, globalContext, hasLocalStorage as hasLocalStorageSupport };

@@ -7,2 +7,65 @@ 'use strict';

function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(source, true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function hasLocalStorage() {

@@ -26,16 +89,2 @@ return false;

function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function parse(input) {

@@ -71,2 +120,3 @@ var value;

var ALL = '*';
var LOCAL_STORAGE = 'localStorage';

@@ -91,3 +141,3 @@ var COOKIE = 'cookie';

if (storageType === 'all') return getAll(key);
if (storageType === ALL) return getAll(key);
/* 1. Try localStorage */

@@ -132,5 +182,11 @@

var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!key || !value) return false;
if (!key || typeof value === 'undefined') {
return;
}
var data = {};
var storageType = getStorageType(options);
var saveValue = JSON.stringify(value);
var setAll = storageType === ALL;
/* 1. Try localStorage */

@@ -140,10 +196,17 @@

// console.log('SET as localstorage', saveValue)
var _oldValue = parse(localStorage.getItem(key));
var values = {
current: value,
previous: parse(localStorage.getItem(key)) // Set LocalStorage item
};
localStorage.setItem(key, saveValue);
return {
value: value,
oldValue: _oldValue,
location: LOCAL_STORAGE
};
if (!setAll) {
return _objectSpread2({
location: LOCAL_STORAGE
}, values);
} // Set object
data[LOCAL_STORAGE] = values;
}

@@ -155,10 +218,17 @@ /* 2. Fallback to cookie */

// console.log('SET as cookie', saveValue)
var _oldValue2 = parse(cookieUtils.getCookie(key));
var cookieValues = {
current: value,
previous: parse(cookieUtils.getCookie(key)) // Set Cookie
};
cookieUtils.setCookie(key, saveValue);
return {
value: value,
oldValue: _oldValue2,
location: COOKIE
};
if (!setAll) {
return _objectSpread2({
location: COOKIE
}, cookieValues);
} // Set object
data[COOKIE] = cookieValues;
}

@@ -168,9 +238,18 @@ /* 3. Fallback to window/global */

var oldValue = globalContext[key];
var globalValues = {
current: value,
previous: globalContext[key] // Set global value
};
globalContext[key] = value;
return {
value: value,
oldValue: oldValue,
location: GLOBAL
};
if (!setAll) {
return _objectSpread2({
location: GLOBAL
}, globalValues);
} // Set object
data[GLOBAL] = globalValues;
return data;
}

@@ -188,11 +267,15 @@ /**

var storageType = getStorageType(options);
var removeAll = storageType === ALL;
var locations = [];
if (useLocal(storageType)) {
if (removeAll || useLocal(storageType)) {
/* 1. Try localStorage */
localStorage.removeItem(key);
return LOCAL_STORAGE;
} else if (useCookie(storageType)) {
locations.push(LOCAL_STORAGE);
}
if (removeAll || useCookie(storageType)) {
/* 2. Fallback to cookie */
cookieUtils.removeCookie(key);
return COOKIE;
locations.push(COOKIE);
}

@@ -202,4 +285,8 @@ /* 3. Fallback to window/global */

globalContext[key] = undefined;
return GLOBAL;
if (removeAll || useGlobal(storageType)) {
globalContext[key] = undefined;
locations.push(GLOBAL);
}
return locations;
}

@@ -211,8 +298,14 @@

function useGlobal(storage) {
return !storage || storage === GLOBAL;
}
function useLocal(storage) {
return hasStorage && (!storage || storage === LOCAL_STORAGE);
// If has localStorage and storage option not defined, or is set to 'localStorage' or '*'
return hasStorage && (!storage || storage === LOCAL_STORAGE || storage === ALL);
}
function useCookie(storage) {
return hasCookies && (!storage || storage === COOKIE);
// If has cookies and storage option not defined, or is set to 'cookies' or '*'
return hasCookies && (!storage || storage === COOKIE || storage === ALL);
}

@@ -229,2 +322,6 @@ var index = {

exports.hasCookieSupport = cookieUtils.hasCookieSupport;
exports.ALL = ALL;
exports.LOCAL_STORAGE = LOCAL_STORAGE;
exports.COOKIE = COOKIE;
exports.GLOBAL = GLOBAL;
exports.getItem = getItem;

@@ -231,0 +328,0 @@ exports.setItem = setItem;

import { hasCookieSupport, getCookie, setCookie, removeCookie } from '@analytics/cookie-utils';
export { getCookie, setCookie, removeCookie, hasCookieSupport } from '@analytics/cookie-utils';
function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(source, true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function hasLocalStorage() {

@@ -22,16 +85,2 @@ return false;

function _typeof(obj) {
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function parse(input) {

@@ -67,2 +116,3 @@ var value;

var ALL = '*';
var LOCAL_STORAGE = 'localStorage';

@@ -87,3 +137,3 @@ var COOKIE = 'cookie';

if (storageType === 'all') return getAll(key);
if (storageType === ALL) return getAll(key);
/* 1. Try localStorage */

@@ -128,5 +178,11 @@

var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
if (!key || !value) return false;
if (!key || typeof value === 'undefined') {
return;
}
var data = {};
var storageType = getStorageType(options);
var saveValue = JSON.stringify(value);
var setAll = storageType === ALL;
/* 1. Try localStorage */

@@ -136,10 +192,17 @@

// console.log('SET as localstorage', saveValue)
var _oldValue = parse(localStorage.getItem(key));
var values = {
current: value,
previous: parse(localStorage.getItem(key)) // Set LocalStorage item
};
localStorage.setItem(key, saveValue);
return {
value: value,
oldValue: _oldValue,
location: LOCAL_STORAGE
};
if (!setAll) {
return _objectSpread2({
location: LOCAL_STORAGE
}, values);
} // Set object
data[LOCAL_STORAGE] = values;
}

@@ -151,10 +214,17 @@ /* 2. Fallback to cookie */

// console.log('SET as cookie', saveValue)
var _oldValue2 = parse(getCookie(key));
var cookieValues = {
current: value,
previous: parse(getCookie(key)) // Set Cookie
};
setCookie(key, saveValue);
return {
value: value,
oldValue: _oldValue2,
location: COOKIE
};
if (!setAll) {
return _objectSpread2({
location: COOKIE
}, cookieValues);
} // Set object
data[COOKIE] = cookieValues;
}

@@ -164,9 +234,18 @@ /* 3. Fallback to window/global */

var oldValue = globalContext[key];
var globalValues = {
current: value,
previous: globalContext[key] // Set global value
};
globalContext[key] = value;
return {
value: value,
oldValue: oldValue,
location: GLOBAL
};
if (!setAll) {
return _objectSpread2({
location: GLOBAL
}, globalValues);
} // Set object
data[GLOBAL] = globalValues;
return data;
}

@@ -184,11 +263,15 @@ /**

var storageType = getStorageType(options);
var removeAll = storageType === ALL;
var locations = [];
if (useLocal(storageType)) {
if (removeAll || useLocal(storageType)) {
/* 1. Try localStorage */
localStorage.removeItem(key);
return LOCAL_STORAGE;
} else if (useCookie(storageType)) {
locations.push(LOCAL_STORAGE);
}
if (removeAll || useCookie(storageType)) {
/* 2. Fallback to cookie */
removeCookie(key);
return COOKIE;
locations.push(COOKIE);
}

@@ -198,4 +281,8 @@ /* 3. Fallback to window/global */

globalContext[key] = undefined;
return GLOBAL;
if (removeAll || useGlobal(storageType)) {
globalContext[key] = undefined;
locations.push(GLOBAL);
}
return locations;
}

@@ -207,8 +294,14 @@

function useGlobal(storage) {
return !storage || storage === GLOBAL;
}
function useLocal(storage) {
return hasStorage && (!storage || storage === LOCAL_STORAGE);
// If has localStorage and storage option not defined, or is set to 'localStorage' or '*'
return hasStorage && (!storage || storage === LOCAL_STORAGE || storage === ALL);
}
function useCookie(storage) {
return hasCookies && (!storage || storage === COOKIE);
// If has cookies and storage option not defined, or is set to 'cookies' or '*'
return hasCookies && (!storage || storage === COOKIE || storage === ALL);
}

@@ -222,2 +315,2 @@ var index = {

export default index;
export { getItem, setItem, removeItem, globalContext, hasLocalStorage as hasLocalStorageSupport };
export { ALL, LOCAL_STORAGE, COOKIE, GLOBAL, getItem, setItem, removeItem, globalContext, hasLocalStorage as hasLocalStorageSupport };
{
"name": "@analytics/storage-utils",
"version": "0.2.4",
"version": "0.2.5",
"description": "Storage utilities for saving values in browser",

@@ -51,3 +51,3 @@ "author": "David Wells",

},
"gitHead": "6235efa2a3431d781a29a91c82e4aeb2b2aec1d9"
"gitHead": "54924261b60e44fa00841b8b3e740918d2e660c5"
}

@@ -0,51 +1,83 @@

<!--
title: Storage Utils
pageTitle: Storage Utils
description: Utility library for persisting data
-->
# Analytics Storage Utils
Storage utilities for [analytics](https://www.npmjs.com/package/analytics)
Stand alone storage utilities used in [analytics](https://www.npmjs.com/package/analytics)
Storage tries to use `localStorage` then `cookies` then defaults to `global` window.
By default, `@analytics/storage-utils` will persist values in browser in this order:
It's also possible to specify exactly where to set, get, and remove data from with the `options` parameter.
1. `localStorage`
2. If no `localStorage`, use `cookies`
3. If no `cookies`, use `global` window
## `getItem`
If you want to specify which storage mechanism to use, use the `options` parameter.
## `setItem`
Set a value.
```js
import { getItem } from '@analytics/storage-utils'
import { setItem } from '@analytics/storage-utils'
// Default lookup, will try resolve value from `localStorage` -> `cookies` -> `global`
const value = getItem('key')
/**
* Basic usage
*/
// Get value from specifically localStorage
const getLSValue = getItem('key', 'localStorage')
/* Save value to `localStorage` or `cookies` or `global` */
setItem('key', 'value')
// returns { value: "value", oldValue: "old", location: "localStorage" }
// Get value from specifically a cookie
const getLSValue = getItem('key', 'cookie')
/** Setting values to specific location */
// Get value from specifically the global window (or global this in Node)
const getLSValue = getItem('key', 'global')
/* Set value to specifically localStorage */
setItem('key', 'otherValue', 'localStorage')
// setItem('key', 'otherValue', { storage: 'localStorage' })
// returns { value: "otherValue", oldValue: "value", location: "localStorage" }
// Get value from all locations
const valueObj = getItem('otherKey', 'all')
// {cookie: undefined, localStorage: "hahaha", global: null}
/* Set value to specifically cookie */
setItem('keyTwo', 'cookieVal', 'cookie')
// setItem('keyTwo', 'cookieVal', { storage: 'cookie' })
// returns { value: "cookieVal", oldValue: "null", location: "cookie" }
/* Set value from specifically the global window (or global this in node.js) */
setItem('keyThree', 'xyz', 'global')
// setItem('keyThree', 'xyz', { storage: 'global' })
// returns { value: "cookieVal", oldValue: "null", location: "cookie" }
```
## `setItem`
## `getItem`
Get a value.
```js
import { setItem } from '@analytics/storage-utils'
import { getItem } from '@analytics/storage-utils'
// Will try save value to `localStorage` -> `cookies` -> `global`
setItem('key', 'value')
// -> { value: "value", oldValue: "old", location: "localStorage" }
/* Basic usage */
// Set value to specifically localStorage
setItem('key', 'otherValue', 'localStorage')
// -> { value: "otherValue", oldValue: "value", location: "localStorage" }
/* Lookup value from `localStorage` or `cookies` or `global` */
const value = getItem('key')
// Set value from specifically a cookie
setItem('keyTwo', 'cookieVal', 'cookie')
// -> { value: "cookieVal", oldValue: "null", location: "cookie" }
/**
* Getting values to specific location
*/
// Set value from specifically the global window (or global this in Node)
setItem('keyThree', 'xyz', 'global')
// -> { value: "xyz", oldValue: "foobar", location: "global" }
// Get value to specifically localStorage
const getLSValue = getItem('key', 'localStorage')
/* Get value to specifically cookie */
const getLSValue = getItem('key', 'cookie')
// getItem('key', { storage: 'cookie' })
/* Get value from specifically the global window (or global this in node.js) */
const getLSValue = getItem('key', 'global')
// getItem('key', { storage: 'global' })
/* Get value from all locations */
const valueObj = getItem('otherKey', '*')
// returns { cookie: undefined, localStorage: "hahaha", global: null }
```

@@ -55,20 +87,25 @@

Remote a value.
```js
import { removeItem } from '@analytics/storage-utils'
// Will try save value to `localStorage` -> `cookies` -> `global`
setItem('key')
// -> { location: "localStorage" }
/* Basic usage */
// Set value to specifically localStorage
setItem('key', 'otherValue', 'localStorage')
// -> { location: "localStorage" }
// Will try remove value from `localStorage` -> `cookies` -> `global`
removeItem('key')
// Set value from specifically a cookie
setItem('keyTwo', 'cookieVal', 'cookie')
// -> { value: "cookieVal", oldValue: "null", location: "cookie" }
/** Removing values to specific location */
// Set value from specifically the global window (or global this in Node)
setItem('keyThree', 'xyz', 'global')
// -> { value: "xyz", oldValue: "foobar", location: "global" }
/* Remove value to specifically localStorage */
removeItem('key', 'localStorage')
// removeItem('key', { storage: 'localStorage' })
/* Remove value to specifically cookie */
removeItem('keyTwo', 'cookie')
// removeItem('keyTwo', { storage: 'cookie' })
/* Remove value to specifically global */
removeItem('keyThree', 'global')
// removeItem('keyThree', { storage: 'global' })
```
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