react-util-tools 1.0.10 → 1.0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -13,7 +13,7 @@
13
13
  ## 安装
14
14
 
15
15
  ```bash
16
- npm install @dj49846917/react-tools
16
+ npm install react-util-tools
17
17
  ```
18
18
 
19
19
  ## 快速开始
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-util-tools",
3
- "version": "1.0.10",
3
+ "version": "1.0.14",
4
4
  "description": "A collection of useful utilities: throttle, debounce, date formatting, device detection, money formatting, decimal calculations and more",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -0,0 +1,142 @@
1
+ # Cookie 工具
2
+
3
+ 提供常用的 Cookie 操作方法。
4
+
5
+ ## 方法列表
6
+
7
+ ### setCookie
8
+
9
+ 设置 Cookie。
10
+
11
+ ```typescript
12
+ setCookie(name: string, value: string, options?: CookieOptions): void
13
+ ```
14
+
15
+ **参数:**
16
+ - `name`: Cookie 名称
17
+ - `value`: Cookie 值
18
+ - `options`: 可选配置
19
+ - `expires`: 过期时间(秒数或 Date 对象)
20
+ - `path`: Cookie 路径
21
+ - `domain`: Cookie 域名
22
+ - `secure`: 是否仅通过 HTTPS 传输
23
+ - `sameSite`: SameSite 属性('Strict' | 'Lax' | 'None')
24
+
25
+ **示例:**
26
+ ```typescript
27
+ import { setCookie } from './cookie';
28
+
29
+ // 设置简单 Cookie
30
+ setCookie('username', 'john');
31
+
32
+ // 设置带过期时间的 Cookie(7天)
33
+ setCookie('token', 'abc123', { expires: 7 * 24 * 60 * 60 });
34
+
35
+ // 设置完整配置的 Cookie
36
+ setCookie('session', 'xyz789', {
37
+ expires: new Date('2025-12-31'),
38
+ path: '/',
39
+ domain: '.example.com',
40
+ secure: true,
41
+ sameSite: 'Strict'
42
+ });
43
+ ```
44
+
45
+ ### getCookie
46
+
47
+ 获取指定名称的 Cookie 值。
48
+
49
+ ```typescript
50
+ getCookie(name: string): string | null
51
+ ```
52
+
53
+ **示例:**
54
+ ```typescript
55
+ import { getCookie } from './cookie';
56
+
57
+ const username = getCookie('username');
58
+ console.log(username); // 'john' 或 null
59
+ ```
60
+
61
+ ### removeCookie
62
+
63
+ 删除指定名称的 Cookie。
64
+
65
+ ```typescript
66
+ removeCookie(name: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void
67
+ ```
68
+
69
+ **注意:** path 和 domain 需要与设置时保持一致才能正确删除。
70
+
71
+ **示例:**
72
+ ```typescript
73
+ import { removeCookie } from './cookie';
74
+
75
+ removeCookie('username');
76
+
77
+ // 删除指定 path 和 domain 的 Cookie
78
+ removeCookie('session', { path: '/', domain: '.example.com' });
79
+ ```
80
+
81
+ ### hasCookie
82
+
83
+ 检查指定名称的 Cookie 是否存在。
84
+
85
+ ```typescript
86
+ hasCookie(name: string): boolean
87
+ ```
88
+
89
+ **示例:**
90
+ ```typescript
91
+ import { hasCookie } from './cookie';
92
+
93
+ if (hasCookie('token')) {
94
+ console.log('用户已登录');
95
+ }
96
+ ```
97
+
98
+ ### getAllCookies
99
+
100
+ 获取所有 Cookie 的键值对。
101
+
102
+ ```typescript
103
+ getAllCookies(): Record<string, string>
104
+ ```
105
+
106
+ **示例:**
107
+ ```typescript
108
+ import { getAllCookies } from './cookie';
109
+
110
+ const cookies = getAllCookies();
111
+ console.log(cookies); // { username: 'john', token: 'abc123', ... }
112
+ ```
113
+
114
+ ### clearAllCookies
115
+
116
+ 清除所有 Cookie。
117
+
118
+ ```typescript
119
+ clearAllCookies(options?: Pick<CookieOptions, 'path' | 'domain'>): void
120
+ ```
121
+
122
+ **示例:**
123
+ ```typescript
124
+ import { clearAllCookies } from './cookie';
125
+
126
+ clearAllCookies();
127
+
128
+ // 清除指定 path 的所有 Cookie
129
+ clearAllCookies({ path: '/' });
130
+ ```
131
+
132
+ ## 类型定义
133
+
134
+ ```typescript
135
+ interface CookieOptions {
136
+ expires?: number | Date; // 过期时间(秒数或 Date 对象)
137
+ path?: string; // Cookie 路径
138
+ domain?: string; // Cookie 域名
139
+ secure?: boolean; // 是否仅 HTTPS
140
+ sameSite?: 'Strict' | 'Lax' | 'None'; // SameSite 属性
141
+ }
142
+ ```
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Cookie 操作工具类
3
+ */
4
+
5
+ export interface CookieOptions {
6
+ expires?: number | Date;
7
+ path?: string;
8
+ domain?: string;
9
+ secure?: boolean;
10
+ sameSite?: 'Strict' | 'Lax' | 'None';
11
+ }
12
+
13
+ /**
14
+ * 设置 Cookie
15
+ * @param name Cookie 名称
16
+ * @param value Cookie 值
17
+ * @param options Cookie 配置选项
18
+ */
19
+ export function setCookie(name: string, value: string, options: CookieOptions = {}): void {
20
+ let cookieString = `${encodeURIComponent(name)}=${encodeURIComponent(value)}`;
21
+
22
+ if (options.expires) {
23
+ const expires = options.expires instanceof Date
24
+ ? options.expires
25
+ : new Date(Date.now() + options.expires * 1000);
26
+ cookieString += `; expires=${expires.toUTCString()}`;
27
+ }
28
+
29
+ if (options.path) {
30
+ cookieString += `; path=${options.path}`;
31
+ }
32
+
33
+ if (options.domain) {
34
+ cookieString += `; domain=${options.domain}`;
35
+ }
36
+
37
+ if (options.secure) {
38
+ cookieString += '; secure';
39
+ }
40
+
41
+ if (options.sameSite) {
42
+ cookieString += `; SameSite=${options.sameSite}`;
43
+ }
44
+
45
+ document.cookie = cookieString;
46
+ }
47
+
48
+ /**
49
+ * 获取 Cookie
50
+ * @param name Cookie 名称
51
+ * @returns Cookie 值,不存在则返回 null
52
+ */
53
+ export function getCookie(name: string): string | null {
54
+ const nameEQ = encodeURIComponent(name) + '=';
55
+ const cookies = document.cookie.split(';');
56
+
57
+ for (let cookie of cookies) {
58
+ cookie = cookie.trim();
59
+ if (cookie.indexOf(nameEQ) === 0) {
60
+ return decodeURIComponent(cookie.substring(nameEQ.length));
61
+ }
62
+ }
63
+
64
+ return null;
65
+ }
66
+
67
+ /**
68
+ * 删除 Cookie
69
+ * @param name Cookie 名称
70
+ * @param options Cookie 配置选项(path 和 domain 需要与设置时一致)
71
+ */
72
+ export function removeCookie(name: string, options: Pick<CookieOptions, 'path' | 'domain'> = {}): void {
73
+ setCookie(name, '', {
74
+ ...options,
75
+ expires: new Date(0)
76
+ });
77
+ }
78
+
79
+ /**
80
+ * 检查 Cookie 是否存在
81
+ * @param name Cookie 名称
82
+ * @returns 是否存在
83
+ */
84
+ export function hasCookie(name: string): boolean {
85
+ return getCookie(name) !== null;
86
+ }
87
+
88
+ /**
89
+ * 获取所有 Cookie
90
+ * @returns Cookie 键值对对象
91
+ */
92
+ export function getAllCookies(): Record<string, string> {
93
+ const cookies: Record<string, string> = {};
94
+ const cookieArray = document.cookie.split(';');
95
+
96
+ for (let cookie of cookieArray) {
97
+ cookie = cookie.trim();
98
+ const [name, ...valueParts] = cookie.split('=');
99
+ if (name) {
100
+ cookies[decodeURIComponent(name)] = decodeURIComponent(valueParts.join('='));
101
+ }
102
+ }
103
+
104
+ return cookies;
105
+ }
106
+
107
+ /**
108
+ * 清除所有 Cookie
109
+ * @param options Cookie 配置选项(path 和 domain)
110
+ */
111
+ export function clearAllCookies(options: Pick<CookieOptions, 'path' | 'domain'> = {}): void {
112
+ const cookies = getAllCookies();
113
+ for (const name in cookies) {
114
+ removeCookie(name, options);
115
+ }
116
+ }