Skip to content

Commit 9902d02

Browse files
committed
feat(cookie): 新增 cookie 解析相关方法
1 parent 85de9d1 commit 9902d02

File tree

4 files changed

+71
-1
lines changed

4 files changed

+71
-1
lines changed

src/common/cookie.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* cookie 相关处理工具方法
3+
*/
4+
5+
export function cookieParse(cookie = '') {
6+
const obj: Record<string, string> = {};
7+
if (!cookie && typeof document !== 'undefined') cookie = document.cookie;
8+
9+
if (typeof cookie === 'string' && cookie.length > 0) {
10+
for (const d of cookie.split(';')) {
11+
const [key, value] = d.split('=').map(d => d.trim());
12+
try {
13+
if (value != null) obj[key] = decodeURIComponent(value);
14+
} catch {
15+
try {
16+
obj[key] = unescape(value);
17+
} catch {
18+
obj[key] = value || '';
19+
}
20+
}
21+
}
22+
}
23+
return obj;
24+
}
25+
26+
export function cookieStringfiy(
27+
cookieObj: Record<string, string | number | boolean>,
28+
options: { filterKeys?: string[]; onlyKeys?: string[]; removeNil?: boolean } = {}
29+
) {
30+
return Object.keys(cookieObj)
31+
.filter(key => {
32+
if (options.filterKeys?.length && options.filterKeys.includes(key)) return false;
33+
if (options.onlyKeys?.length && !options.onlyKeys.includes(key)) return false;
34+
if (options.removeNil && (cookieObj[key] == null || cookieObj[key] === '')) return false;
35+
return true;
36+
})
37+
.map(key => `${key}=${cookieObj[key] ? encodeURIComponent(cookieObj[key]) : ''}`)
38+
.join('; ');
39+
}

src/common/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './async';
2+
export * from './cookie';
23
export * from './date';
34
export * from './helper';
45
export * from './is';

src/common/url.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
/*
2+
* @Author: renxia
3+
* @Date: 2023-03-23 23:05:16
4+
* @LastEditors: renxia
5+
* @LastEditTime: 2024-01-11 10:58:35
6+
* @Description:
7+
*/
18
/**
2-
*
9+
* url 格式化,返回一个 URL 对象
310
* @param url
411
* @param params 参数
512
* @param isRepalce 是否替换已存在于 url 中的参数

src/node/clearRequireCache.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fs } from './fs-system';
2+
13
/**
24
* 清除指定模块的 require 缓存(内存清理或实现热更新)
35
* @example
@@ -30,3 +32,24 @@ export function clearRequireCache(filePath: string) {
3032
for (const id of children) clearRequireCache(id);
3133
return true;
3234
}
35+
36+
const hotLoadCache = new Map<string, number>();
37+
// cache.delete(cache.keys().next().value);
38+
39+
/** require 热加载指定的文件 */
40+
export function requireHotLoad(filePath: string, force = false) {
41+
let needClearCache = force;
42+
let lastModified = 0;
43+
44+
if (!needClearCache) {
45+
lastModified = fs.statSync(filePath).mtimeMs;
46+
needClearCache = hotLoadCache.get(filePath) !== lastModified;
47+
}
48+
49+
if (needClearCache) {
50+
clearRequireCache(filePath);
51+
hotLoadCache.set(filePath, lastModified || fs.statSync(filePath).mtimeMs);
52+
}
53+
54+
return require(filePath);
55+
}

0 commit comments

Comments
 (0)