简易封装document.cookie辅助函数

发布时间 2023-03-26 19:09:39作者: 樊顺

MDN官网提供的cookie操作的辅助函数:

document.cookie

MDN document.cookie读写器函数(源码):

/*\
|*|
|*|  :: cookies.js ::
|*|
|*|  A complete cookies reader/writer framework with full unicode support.
|*|
|*|  https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|*|
|*|  This framework is released under the GNU Public License, version 3 or later.
|*|  http://www.gnu.org/licenses/gpl-3.0-standalone.html
|*|
|*|  Syntaxes:
|*|
|*|  * docCookies.setItem(name, value[, end[, path[, domain[, secure]]]])
|*|  * docCookies.getItem(name)
|*|  * docCookies.removeItem(name[, path], domain)
|*|  * docCookies.hasItem(name)
|*|  * docCookies.keys()
|*|
\*/

var docCookies = {
  // 获取  
  getItem: function (sKey) {
    return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
  },
  // 设置/添加
  setItem: function (sKey, sValue, vEnd, sPath, sDomain, bSecure) {
    if (!sKey || /^(?:expires|max\-age|path|domain|secure)$/i.test(sKey)) { return false; }
    var sExpires = "";
    if (vEnd) {
      switch (vEnd.constructor) {
        case Number:
          sExpires = vEnd === Infinity ? "; expires=Fri, 31 Dec 9999 23:59:59 GMT" : "; max-age=" + vEnd;
          break;
        case String:
          sExpires = "; expires=" + vEnd;
          break;
        case Date:
          sExpires = "; expires=" + vEnd.toUTCString();
          break;
      }
    }
    document.cookie = encodeURIComponent(sKey) + "=" + encodeURIComponent(sValue) + sExpires + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : "") + (bSecure ? "; secure" : "");
    return true;
  },
  // 移除
  removeItem: function (sKey, sPath, sDomain) {
    if (!sKey || !this.hasItem(sKey)) { return false; }
    document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + ( sDomain ? "; domain=" + sDomain : "") + ( sPath ? "; path=" + sPath : "");
    return true;
  },
  // 判断是否存在
  hasItem: function (sKey) {
    return (new RegExp("(?:^|;\\s*)" + encodeURIComponent(sKey).replace(/[-.+*]/g, "\\$&") + "\\s*\\=")).test(document.cookie);
  },
  // 获取所有可用cookie的key列表
  keys: /* optional method: you can safely remove it! */ function () {
    var aKeys = document.cookie.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, "").split(/\s*(?:\=[^;]*)?;\s*/);
    for (var nIdx = 0; nIdx < aKeys.length; nIdx++) { aKeys[nIdx] = decodeURIComponent(aKeys[nIdx]); }
    return aKeys;
  }
};

 

简易代码实现的随笔:

/**
 * 获取单个cookie的值
 * @param {string} key - cookie的键名
 * @return {(string | object)} 
 */
function getCookieItem(key) {
  const cookies = document.cookie ? document.cookie.split('; ') : [];
  const size = cookies.length;
  let result = {};
  for (let i = 0; i < size; i++) {
    const [k, v] = cookies[i].split('=');
    const name = decode(k);
    if (name === key) {
      result = decode(v);
      break;
    }
    if (key === undefined && v) {
      result[name] = decode(v);
    }
  }
  return result;
}

/**
 * cookie配置项的类型定义
 * @typedef {Object} CookieOptions
 * @prop {string} path - 路径
 * @prop {string} domain - 域名
 * @prop {number} maxAge - cookie的有效期
 * @prop {(string | number | Date)} expires - UTC字符串(没有设置,则会在会话结束时过期)
 * @prop {boolean} secure - 只允许https的协议传输
 */

/**
 * 添加/设置cookie
 * @param {string} key - cookie的键名
 * @param {string} value - cookie的值
 * @param {CookieOptions} [options={}] - cookie项的相关配置
 * @return {boolean}
 */
function setCookieItem(key, value, options = {}) {
  if (!key) {
    return false;
  }
  let expires = '';
  if (options.expires) {
    switch(options.expires.constructor) {
      case Number:
        expires = options.expires === Infinity ? '; expires=Fri, 31 Dec 9999 23:59:59 GMT' : '; max-age=' + options.expires;
        break;
      case String:
        expires = '; expires=' + options.expires;
        break;
      case Date:
        expires = '; expires=' + options.expires.toUTCString();
        break;
    }
  }
  document.cookie = [
      encode(key),
      '=',
      encode(value),
      expires, 
      options.path ? '; path=' + options.path : '',
      options.domain ? '; domain=' + options.domain : '',
      options.secure ? '; secure' : '',
  ].join('');
  return true;
}

/**
 * 移除单个cookie记录
 * @param {string} key - cookie的键名
 * @param {CookieOptions} [options={}] - cookie项的相关配置
 * @return {boolean} 
 */
function removeCookieItem(key, options = {}) {
  setCookieItem(key, '', { ...options, expires: -1 });
  return !getCookieItem(key);
}

/**
 * 编码URI字符串
 * @param {(string | number | boolean)} uriComponent - 未编码的资源标识符的部分字符
 * @return {string} 
 */
function encode(uriComponent) {
  return window.encodeURIComponent(uriComponent);
}

/**
 * 解码URI字符串
 * @param {string} encodedURIComponent - 编译后的资源标识符字符串
 * @return {string}
 */
function decode(encodedURIComponent) {
  return window.decodeURIComponent(encodedURIComponent);
}