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    
Size: Mime:
const ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000;

const storage = {
  /**
   * Reads a cookie and returns its value. Defaults to undefined.
   * @param {string} key The key of the cookie to retrieve
   * @param {() -> string} [options.getCookie] An optional function used to read the cookie. `document.cookie` will be used otherwise.
   * @returns {string|undefined}
   */
  getCookie(key, options = {}) {
    const cookies = ((options.getCookie && options.getCookie()) || document.cookie).split('; ');

    return cookies.reduce((result, cookie) => {
      const searchKey = `${key}=`;

      if (cookie.indexOf(searchKey) === 0) {
        return cookie.substring(searchKey.length);
      }

      return result;
    }, undefined);
  },

  /**
   * @param {string} token The token value
   * @param {string} domain The domain in which to set the cookie
   */
  setCookie(key, value, expirationInDays, domain) {
    let cookie = `${key}=${value};path=/`;
    if (expirationInDays) {
      const expires = new Date(new Date().getTime() + ONE_DAY_IN_MILLIS * expirationInDays);
      cookie += `;expires=${expires.toUTCString()})`;
    }
    if (domain) {
      cookie += `;domain=${encodeURIComponent(domain)}`;
    }
    document.cookie = cookie;
  },
};

export default storage;