@whitesev/utils 1.1.3 → 1.1.5

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.
@@ -1,4 +1,4 @@
1
- declare interface UtilsGMCookieListResult {
1
+ declare interface UtilsGMCookieResult {
2
2
  /** 为 window.location.hostname */
3
3
  domain: string;
4
4
  expirationDate: null;
@@ -11,15 +11,35 @@ declare interface UtilsGMCookieListResult {
11
11
  session: false;
12
12
  value: string;
13
13
  }
14
+ declare interface UtilsGMCookieListOptions {
15
+ /** 默认为当前的url */
16
+ url: string;
17
+ /** 默认为当前的域名(window.location.hostname) */
18
+ domain: string;
19
+ /** 需要检索的Cookie的名字 */
20
+ name: string | RegExp;
21
+ /** 需要检索的Cookie的路径,默认为"/" */
22
+ path: string;
23
+ }
14
24
  declare class UtilsGMCookie {
15
25
  /**
16
- * 获取Cookie
26
+ * 获取单个cookie
27
+ * @param cookieName cookie名
28
+ */
29
+ get(cookieName: string): UtilsGMCookieResult | undefined;
30
+ /**
31
+ * 获取多组Cookie
17
32
  * @param paramDetails
18
33
  * @param callback
19
34
  * + cookies object[]
20
35
  * + error string|undefined
21
36
  **/
22
- list(paramDetails?: Partial<UtilsGMCookieListResult>, callback?: (resultData: UtilsGMCookieListResult[], error?: Error) => void): void;
37
+ list(paramDetails: Partial<UtilsGMCookieListOptions>, callback?: (data: UtilsGMCookieResult[], error?: Error) => void): void;
38
+ /**
39
+ * 获取多组Cookie
40
+ * @param paramDetails
41
+ **/
42
+ getList(paramDetails: Partial<UtilsGMCookieListOptions>): UtilsGMCookieResult[];
23
43
  /**
24
44
  * 设置Cookie
25
45
  * @param paramDetails
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/node/index.esm.js",
@@ -1,6 +1,6 @@
1
1
  import { Utils } from "./Utils";
2
2
 
3
- declare interface UtilsGMCookieListResult {
3
+ declare interface UtilsGMCookieResult {
4
4
  /** 为 window.location.hostname */
5
5
  domain: string;
6
6
  expirationDate: null;
@@ -20,7 +20,7 @@ declare interface UtilsGMCookieListOptions {
20
20
  /** 默认为当前的域名(window.location.hostname) */
21
21
  domain: string;
22
22
  /** 需要检索的Cookie的名字 */
23
- name: string;
23
+ name: string | RegExp;
24
24
  /** 需要检索的Cookie的路径,默认为"/" */
25
25
  path: string;
26
26
  }
@@ -53,20 +53,57 @@ declare interface UtilsGMCookieDeleteOptions {
53
53
 
54
54
  class UtilsGMCookie {
55
55
  /**
56
- * 获取Cookie
56
+ * 获取单个cookie
57
+ * @param cookieName cookie名
58
+ */
59
+ get(cookieName: string) {
60
+ if (typeof cookieName !== "string") {
61
+ throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
62
+ }
63
+
64
+ let cookies = document.cookie.split(";");
65
+ let findValue: UtilsGMCookieResult | undefined = void 0;
66
+ for (const cookieItem of cookies) {
67
+ let item = cookieItem.trim();
68
+ let itemSplit = item.split("=");
69
+ let itemName = itemSplit[0];
70
+ itemSplit.splice(0, 1);
71
+ let itemValue = itemSplit.join("");
72
+ if (itemName === cookieName) {
73
+ findValue = {
74
+ domain: globalThis.location.hostname,
75
+ expirationDate: null,
76
+ hostOnly: true,
77
+ httpOnly: false,
78
+ name: cookieName,
79
+ path: "/",
80
+ sameSite: "unspecified",
81
+ secure: true,
82
+ session: false,
83
+ value: itemValue,
84
+ };
85
+ break;
86
+ }
87
+ }
88
+ return findValue;
89
+ }
90
+ /**
91
+ * 获取多组Cookie
57
92
  * @param paramDetails
58
93
  * @param callback
59
94
  * + cookies object[]
60
95
  * + error string|undefined
61
96
  **/
62
97
  list(
63
- paramDetails: Partial<UtilsGMCookieListResult> = {},
64
- callback = (resultData: UtilsGMCookieListResult[], error?: Error) => {}
98
+ paramDetails: Partial<UtilsGMCookieListOptions>,
99
+ callback?: (data: UtilsGMCookieResult[], error?: Error) => void
65
100
  ) {
66
- let resultData: UtilsGMCookieListResult[] = [];
101
+ if (paramDetails == null) {
102
+ throw new Error("Utils.GMCookie.list 参数不能为空");
103
+ }
104
+ let resultData: UtilsGMCookieResult[] = [];
67
105
  try {
68
- let details: Partial<UtilsGMCookieListResult> = {
69
- // @ts-ignore
106
+ let details: Partial<UtilsGMCookieListOptions> = {
70
107
  url: globalThis.location.href,
71
108
  domain: globalThis.location.hostname,
72
109
  name: "",
@@ -75,11 +112,13 @@ class UtilsGMCookie {
75
112
  details = Utils.assign(details, paramDetails);
76
113
  let cookies = document.cookie.split(";");
77
114
  cookies.forEach((item) => {
78
- item = item.trimStart();
79
- let itemName = item.split("=")[0];
80
- let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
115
+ item = item.trim();
116
+ let itemSplit = item.split("=");
117
+ let itemName = itemSplit[0];
118
+ itemSplit.splice(0, 1);
119
+ let itemValue = itemSplit.join("");
81
120
  let nameRegexp =
82
- (details.name as any) instanceof RegExp
121
+ (details.name as RegExp) instanceof RegExp
83
122
  ? details.name
84
123
  : new RegExp("^" + details.name, "g");
85
124
  if (itemName.match(nameRegexp as RegExp)) {
@@ -95,14 +134,63 @@ class UtilsGMCookie {
95
134
  session: false,
96
135
  value: itemValue,
97
136
  });
98
- return;
99
137
  }
100
138
  });
101
- callback(resultData);
139
+ if (typeof callback === "function") {
140
+ callback(resultData);
141
+ }
102
142
  } catch (error) {
103
- callback(resultData, error as Error);
143
+ if (typeof callback === "function") {
144
+ callback(resultData, error as Error);
145
+ }
104
146
  }
105
147
  }
148
+ /**
149
+ * 获取多组Cookie
150
+ * @param paramDetails
151
+ **/
152
+ getList(
153
+ paramDetails: Partial<UtilsGMCookieListOptions>
154
+ ): UtilsGMCookieResult[] {
155
+ if (paramDetails == null) {
156
+ throw new Error("Utils.GMCookie.list 参数不能为空");
157
+ }
158
+ let resultData: UtilsGMCookieResult[] = [];
159
+ let details: Partial<UtilsGMCookieListOptions> = {
160
+ url: globalThis.location.href,
161
+ domain: globalThis.location.hostname,
162
+ name: "",
163
+ path: "/",
164
+ };
165
+ details = Utils.assign(details, paramDetails);
166
+ let cookies = document.cookie.split(";");
167
+ cookies.forEach((item) => {
168
+ item = item.trim();
169
+ let itemSplit = item.split("=");
170
+ let itemName = itemSplit[0];
171
+ itemSplit.splice(0, 1);
172
+ let itemValue = itemSplit.join("");
173
+ let nameRegexp =
174
+ (details.name as RegExp) instanceof RegExp
175
+ ? details.name
176
+ : new RegExp("^" + details.name, "g");
177
+ if (itemName.match(nameRegexp as RegExp)) {
178
+ resultData.push({
179
+ domain: globalThis.location.hostname,
180
+ expirationDate: null,
181
+ hostOnly: true,
182
+ httpOnly: false,
183
+ name: itemName,
184
+ path: "/",
185
+ sameSite: "unspecified",
186
+ secure: true,
187
+ session: false,
188
+ value: itemValue,
189
+ });
190
+ }
191
+ });
192
+ return resultData;
193
+ }
106
194
  /**
107
195
  * 设置Cookie
108
196
  * @param paramDetails
@@ -150,7 +238,7 @@ class UtilsGMCookie {
150
238
  let details: Partial<UtilsGMCookieDeleteOptions> = {
151
239
  url: window.location.href,
152
240
  name: "",
153
- // @ts-ignore
241
+ // @ts-ignore
154
242
  firstPartyDomain: "",
155
243
  };
156
244
  details = Utils.assign(details, paramDetails);