request/lib/cookie.js

37 lines
631 B
JavaScript

/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/20 15:08:50
*/
// var KEY_REGEXP = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/
const SPLIT_REGEXP = /; */
// var encode = encodeURIComponent
const decode = decodeURIComponent
/**
* [parse 格式化字符串]
*/
export function parseCookie(str) {
let obj = {}
let pairs
if (typeof str !== 'string') {
return {}
}
pairs = str.split(SPLIT_REGEXP)
for (let item of pairs) {
item = item.split('=')
if (item.length < 2) {
continue
}
let key = item[0].trim()
let val = item[1].trim()
obj[key] = decode(val)
}
return obj
}
对Http的request进一步封装, 提供常用的API
JavaScript 100%