analytics-utils
Advanced tools
Comparing version 0.0.11 to 0.0.12
'use strict'; | ||
var inBrowser = require('./inBrowser'); | ||
var hasCookies = require('./storage/hasCookies'); | ||
var cookiesSupported = exports.cookiesSupported = hasCookies(); | ||
function setCookie(name, value, days) { | ||
if (!inBrowser) { | ||
return false; | ||
} | ||
if (!cookiesSupported) return false; | ||
var expires = ''; | ||
@@ -20,5 +20,3 @@ if (days) { | ||
function getCookie(name) { | ||
if (!inBrowser) { | ||
return false; | ||
} | ||
if (!cookiesSupported) return false; | ||
var find = name + '='; | ||
@@ -40,4 +38,5 @@ var allCookies = document.cookie.split(';'); | ||
function removeCookie(name) { | ||
if (!cookiesSupported) return false; | ||
setCookie(name, '', -1); | ||
} | ||
exports.removeCookie = removeCookie; |
@@ -14,2 +14,3 @@ 'use strict'; | ||
var removeCookie = _cookie.removeCookie; | ||
var cookiesSupported = _cookie.cookiesSupported; | ||
@@ -19,57 +20,89 @@ | ||
function getItem(key) { | ||
if (!inBrowser) { | ||
return false; | ||
} | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {[type]} key - key of item to get | ||
* @param {Object} opts - (optional) | ||
* @param {String} opts.storage - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
function getItem(key, opts) { | ||
if (!inBrowser || !key) return false; | ||
var options = opts || {}; | ||
var storage = options.storage; | ||
// Try localStorage | ||
if (hasLocalStorage) { | ||
return parse(localStorage.getItem(key)); | ||
if (hasLocalStorage && (!storage || storage === 'localStorage')) { | ||
var value = localStorage.getItem(key); | ||
if (value || storage === 'localStorage') return parse(value); | ||
} | ||
// Fallback to cookie | ||
try { | ||
return parse(getCookie(key)); | ||
} catch (e) { | ||
// Fallback to window | ||
return window[key] || null; | ||
if (cookiesSupported && (!storage || storage === 'cookie')) { | ||
var _value = getCookie(key); | ||
if (_value || storage === 'cookie') return parse(_value); | ||
} | ||
// Fallback to window | ||
return window[key] || null; | ||
} | ||
exports.getItem = getItem; | ||
function setItem(key, value) { | ||
if (!inBrowser) { | ||
return false; | ||
} | ||
exports.getItem = getItem; /** | ||
* Store values in localStorage, cookie, or window | ||
* @param {String} key - key of item to set | ||
* @param {Any} value - value of item to set | ||
* @param {Object} opts - (optional) | ||
* @param {String} opts.storage - Define type of storage to set to. | ||
*/ | ||
function setItem(key, value, opts) { | ||
if (!inBrowser || !key || !value) return false; | ||
var saveValue = JSON.stringify(value); | ||
// Try localStorage | ||
if (hasLocalStorage) { | ||
return localStorage.setItem(key, saveValue); | ||
var options = opts || {}; | ||
var storage = options.storage; | ||
// 1. Try localStorage | ||
if (hasLocalStorage && (!storage || storage === 'localStorage')) { | ||
// console.log('SET as localstorage', saveValue) | ||
var _oldValue = parse(localStorage.getItem(key)); | ||
localStorage.setItem(key, saveValue); | ||
return { value: value, oldValue: _oldValue, type: 'localStorage' }; | ||
} | ||
// Fallback to cookie | ||
try { | ||
// 2. Fallback to cookie | ||
if (cookiesSupported && (!storage || storage === 'cookie')) { | ||
// console.log('SET as cookie', saveValue) | ||
var _oldValue2 = parse(getCookie(key)); | ||
setCookie(key, saveValue); | ||
} catch (e) { | ||
// Fallback to window | ||
window[key] = value; | ||
return { value: value, oldValue: _oldValue2, type: 'cookie' }; | ||
} | ||
return value; | ||
// 3. Fallback to window | ||
var oldValue = window[key]; | ||
// console.log('SET as window', value) | ||
window[key] = value; | ||
return { value: value, oldValue: oldValue, type: 'window' }; | ||
} | ||
exports.setItem = setItem; | ||
function removeItem(key) { | ||
if (!inBrowser) { | ||
return false; | ||
exports.setItem = setItem; /** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {String} key - key of item to set | ||
* @param {Object} opts - (optional) | ||
* @param {String} opts.storage - Define type of storage to set to. | ||
*/ | ||
function removeItem(key, opts) { | ||
if (!inBrowser || !key) return false; | ||
var options = opts || {}; | ||
var storage = options.storage; | ||
// 1. Try localStorage | ||
if (hasLocalStorage && (!storage || storage === 'localStorage')) { | ||
localStorage.removeItem(key); | ||
return null; | ||
} | ||
// Try localStorage | ||
if (hasLocalStorage) { | ||
return localStorage.removeItem(key); | ||
} | ||
// Fallback to Cookie | ||
try { | ||
// 2. Fallback to cookie | ||
if (cookiesSupported && (!storage || storage === 'cookie')) { | ||
removeCookie(key); | ||
} catch (e) { | ||
// Fallback to window | ||
window[key] = null; | ||
return null; | ||
} | ||
// 3. Fallback to window | ||
window[key] = null; | ||
return null; | ||
} | ||
exports.removeItem = removeItem; |
@@ -1,7 +0,7 @@ | ||
import inBrowser from './inBrowser' | ||
import hasCookies from './storage/hasCookies' | ||
export const cookiesSupported = hasCookies() | ||
export function setCookie(name, value, days) { | ||
if (!inBrowser) { | ||
return false | ||
} | ||
if (!cookiesSupported) return false | ||
let expires = '' | ||
@@ -17,5 +17,3 @@ if (days) { | ||
export function getCookie(name) { | ||
if (!inBrowser) { | ||
return false | ||
} | ||
if (!cookiesSupported) return false | ||
const find = `${name}=` | ||
@@ -36,3 +34,4 @@ const allCookies = document.cookie.split(';') | ||
export function removeCookie(name) { | ||
if (!cookiesSupported) return false | ||
setCookie(name, '', -1) | ||
} |
import inBrowser from '../inBrowser' | ||
import parse from './parse' | ||
import checkLocalStorage from './hasLocalStorage' | ||
import { getCookie, setCookie, removeCookie } from '../cookie' | ||
import { getCookie, setCookie, removeCookie, cookiesSupported } from '../cookie' | ||
const hasLocalStorage = checkLocalStorage() | ||
export function getItem(key) { | ||
if (!inBrowser) { | ||
return false | ||
} | ||
/** | ||
* Get storage item from localStorage, cookie, or window | ||
* @param {[type]} key - key of item to get | ||
* @param {Object} opts - (optional) | ||
* @param {String} opts.storage - Define type of storage to pull from. | ||
* @return {Any} the value of key | ||
*/ | ||
export function getItem(key, opts) { | ||
if (!inBrowser || !key) return false | ||
const options = opts || {} | ||
const { storage } = options | ||
// Try localStorage | ||
if (hasLocalStorage) { | ||
return parse(localStorage.getItem(key)) | ||
if (hasLocalStorage && (!storage || storage === 'localStorage')) { | ||
const value = localStorage.getItem(key) | ||
if (value || storage === 'localStorage') return parse(value) | ||
} | ||
// Fallback to cookie | ||
try { | ||
return parse(getCookie(key)) | ||
} catch (e) { | ||
// Fallback to window | ||
return window[key] || null | ||
if (cookiesSupported && (!storage || storage === 'cookie')) { | ||
const value = getCookie(key) | ||
if (value || storage === 'cookie') return parse(value) | ||
} | ||
// Fallback to window | ||
return window[key] || null | ||
} | ||
export function setItem(key, value) { | ||
if (!inBrowser) { | ||
return false | ||
} | ||
/** | ||
* Store values in localStorage, cookie, or window | ||
* @param {String} key - key of item to set | ||
* @param {Any} value - value of item to set | ||
* @param {Object} opts - (optional) | ||
* @param {String} opts.storage - Define type of storage to set to. | ||
*/ | ||
export function setItem(key, value, opts) { | ||
if (!inBrowser || !key || !value) return false | ||
const saveValue = JSON.stringify(value) | ||
// Try localStorage | ||
if (hasLocalStorage) { | ||
return localStorage.setItem(key, saveValue) | ||
const options = opts || {} | ||
const { storage } = options | ||
// 1. Try localStorage | ||
if (hasLocalStorage && (!storage || storage === 'localStorage')) { | ||
// console.log('SET as localstorage', saveValue) | ||
const oldValue = parse(localStorage.getItem(key)) | ||
localStorage.setItem(key, saveValue) | ||
return { value, oldValue, type: 'localStorage' } | ||
} | ||
// Fallback to cookie | ||
try { | ||
// 2. Fallback to cookie | ||
if (cookiesSupported && (!storage || storage === 'cookie')) { | ||
// console.log('SET as cookie', saveValue) | ||
const oldValue = parse(getCookie(key)) | ||
setCookie(key, saveValue) | ||
} catch (e) { | ||
// Fallback to window | ||
window[key] = value | ||
return { value, oldValue, type: 'cookie' } | ||
} | ||
return value | ||
// 3. Fallback to window | ||
const oldValue = window[key] | ||
// console.log('SET as window', value) | ||
window[key] = value | ||
return { value, oldValue, type: 'window' } | ||
} | ||
export function removeItem(key) { | ||
if (!inBrowser) { | ||
return false | ||
/** | ||
* Remove values from localStorage, cookie, or window | ||
* @param {String} key - key of item to set | ||
* @param {Object} opts - (optional) | ||
* @param {String} opts.storage - Define type of storage to set to. | ||
*/ | ||
export function removeItem(key, opts) { | ||
if (!inBrowser || !key) return false | ||
const options = opts || {} | ||
const { storage } = options | ||
// 1. Try localStorage | ||
if (hasLocalStorage && (!storage || storage === 'localStorage')) { | ||
localStorage.removeItem(key) | ||
return null | ||
} | ||
// Try localStorage | ||
if (hasLocalStorage) { | ||
return localStorage.removeItem(key) | ||
} | ||
// Fallback to Cookie | ||
try { | ||
// 2. Fallback to cookie | ||
if (cookiesSupported && (!storage || storage === 'cookie')) { | ||
removeCookie(key) | ||
} catch (e) { | ||
// Fallback to window | ||
window[key] = null | ||
return null | ||
} | ||
// 3. Fallback to window | ||
window[key] = null | ||
return null | ||
} |
{ | ||
"name": "analytics-utils", | ||
"version": "0.0.11", | ||
"version": "0.0.12", | ||
"description": "", | ||
@@ -50,3 +50,3 @@ "main": "dist/index.js", | ||
}, | ||
"gitHead": "0eae197f84f1e22f020b5e515e59954d2fee784e" | ||
"gitHead": "b27d34da5edb579ab9c8747274305f6f15d20afe" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
243456
46
6954