@whitesev/utils 2.5.3 → 2.5.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.
@@ -9,28 +9,28 @@ declare class UtilsGMCookie {
9
9
  get(cookieName: string): UtilsGMCookieResult | undefined;
10
10
  /**
11
11
  * 获取多组Cookie
12
- * @param paramDetails 配置
12
+ * @param option 配置
13
13
  * @param callback 获取操作后的回调
14
14
  * + cookies object[]
15
15
  * + error string|undefined
16
16
  **/
17
- list(paramDetails: Partial<UtilsGMCookieListOptions>, callback?: (data: UtilsGMCookieResult[], error?: Error) => void): void;
17
+ list(option: UtilsGMCookieListOptions, callback?: (data: UtilsGMCookieResult[], error?: Error) => void): void;
18
18
  /**
19
19
  * 获取多组Cookie
20
- * @param paramDetails 配置
20
+ * @param option 配置
21
21
  **/
22
- getList(paramDetails: Partial<UtilsGMCookieListOptions>): UtilsGMCookieResult[];
22
+ getList(option: UtilsGMCookieListOptions): UtilsGMCookieResult[];
23
23
  /**
24
24
  * 设置Cookie
25
- * @param paramDetails 配置
25
+ * @param option 配置
26
26
  * @param callback 设置操作后的回调(成功/失败)
27
27
  */
28
- set(paramDetails: Partial<UtilsGMCookieSetOptions>, callback?: (error?: Error) => void): void;
28
+ set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void): void;
29
29
  /**
30
30
  * 删除Cookie
31
- * @param paramDetails 配置
31
+ * @param option 配置
32
32
  * @param callback 删除操作后的回调(成功/失败)
33
33
  */
34
- delete(paramDetails: Partial<UtilsGMCookieDeleteOptions>, callback?: (error?: Error) => void): void;
34
+ delete(option: UtilsGMCookieDeleteOptions, callback?: (error?: Error) => void): void;
35
35
  }
36
36
  export { UtilsGMCookie };
@@ -1,52 +1,90 @@
1
1
  export interface UtilsGMCookieResult {
2
2
  /** 为 window.location.hostname */
3
3
  domain: string;
4
+ /** 过期时间 */
4
5
  expirationDate: null;
5
6
  hostOnly: true;
6
7
  httpOnly: false;
8
+ /** Cookie名 */
7
9
  name: string;
10
+ /** Cookie的路径 */
8
11
  path: "/";
12
+ /** Cookie是否同源策略 */
9
13
  sameSite: "unspecified";
10
14
  secure: true;
11
15
  session: false;
16
+ /** Cookie值 */
12
17
  value: string;
13
18
  }
14
19
 
15
20
  export interface UtilsGMCookieListOptions {
16
21
  /** 默认为当前的url */
17
- url: string;
18
- /** 默认为当前的域名(window.location.hostname) */
19
- domain: string;
22
+ url?: string;
23
+ /**
24
+ * Cookie所在域
25
+ * @default window.location.hostname
26
+ */
27
+ domain?: string;
20
28
  /** 需要检索的Cookie的名字 */
21
29
  name: string | RegExp;
22
- /** 需要检索的Cookie的路径,默认为"/" */
23
- path: string;
30
+ /**
31
+ * 需要检索的Cookie的路径
32
+ * @default "/"
33
+ */
34
+ path?: string;
24
35
  }
25
36
 
26
37
  export interface UtilsGMCookieSetOptions {
27
- /** 默认为当前的url */
38
+ /**
39
+ * 默认为当前的url
40
+ */
28
41
  url?: string;
29
- /** 默认为当前的域名(window.location.hostname) */
42
+ /**
43
+ * Cookie所在域
44
+ * @default window.location.hostname
45
+ */
30
46
  domain?: string;
31
47
  /** 需要检索的Cookie的名字 */
32
- name?: string;
33
- /** 需要检索的Cookie的路径,默认为"/" */
48
+ name: string;
49
+ /**
50
+ * 需要检索的Cookie的路径
51
+ * @default "/"
52
+ */
34
53
  path?: string;
35
- /** 值 */
36
- value?: string | number;
37
- /** 确保Cookie只在通过安全协议(如HTTPS)的情况下传输 */
54
+ /** Cookie值 */
55
+ value: string | number;
56
+ /**
57
+ * 确保Cookie只在通过安全协议(如HTTPS)的情况下传输
58
+ * @default true
59
+ */
38
60
  secure?: boolean;
39
- /** 是否防止JavaScript代码访问Cookie */
61
+ /**
62
+ * 是否防止JavaScript代码访问Cookie
63
+ * @default false
64
+ */
40
65
  httpOnly?: boolean;
41
- /** Cookie过期时间,默认为30天 */
66
+ /**
67
+ * Cookie过期时间的时间戳,默认为30天
68
+ * @default Math.floor(Date.now()) + 60 * 60 * 24 * 30
69
+ */
42
70
  expirationDate?: number;
43
71
  }
44
72
 
45
73
  export interface UtilsGMCookieDeleteOptions {
46
- /** 默认为当前的url */
47
- url: string;
48
74
  /** 需要检索的Cookie的名字 */
49
75
  name: string;
76
+ /** 默认为当前的url */
77
+ url?: string;
78
+ /**
79
+ * Cookie的路径
80
+ * @default "/"
81
+ */
82
+ path?: string;
83
+ /**
84
+ * Cookie所在域
85
+ * @default window.location.hostname
86
+ */
87
+ firstPartyDomain?: string;
50
88
  }
51
89
 
52
90
  export interface WindowApiOption {
@@ -121,20 +121,32 @@ export declare interface Vue2Instance {
121
121
  back: () => void;
122
122
  go: (...args: any[]) => void;
123
123
  replace: (...args: any[]) => void;
124
+ /** 添加路由 */
124
125
  addRoute: (...args: any[]) => void;
125
126
  addRoutes: (...args: any[]) => void;
126
127
  [key: string]: any;
127
128
  };
128
129
  $ssrContext: any;
129
-
130
+ /** 观察者 @returns 取消观察者 */
130
131
  $watch: (
132
+ /** 需要观察的属性 */
131
133
  key: string | string[] | (() => any),
132
- handler: (this: any, newVal: any, oldVal: any) => void,
134
+ /** 属性改变时触发的回调 */
135
+ handler: (
136
+ this: Vue2Instance,
137
+ /** 新值,也就是改变后的值 */
138
+ newVal: any,
139
+ /** 旧值,也就是改变前的值 */
140
+ oldVal: any
141
+ ) => void,
142
+ /** 监听配置 */
133
143
  options?: {
144
+ /** 是否立即执行handler */
134
145
  immediate?: boolean;
146
+ /** 是否深度监听 */
135
147
  deep?: boolean;
136
148
  }
137
- ) => void;
149
+ ) => Function;
138
150
 
139
151
  [key: string]: any;
140
152
  }
@@ -22,3 +22,6 @@ export declare interface AnyObject {
22
22
  [key: string]: any | AnyObject;
23
23
  toString(): string;
24
24
  }
25
+ export type PartialKeys<T, K extends keyof T> = {
26
+ [P in K]?: T[P];
27
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@whitesev/utils",
3
- "version": "2.5.3",
3
+ "version": "2.5.5",
4
4
  "description": "一个常用的工具库",
5
5
  "main": "dist/index.cjs.js",
6
6
  "module": "dist/index.esm.js",
@@ -46,6 +46,7 @@
46
46
  "scripts": {
47
47
  "dev": "rollup --config --watch",
48
48
  "build": "rollup --config",
49
- "build:all": "rollup --config"
49
+ "build:all": "rollup --config",
50
+ "build:all-new": "rollup --config"
50
51
  }
51
52
  }
package/src/Utils.ts CHANGED
@@ -29,7 +29,7 @@ class Utils {
29
29
  this.windowApi = new WindowApi(option);
30
30
  }
31
31
  /** 版本号 */
32
- version = "2024.11.16";
32
+ version = "2024.12.3";
33
33
 
34
34
  /**
35
35
  * 在页面中增加style元素,如果html节点存在子节点,添加子节点第一个,反之,添加到html节点的子节点最后一个
@@ -54,27 +54,27 @@ class UtilsGMCookie {
54
54
  }
55
55
  /**
56
56
  * 获取多组Cookie
57
- * @param paramDetails 配置
57
+ * @param option 配置
58
58
  * @param callback 获取操作后的回调
59
59
  * + cookies object[]
60
60
  * + error string|undefined
61
61
  **/
62
62
  list(
63
- paramDetails: Partial<UtilsGMCookieListOptions>,
63
+ option: UtilsGMCookieListOptions,
64
64
  callback?: (data: UtilsGMCookieResult[], error?: Error) => void
65
65
  ) {
66
- if (paramDetails == null) {
66
+ if (option == null) {
67
67
  throw new Error("Utils.GMCookie.list 参数不能为空");
68
68
  }
69
69
  let resultData: UtilsGMCookieResult[] = [];
70
70
  try {
71
- let details: Partial<UtilsGMCookieListOptions> = {
71
+ let defaultOption: Required<UtilsGMCookieListOptions> = {
72
72
  url: this.windowApi.window.location.href,
73
73
  domain: this.windowApi.window.location.hostname,
74
74
  name: "",
75
75
  path: "/",
76
76
  };
77
- details = Utils.assign(details, paramDetails);
77
+ defaultOption = Utils.assign(defaultOption, option);
78
78
  let cookies = this.windowApi.document.cookie.split(";");
79
79
  cookies.forEach((item) => {
80
80
  item = item.trim();
@@ -83,9 +83,9 @@ class UtilsGMCookie {
83
83
  itemSplit.splice(0, 1);
84
84
  let itemValue = decodeURIComponent(itemSplit.join(""));
85
85
  let nameRegexp =
86
- (details.name as RegExp) instanceof RegExp
87
- ? details.name
88
- : new RegExp("^" + details.name, "g");
86
+ (defaultOption.name as RegExp) instanceof RegExp
87
+ ? defaultOption.name
88
+ : new RegExp("^" + defaultOption.name, "g");
89
89
  if (itemName.match(nameRegexp as RegExp)) {
90
90
  resultData.push({
91
91
  domain: this.windowApi.window.location.hostname,
@@ -112,22 +112,20 @@ class UtilsGMCookie {
112
112
  }
113
113
  /**
114
114
  * 获取多组Cookie
115
- * @param paramDetails 配置
115
+ * @param option 配置
116
116
  **/
117
- getList(
118
- paramDetails: Partial<UtilsGMCookieListOptions>
119
- ): UtilsGMCookieResult[] {
120
- if (paramDetails == null) {
117
+ getList(option: UtilsGMCookieListOptions): UtilsGMCookieResult[] {
118
+ if (option == null) {
121
119
  throw new Error("Utils.GMCookie.list 参数不能为空");
122
120
  }
123
121
  let resultData: UtilsGMCookieResult[] = [];
124
- let details: Partial<UtilsGMCookieListOptions> = {
122
+ let defaultOption: Required<UtilsGMCookieListOptions> = {
125
123
  url: this.windowApi.window.location.href,
126
124
  domain: this.windowApi.window.location.hostname,
127
125
  name: "",
128
126
  path: "/",
129
127
  };
130
- details = Utils.assign(details, paramDetails);
128
+ defaultOption = Utils.assign(defaultOption, option);
131
129
  let cookies = this.windowApi.document.cookie.split(";");
132
130
  cookies.forEach((item) => {
133
131
  item = item.trim();
@@ -136,9 +134,9 @@ class UtilsGMCookie {
136
134
  itemSplit.splice(0, 1);
137
135
  let itemValue = decodeURIComponent(itemSplit.join(""));
138
136
  let nameRegexp =
139
- (details.name as RegExp) instanceof RegExp
140
- ? details.name
141
- : new RegExp("^" + details.name, "g");
137
+ (defaultOption.name as RegExp) instanceof RegExp
138
+ ? defaultOption.name
139
+ : new RegExp("^" + defaultOption.name, "g");
142
140
  if (itemName.match(nameRegexp as RegExp)) {
143
141
  resultData.push({
144
142
  domain: this.windowApi.window.location.hostname,
@@ -158,15 +156,13 @@ class UtilsGMCookie {
158
156
  }
159
157
  /**
160
158
  * 设置Cookie
161
- * @param paramDetails 配置
159
+ * @param option 配置
162
160
  * @param callback 设置操作后的回调(成功/失败)
163
161
  */
164
- set(
165
- paramDetails: Partial<UtilsGMCookieSetOptions>,
166
- callback = (error?: Error) => {}
167
- ) {
162
+ set(option: UtilsGMCookieSetOptions, callback?: (error?: Error) => void) {
163
+ let errorInfo;
168
164
  try {
169
- let details: Partial<UtilsGMCookieSetOptions> = {
165
+ let defaultOption: Required<UtilsGMCookieSetOptions> = {
170
166
  url: this.windowApi.window.location.href,
171
167
  name: "",
172
168
  value: "",
@@ -179,46 +175,52 @@ class UtilsGMCookie {
179
175
  */
180
176
  expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
181
177
  };
182
- details = Utils.assign(details, paramDetails);
183
- let life = details.expirationDate
184
- ? details.expirationDate
178
+ defaultOption = Utils.assign(defaultOption, option);
179
+ let life = defaultOption.expirationDate
180
+ ? defaultOption.expirationDate
185
181
  : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
186
182
  let cookieStr =
187
- details.name +
183
+ defaultOption.name +
188
184
  "=" +
189
- decodeURIComponent(details.value as string) +
185
+ decodeURIComponent(defaultOption.value as string) +
190
186
  ";expires=" +
191
187
  (new Date(life) as any).toGMTString() +
192
188
  "; path=/";
193
189
  this.windowApi.document.cookie = cookieStr;
194
- callback();
195
190
  } catch (error: any) {
196
- callback(error);
191
+ errorInfo = error;
192
+ } finally {
193
+ if (typeof callback === "function") {
194
+ callback(errorInfo);
195
+ }
197
196
  }
198
197
  }
199
198
  /**
200
199
  * 删除Cookie
201
- * @param paramDetails 配置
200
+ * @param option 配置
202
201
  * @param callback 删除操作后的回调(成功/失败)
203
202
  */
204
203
  delete(
205
- paramDetails: Partial<UtilsGMCookieDeleteOptions>,
206
- callback = (error?: Error) => {}
204
+ option: UtilsGMCookieDeleteOptions,
205
+ callback?: (error?: Error) => void
207
206
  ) {
207
+ let errorInfo;
208
208
  try {
209
- let details: Partial<UtilsGMCookieDeleteOptions> = {
209
+ let defaultOption: Required<UtilsGMCookieDeleteOptions> = {
210
210
  url: this.windowApi.window.location.href,
211
211
  name: "",
212
- // @ts-ignore
213
- firstPartyDomain: "",
212
+ path: "/",
213
+ firstPartyDomain: this.windowApi.window.location.hostname,
214
214
  };
215
- details = Utils.assign(details, paramDetails);
216
- let cookieStr =
217
- details.name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
215
+ defaultOption = Utils.assign(defaultOption, option);
216
+ let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}; domain=${defaultOption.firstPartyDomain};`;
218
217
  this.windowApi.document.cookie = cookieStr;
219
- callback();
220
218
  } catch (error: any) {
221
- callback(error);
219
+ errorInfo = error;
220
+ } finally {
221
+ if (typeof callback === "function") {
222
+ callback(errorInfo);
223
+ }
222
224
  }
223
225
  }
224
226
  }
@@ -1,52 +1,90 @@
1
1
  export interface UtilsGMCookieResult {
2
2
  /** 为 window.location.hostname */
3
3
  domain: string;
4
+ /** 过期时间 */
4
5
  expirationDate: null;
5
6
  hostOnly: true;
6
7
  httpOnly: false;
8
+ /** Cookie名 */
7
9
  name: string;
10
+ /** Cookie的路径 */
8
11
  path: "/";
12
+ /** Cookie是否同源策略 */
9
13
  sameSite: "unspecified";
10
14
  secure: true;
11
15
  session: false;
16
+ /** Cookie值 */
12
17
  value: string;
13
18
  }
14
19
 
15
20
  export interface UtilsGMCookieListOptions {
16
21
  /** 默认为当前的url */
17
- url: string;
18
- /** 默认为当前的域名(window.location.hostname) */
19
- domain: string;
22
+ url?: string;
23
+ /**
24
+ * Cookie所在域
25
+ * @default window.location.hostname
26
+ */
27
+ domain?: string;
20
28
  /** 需要检索的Cookie的名字 */
21
29
  name: string | RegExp;
22
- /** 需要检索的Cookie的路径,默认为"/" */
23
- path: string;
30
+ /**
31
+ * 需要检索的Cookie的路径
32
+ * @default "/"
33
+ */
34
+ path?: string;
24
35
  }
25
36
 
26
37
  export interface UtilsGMCookieSetOptions {
27
- /** 默认为当前的url */
38
+ /**
39
+ * 默认为当前的url
40
+ */
28
41
  url?: string;
29
- /** 默认为当前的域名(window.location.hostname) */
42
+ /**
43
+ * Cookie所在域
44
+ * @default window.location.hostname
45
+ */
30
46
  domain?: string;
31
47
  /** 需要检索的Cookie的名字 */
32
- name?: string;
33
- /** 需要检索的Cookie的路径,默认为"/" */
48
+ name: string;
49
+ /**
50
+ * 需要检索的Cookie的路径
51
+ * @default "/"
52
+ */
34
53
  path?: string;
35
- /** 值 */
36
- value?: string | number;
37
- /** 确保Cookie只在通过安全协议(如HTTPS)的情况下传输 */
54
+ /** Cookie值 */
55
+ value: string | number;
56
+ /**
57
+ * 确保Cookie只在通过安全协议(如HTTPS)的情况下传输
58
+ * @default true
59
+ */
38
60
  secure?: boolean;
39
- /** 是否防止JavaScript代码访问Cookie */
61
+ /**
62
+ * 是否防止JavaScript代码访问Cookie
63
+ * @default false
64
+ */
40
65
  httpOnly?: boolean;
41
- /** Cookie过期时间,默认为30天 */
66
+ /**
67
+ * Cookie过期时间的时间戳,默认为30天
68
+ * @default Math.floor(Date.now()) + 60 * 60 * 24 * 30
69
+ */
42
70
  expirationDate?: number;
43
71
  }
44
72
 
45
73
  export interface UtilsGMCookieDeleteOptions {
46
- /** 默认为当前的url */
47
- url: string;
48
74
  /** 需要检索的Cookie的名字 */
49
75
  name: string;
76
+ /** 默认为当前的url */
77
+ url?: string;
78
+ /**
79
+ * Cookie的路径
80
+ * @default "/"
81
+ */
82
+ path?: string;
83
+ /**
84
+ * Cookie所在域
85
+ * @default window.location.hostname
86
+ */
87
+ firstPartyDomain?: string;
50
88
  }
51
89
 
52
90
  export interface WindowApiOption {
@@ -121,20 +121,32 @@ export declare interface Vue2Instance {
121
121
  back: () => void;
122
122
  go: (...args: any[]) => void;
123
123
  replace: (...args: any[]) => void;
124
+ /** 添加路由 */
124
125
  addRoute: (...args: any[]) => void;
125
126
  addRoutes: (...args: any[]) => void;
126
127
  [key: string]: any;
127
128
  };
128
129
  $ssrContext: any;
129
-
130
+ /** 观察者 @returns 取消观察者 */
130
131
  $watch: (
132
+ /** 需要观察的属性 */
131
133
  key: string | string[] | (() => any),
132
- handler: (this: any, newVal: any, oldVal: any) => void,
134
+ /** 属性改变时触发的回调 */
135
+ handler: (
136
+ this: Vue2Instance,
137
+ /** 新值,也就是改变后的值 */
138
+ newVal: any,
139
+ /** 旧值,也就是改变前的值 */
140
+ oldVal: any
141
+ ) => void,
142
+ /** 监听配置 */
133
143
  options?: {
144
+ /** 是否立即执行handler */
134
145
  immediate?: boolean;
146
+ /** 是否深度监听 */
135
147
  deep?: boolean;
136
148
  }
137
- ) => void;
149
+ ) => Function;
138
150
 
139
151
  [key: string]: any;
140
152
  }
@@ -22,3 +22,6 @@ export declare interface AnyObject {
22
22
  [key: string]: any | AnyObject;
23
23
  toString(): string;
24
24
  }
25
+ export type PartialKeys<T, K extends keyof T> = {
26
+ [P in K]?: T[P];
27
+ };