Skip to content

Commit

Permalink
fix(cookie): 修复 cookieParse 解析 value 包含 = 时会丢失后半部分的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
renxia committed Jul 24, 2024
1 parent f275e42 commit bbc0914
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/common/cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('cookie.ts', () => {
['a=1;b=b', { a: '1', b: 'b' }],
['a=;b=b', { a: '', b: 'b' }],
['a=%E6%B5%8B%E8%AF%95001;b=b', { a: '测试001', b: 'b' }],
['a=%E6%B5%8B%E8%AF%95001=123;b=b', { a: '测试001=123', b: 'b' }],
] as const;
for (const [cookie, obj] of list) {
expect(cookieParse(cookie as never)).toEqual(obj);
Expand Down
6 changes: 4 additions & 2 deletions src/common/cookie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: renxia
* @Date: 2024-01-15 11:26:52
* @LastEditors: renxia
* @LastEditTime: 2024-01-16 16:38:39
* @LastEditTime: 2024-07-24 11:38:51
* @Description: cookie 相关处理工具方法
*/

Expand All @@ -14,7 +14,9 @@ export function cookieParse(cookie = '', filterNilValue = false) {

if (typeof cookie === 'string' && cookie.length > 0) {
for (const d of cookie.split(';')) {
const [key, value] = d.split('=').map(d => d.trim());
const arr = d.split('=');
const key = arr[0].trim();
const value = arr.slice(1).join('=').trim();
if (filterNilValue && !value) continue;

try {
Expand Down

0 comments on commit bbc0914

Please sign in to comment.