request/lib/cookie.js

37 lines
631 B
JavaScript
Raw Normal View History

2020-09-21 17:57:42 +08:00
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/20 15:08:50
*/
// var KEY_REGEXP = /^[\u0009\u0020-\u007e\u0080-\u00ff]+$/
2023-10-25 18:45:16 +08:00
const SPLIT_REGEXP = /; */
2020-09-21 17:57:42 +08:00
// var encode = encodeURIComponent
2023-10-25 18:45:16 +08:00
const decode = decodeURIComponent
2020-09-21 17:57:42 +08:00
/**
* [parse 格式化字符串]
*/
export function parseCookie(str) {
2023-10-25 18:45:16 +08:00
let obj = {}
let pairs
2020-09-21 17:57:42 +08:00
if (typeof str !== 'string') {
return {}
}
pairs = str.split(SPLIT_REGEXP)
for (let item of pairs) {
item = item.split('=')
if (item.length < 2) {
continue
}
2023-10-25 18:45:16 +08:00
let key = item[0].trim()
let val = item[1].trim()
2020-09-21 17:57:42 +08:00
obj[key] = decode(val)
}
return obj
}