1. 前言
1.1 环境
Cookies.set(‘foo’, ‘bar’)
<a name="JMB54"></a>
## 2.2 源码
```javascript
// 初始化函数
function init (converter, defaultAttributes) {
// 设置cookie
function set (name, value, attributes) {
if (typeof document === 'undefined') {
return
}
// 合并属性
attributes = assign({}, defaultAttributes, attributes)
// 设置过期时间
if (typeof attributes.expires === 'number') {
// 用了科学计数864e5=86400000,e5就是后面5个0,Date.now()是毫秒级别,864e5这里代表一天,
attributes.expires = new Date(Date.now() + attributes.expires * 864e5)
}
if (attributes.expires) {
attributes.expires = attributes.expires.toUTCString()
}
// 做了uri的编码
name = encodeURIComponent(name)
.replace(/%(2[346B]|5E|60|7C)/g, decodeURIComponent)
.replace(/[()]/g, escape)
var stringifiedAttributes = ''
for (var attributeName in attributes) {
if (!attributes[attributeName]) {
continue
}
// 将参数通过; 累加
stringifiedAttributes += '; ' + attributeName
if (attributes[attributeName] === true) {
continue
}
// Considers RFC 6265 section 5.2:
// ...
// 3. If the remaining unparsed-attributes contains a %x3B (";")
// character:
// Consume the characters of the unparsed-attributes up to,
// not including, the first %x3B (";") character.
// ...
stringifiedAttributes += '=' + attributes[attributeName].split(';')[0]
}
return (document.cookie =
name + '=' + converter.write(value, name) + stringifiedAttributes)
}
// 获取名称
function get (name) {
if (typeof document === 'undefined' || (arguments.length && !name)) {
return
}
// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all.
// 将cookie分割成数组
var cookies = document.cookie ? document.cookie.split('; ') : []
var jar = {}
for (var i = 0; i < cookies.length; i++) {
var parts = cookies[i].split('=')
var value = parts.slice(1).join('=')
try {
var found = decodeURIComponent(parts[0])
jar[found] = converter.read(value, found)
if (name === found) {
break
}
} catch (e) {}
}
// 获取需要的数组
return name ? jar[name] : jar
}
// 初始化返回一个对象,set,get,remove,只读attributes和converter
return Object.create(
{
set: set,
get: get,
// remove 清空数据,如果有属性,以属性作为初始化值
remove: function (name, attributes) {
set(
name,
'',
assign({}, attributes, {
expires: -1
})
)
},
// 修改attributes,返回一个新的对象
withAttributes: function (attributes) {
return init(this.converter, assign({}, this.attributes, attributes))
},
// 修改converter,返回一个新的对象
withConverter: function (converter) {
return init(assign({}, this.converter, converter), this.attributes)
}
},
{
attributes: { value: Object.freeze(defaultAttributes) },
converter: { value: Object.freeze(converter) }
}
)
}
export default init(defaultConverter, { path: '/' })
/* eslint-enable no-var */