Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
@doodle/tracking / dist / esm / src / helpers / storage.js
Size: Mime:
import { objectSpread2 as _objectSpread2 } from '../../_virtual/_rollupPluginBabelHelpers.js';
import js_cookie from '../../node_modules/js-cookie/src/js.cookie.js';
import { areCookiesEnabled, isLocalStorageEnabled } from './capabilities.js';

var getCookie = function getCookie() {
  var name = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

  if (!areCookiesEnabled()) {
    return null;
  }

  if (name) {
    return js_cookie.get(name);
  }

  return null;
};
/**
 * Returns the name of all cookies. If a filter is provided, only the
 * cookie names matching the filter are returned. Filters are ran through `str.match`.
 * @param {string} filter - A regular expression or string used for filtering the results.
 * @returns {string[]|*[]} - The name of matching cookies.
 */


var getCookieNames = function getCookieNames() {
  var filter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

  if (!areCookiesEnabled()) {
    return [];
  }

  var cookieNames = Object.keys(js_cookie.get());

  if (filter) {
    return cookieNames.filter(function (name) {
      return name.match(filter);
    });
  }

  return cookieNames;
};
/**
 * Removes a cookie by its name.
 * @param {string} name - The name of the cookie
 * @param {string|null} domain - Optionally, the domain the cookie is scoped to.
 */


var removeCookie = function removeCookie(name) {
  var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

  if (!areCookiesEnabled() || !name) {
    return;
  } // Removing cookies may require additional attributes such as domain or a path.
  // From the documentation: https://github.com/js-cookie/js-cookie
  // > IMPORTANT! When deleting a cookie and you're not relying on the default attributes, you must pass the exact same path and domain attributes that were used to set the cookie.


  var attributes = {};

  if (domain) {
    attributes.domain = domain;
  } // As cookies can be scoped on path, iterate through the path hierarchy and try to delete
  // the cookie with the given path


  var pathHierarchy = window.location.pathname.split('/').filter(function (path) {
    return path;
  });

  for (var i = 0; i < pathHierarchy.length; i += 1) {
    // Construct the full path until the current hierarchical point
    // eg: ['auth', 'login'] => '/auth/login'
    var path = "/".concat(pathHierarchy.slice(0, i + 1).join('/')); // Remove the cookie with path and optionally set domain

    js_cookie.remove(name, _objectSpread2(_objectSpread2({}, attributes), {}, {
      path: path
    })); // If the domain is provided, still try to delete the cookie with the default as a fallback.

    if (domain) {
      js_cookie.remove(name, {
        path: path
      });
    }
  } // Cookies without path, optionally with domain


  js_cookie.remove(name, attributes); // Again, attempt to remove the cookie by falling back on defaults.

  if (domain) {
    js_cookie.remove(name);
  }
};
/**
 * Removes all cookies that match the filters.
 * @param {string[]|RegExp[]} filters
 * @param {string|null} domain - Optionally, the domain the cookie is scoped to.
 */


var removeCookies = function removeCookies() {
  var filters = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var domain = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;

  if (!filters || filters.length === 0) {
    return;
  }

  filters.forEach(function (filter) {
    var cookieNames = getCookieNames(filter);
    cookieNames.forEach(function (name) {
      return removeCookie(name, domain);
    });
  });
};
/**
 * Returns the name of all items in LocalStorage. If a filter is provided, only the
 * items names matching the filter are returned. Filters are ran through `str.match`.
 * @param {string} filter - A regular expression or string used for filtering the results.
 * @returns {string[]|*[]} - The name of matching LocalStorage items..
 */


var getLocalStorageItemNames = function getLocalStorageItemNames() {
  var filter = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;

  if (!isLocalStorageEnabled()) {
    return [];
  }

  var itemNames = Object.keys(localStorage);

  if (filter) {
    return itemNames.filter(function (name) {
      return name.match(filter);
    });
  }

  return itemNames;
};
/**
 * Remove items from local storage by name.
 * @param {string} name - The name if the item to remove.
 */


var removeLocalStorageItem = function removeLocalStorageItem(name) {
  if (!isLocalStorageEnabled() || !name) {
    return;
  }

  localStorage.removeItem(name);
};
/**
 * Removes all items in local storage that match the filters.
 * @param {string[]|RegExp[]} filters
 */


var removeLocalStorageItems = function removeLocalStorageItems() {
  var filters = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];

  if (!filters || filters.length === 0) {
    return;
  }

  filters.forEach(function (filter) {
    var itemNames = getLocalStorageItemNames(filter);
    itemNames.forEach(function (name) {
      return removeLocalStorageItem(name);
    });
  });
};

export { getCookie, getCookieNames, getLocalStorageItemNames, removeCookie, removeCookies, removeLocalStorageItem, removeLocalStorageItems };
//# sourceMappingURL=storage.js.map