@zibu/common
Version:
通用逻辑功能模块。包含AjaxService、clone、cookie、dataFormat、queryString、clipboard
141 lines (127 loc) • 4.19 kB
JavaScript
/*\
|*|
|*| :: cookies.js ::
|*|
|*| A complete cookies reader/writer framework with full unicode support.
|*|
|*| https://developer.mozilla.org/en-US/docs/DOM/document.cookie
|*|
|*| Syntaxes:
|*|
|*| * cookies.setItem(name, value[, option])
|*| * cookies.getItem(name)
|*| * cookies.removeItem(name[, option])
|*| * cookies.hasItem(name)
|*| * cookies.keys()
|*|
\*/
/**
* Cookies类定义
*/
class Cookies {
/**
* 获取指定key的value
* @param {String} key 要获取的key
* @returns {String} 返回指定key的value
*/
getItem(key) {
key = encodeURIComponent(key).replace(/[-.+*]/g, '\\$&');
const reg = new RegExp('(?:(?:^|.*;)\\s*' + key + '\\s*\\=\\s*([^;]*).*$)|^.*$');
return decodeURIComponent(document.cookie.replace(reg, '$1')) || null;
}
/**
* 设置指定key的value
* @param {String} key 指定的key
* @param {String} value 设置的value
* @param {Object} option cookie配置项,包括:expires、domain、path、secure
* @returns {Boolean} 返回是否设置成功
*/
setItem(key, value, option = { expires: undefined, domain: '', path: '', secure: false }) {
// 是否未提供key或者key是cookie自己的属性名
if (!key || /^(?:expires|max\-age|path|domain|secure)$/i.test(key)) {
return false;
}
const result = [];
if (typeof value === 'object' && value) {
value = JSON.stringify(value);
}
result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
if (option.expires) {
let sExpires = '';
switch (option.expires.constructor) {
case Number:
sExpires =
option.expires === Infinity
? 'expires=Fri, 31 Dec 9999 23:59:59 GMT'
: 'max-age=' + option.expires;
break;
case String:
sExpires = 'expires=' + option.expires;
break;
case Date:
sExpires = 'expires=' + option.expires.toUTCString();
break;
}
if (sExpires) {
result.push(sExpires);
}
}
if (option.domain) {
result.push('domain=' + option.domain);
}
if (option.path) {
result.push('path=' + option.path);
}
if (option.secure) {
result.push('secure');
}
document.cookie = result.join('; ');
return true;
}
/**
* 移除指定key的cookie
* @param {String} key 要移除的key
* @param {Option} option cookie配置项,包括:domain、path
* @returns {Boolean} 返回是否移除成功
*/
removeItem(key, option = { domain: '', path: '' }) {
// 是否未提供key,或者cookie中不包含指定的key
if (!key || !this.hasItem(key)) {
return false;
}
return this.setItem(key, '', {
expires: 'Thu, 01 Jan 1970 00:00:00 GMT',
domain: option.domain,
path: option.path
});
}
/**
* 是否含有指定key
* @param {String} key 要查找的key
* @returns {Boolean} 返回是否包含
*/
hasItem(key) {
return new RegExp('(?:^|;\\s*)' + encodeURIComponent(key).replace(/[-.+*]/g, '\\$&') + '\\s*\\=').test(
document.cookie
);
}
/**
* 获取设置的所有key
* @returns {Array} 返回所有key
*/
keys() {
var aKeys = document.cookie
.replace(/((?:^|\s*;)[^\=]+)(?=;|$)|^\s*|\s*(?:\=[^;]*)?(?:\1|$)/g, '')
.split(/\s*(?:\=[^;]*)?;\s*/);
const result = [];
for (var nIdx = 0; nIdx < aKeys.length; nIdx++) {
const key = decodeURIComponent(aKeys[nIdx]);
// 排除cookie自己的属性名
if (!/^(?:expires|max\-age|path|domain|secure)$/i.test(key)) {
result.push(key);
}
}
return result;
}
}
export default new Cookies();