@whitesev/utils 2.6.2 → 2.6.4
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/dist/index.amd.js +25 -7
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +25 -7
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +25 -7
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +25 -7
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +25 -7
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +25 -7
- package/dist/index.umd.js.map +1 -1
- package/dist/types/src/UtilsGMCookie.d.ts +4 -0
- package/dist/types/src/types/UtilsGMCookie.d.ts +2 -2
- package/package.json +1 -1
- package/src/Utils.ts +1 -1
- package/src/UtilsGMCookie.ts +24 -7
- package/src/types/UtilsGMCookie.d.ts +2 -2
|
@@ -2,6 +2,10 @@ import type { UtilsGMCookieDeleteOptions, UtilsGMCookieListOptions, UtilsGMCooki
|
|
|
2
2
|
export declare class UtilsGMCookie {
|
|
3
3
|
private windowApi;
|
|
4
4
|
constructor(windowApiOption?: WindowApiOption);
|
|
5
|
+
/**
|
|
6
|
+
* 获取Cookie分组
|
|
7
|
+
*/
|
|
8
|
+
private getCookiesList;
|
|
5
9
|
/**
|
|
6
10
|
* 获取单个cookie
|
|
7
11
|
* @param cookieName cookie名
|
|
@@ -41,7 +41,8 @@ export interface UtilsGMCookieSetOptions {
|
|
|
41
41
|
url?: string;
|
|
42
42
|
/**
|
|
43
43
|
* Cookie所在域
|
|
44
|
-
*
|
|
44
|
+
*
|
|
45
|
+
* 不填的话会默认在window.location.hostname的前面加上.
|
|
45
46
|
*/
|
|
46
47
|
domain?: string;
|
|
47
48
|
/** 需要检索的Cookie的名字 */
|
|
@@ -82,7 +83,6 @@ export interface UtilsGMCookieDeleteOptions {
|
|
|
82
83
|
path?: string;
|
|
83
84
|
/**
|
|
84
85
|
* Cookie所在域
|
|
85
|
-
* @default window.location.hostname
|
|
86
86
|
*/
|
|
87
87
|
firstPartyDomain?: string;
|
|
88
88
|
}
|
package/package.json
CHANGED
package/src/Utils.ts
CHANGED
package/src/UtilsGMCookie.ts
CHANGED
|
@@ -17,6 +17,15 @@ export class UtilsGMCookie {
|
|
|
17
17
|
this.windowApi = Object.assign({}, windowApiOption);
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* 获取Cookie分组
|
|
22
|
+
*/
|
|
23
|
+
private getCookiesList() {
|
|
24
|
+
if (this.windowApi.document.cookie.trim() === "") {
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
return this.windowApi.document.cookie.split(";");
|
|
28
|
+
}
|
|
20
29
|
/**
|
|
21
30
|
* 获取单个cookie
|
|
22
31
|
* @param cookieName cookie名
|
|
@@ -25,8 +34,7 @@ export class UtilsGMCookie {
|
|
|
25
34
|
if (typeof cookieName !== "string") {
|
|
26
35
|
throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
|
|
27
36
|
}
|
|
28
|
-
|
|
29
|
-
let cookies = this.windowApi.document.cookie.split(";");
|
|
37
|
+
let cookies = this.getCookiesList();
|
|
30
38
|
let findValue: UtilsGMCookieResult | undefined = void 0;
|
|
31
39
|
for (const cookieItem of cookies) {
|
|
32
40
|
let item = cookieItem.trim();
|
|
@@ -75,7 +83,7 @@ export class UtilsGMCookie {
|
|
|
75
83
|
path: "/",
|
|
76
84
|
};
|
|
77
85
|
defaultOption = Utils.assign(defaultOption, option);
|
|
78
|
-
let cookies = this.
|
|
86
|
+
let cookies = this.getCookiesList();
|
|
79
87
|
cookies.forEach((item) => {
|
|
80
88
|
item = item.trim();
|
|
81
89
|
let itemSplit = item.split("=");
|
|
@@ -126,7 +134,7 @@ export class UtilsGMCookie {
|
|
|
126
134
|
path: "/",
|
|
127
135
|
};
|
|
128
136
|
defaultOption = Utils.assign(defaultOption, option);
|
|
129
|
-
let cookies = this.
|
|
137
|
+
let cookies = this.getCookiesList();
|
|
130
138
|
cookies.forEach((item) => {
|
|
131
139
|
item = item.trim();
|
|
132
140
|
let itemSplit = item.split("=");
|
|
@@ -166,7 +174,7 @@ export class UtilsGMCookie {
|
|
|
166
174
|
url: this.windowApi.window.location.href,
|
|
167
175
|
name: "",
|
|
168
176
|
value: "",
|
|
169
|
-
domain:
|
|
177
|
+
domain: "",
|
|
170
178
|
path: "/",
|
|
171
179
|
secure: true,
|
|
172
180
|
httpOnly: false,
|
|
@@ -186,6 +194,9 @@ export class UtilsGMCookie {
|
|
|
186
194
|
";expires=" +
|
|
187
195
|
(new Date(life) as any).toGMTString() +
|
|
188
196
|
"; path=/";
|
|
197
|
+
if (Utils.isNotNull(defaultOption.domain)) {
|
|
198
|
+
cookieStr += "; domain=" + defaultOption.domain;
|
|
199
|
+
}
|
|
189
200
|
this.windowApi.document.cookie = cookieStr;
|
|
190
201
|
} catch (error: any) {
|
|
191
202
|
errorInfo = error;
|
|
@@ -210,10 +221,13 @@ export class UtilsGMCookie {
|
|
|
210
221
|
url: this.windowApi.window.location.href,
|
|
211
222
|
name: "",
|
|
212
223
|
path: "/",
|
|
213
|
-
firstPartyDomain:
|
|
224
|
+
firstPartyDomain: "",
|
|
214
225
|
};
|
|
215
226
|
defaultOption = Utils.assign(defaultOption, option);
|
|
216
|
-
let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}
|
|
227
|
+
let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}`;
|
|
228
|
+
if (Utils.isNotNull(defaultOption.firstPartyDomain)) {
|
|
229
|
+
cookieStr += `; domain=${defaultOption.firstPartyDomain};`;
|
|
230
|
+
}
|
|
217
231
|
this.windowApi.document.cookie = cookieStr;
|
|
218
232
|
} catch (error: any) {
|
|
219
233
|
errorInfo = error;
|
|
@@ -229,6 +243,9 @@ export class UtilsGMCookie {
|
|
|
229
243
|
* @param cookieStr
|
|
230
244
|
*/
|
|
231
245
|
parseCookie(cookieStr: string) {
|
|
246
|
+
if (cookieStr.trim() === "") {
|
|
247
|
+
return [];
|
|
248
|
+
}
|
|
232
249
|
let cookies = cookieStr.split(";");
|
|
233
250
|
let result: { key: string; value: string }[] = [];
|
|
234
251
|
for (const cookieItem of cookies) {
|
|
@@ -41,7 +41,8 @@ export interface UtilsGMCookieSetOptions {
|
|
|
41
41
|
url?: string;
|
|
42
42
|
/**
|
|
43
43
|
* Cookie所在域
|
|
44
|
-
*
|
|
44
|
+
*
|
|
45
|
+
* 不填的话会默认在window.location.hostname的前面加上.
|
|
45
46
|
*/
|
|
46
47
|
domain?: string;
|
|
47
48
|
/** 需要检索的Cookie的名字 */
|
|
@@ -82,7 +83,6 @@ export interface UtilsGMCookieDeleteOptions {
|
|
|
82
83
|
path?: string;
|
|
83
84
|
/**
|
|
84
85
|
* Cookie所在域
|
|
85
|
-
* @default window.location.hostname
|
|
86
86
|
*/
|
|
87
87
|
firstPartyDomain?: string;
|
|
88
88
|
}
|