@whitesev/utils 1.0.5 → 1.0.8
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 +2123 -2269
- package/dist/index.amd.js.map +1 -1
- package/dist/index.cjs.js +2123 -2269
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +2123 -2269
- package/dist/index.esm.js.map +1 -1
- package/dist/index.iife.js +2123 -2269
- package/dist/index.iife.js.map +1 -1
- package/dist/index.system.js +2123 -2269
- package/dist/index.system.js.map +1 -1
- package/dist/index.umd.js +2123 -2269
- package/dist/index.umd.js.map +1 -1
- package/dist/src/Dictionary.d.ts +82 -0
- package/dist/src/Hooks.d.ts +11 -0
- package/dist/src/Httpx.d.ts +1201 -0
- package/dist/src/LockFunction.d.ts +31 -0
- package/dist/src/Log.d.ts +96 -0
- package/dist/src/Progress.d.ts +37 -0
- package/dist/src/UtilsGMMenu.d.ts +156 -0
- package/dist/src/index.d.ts +20 -27
- package/dist/src/indexedDB.d.ts +73 -0
- package/dist/src/tryCatch.d.ts +31 -0
- package/package.json +36 -36
- package/src/Dictionary.ts +152 -0
- package/src/{Hooks/Hooks.js → Hooks.ts} +31 -17
- package/src/{Httpx/index.d.ts → Httpx.ts} +837 -46
- package/src/LockFunction.ts +62 -0
- package/src/Log.ts +281 -0
- package/src/Progress.ts +143 -0
- package/src/UtilsGMMenu.ts +530 -0
- package/src/index.ts +17 -29
- package/src/indexedDB.ts +421 -0
- package/src/tryCatch.ts +107 -0
- package/tsconfig.json +1 -1
- package/dist/src/Dictionary/Dictionary.d.ts +0 -85
- package/dist/src/Hooks/Hooks.d.ts +0 -5
- package/dist/src/Httpx/Httpx.d.ts +0 -50
- package/dist/src/LockFunction/LockFunction.d.ts +0 -16
- package/dist/src/Log/Log.d.ts +0 -66
- package/dist/src/Progress/Progress.d.ts +0 -6
- package/dist/src/UtilsGMMenu/UtilsGMMenu.d.ts +0 -119
- package/dist/src/indexedDB/indexedDB.d.ts +0 -165
- package/dist/src/tryCatch/tryCatch.d.ts +0 -31
- package/src/Dictionary/Dictionary.js +0 -157
- package/src/Dictionary/index.d.ts +0 -52
- package/src/Hooks/index.d.ts +0 -16
- package/src/Httpx/Httpx.js +0 -747
- package/src/LockFunction/LockFunction.js +0 -35
- package/src/LockFunction/index.d.ts +0 -47
- package/src/Log/Log.js +0 -256
- package/src/Log/index.d.ts +0 -91
- package/src/Progress/Progress.js +0 -98
- package/src/Progress/index.d.ts +0 -30
- package/src/UtilsGMMenu/UtilsGMMenu.js +0 -464
- package/src/UtilsGMMenu/index.d.ts +0 -224
- package/src/indexedDB/index.d.ts +0 -128
- package/src/indexedDB/indexedDB.js +0 -355
- package/src/tryCatch/index.d.ts +0 -6
- package/src/tryCatch/tryCatch.js +0 -100
package/src/indexedDB/index.d.ts
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
/** indexedDB */
|
|
2
|
-
declare interface UtilsIndexedDBConstructor {
|
|
3
|
-
dbName: string;
|
|
4
|
-
slqVersion: string;
|
|
5
|
-
dbVersion: number;
|
|
6
|
-
storeName: string;
|
|
7
|
-
indexedDB: IDBFactory;
|
|
8
|
-
db: UtilsNestedObjectWithToString;
|
|
9
|
-
store: null;
|
|
10
|
-
errorCode: UtilsNestedObjectWithToString;
|
|
11
|
-
/**
|
|
12
|
-
* 创建 “表”
|
|
13
|
-
* @param dbName 表名
|
|
14
|
-
*/
|
|
15
|
-
createStore(dbName: string): any;
|
|
16
|
-
/**
|
|
17
|
-
* 打开数据库
|
|
18
|
-
* @param callback 回调
|
|
19
|
-
* @param dbName 数据库名
|
|
20
|
-
*/
|
|
21
|
-
open(
|
|
22
|
-
callback: (
|
|
23
|
-
store:
|
|
24
|
-
| UtilsNestedObjectWithToString
|
|
25
|
-
| {
|
|
26
|
-
code: number;
|
|
27
|
-
msg: string;
|
|
28
|
-
error: Error;
|
|
29
|
-
}
|
|
30
|
-
) => void,
|
|
31
|
-
dbName: string
|
|
32
|
-
): void;
|
|
33
|
-
/**
|
|
34
|
-
* 保存数据到数据库
|
|
35
|
-
* @param key 数据key
|
|
36
|
-
* @param value 数据值
|
|
37
|
-
*/
|
|
38
|
-
save(
|
|
39
|
-
key: string,
|
|
40
|
-
value: any
|
|
41
|
-
): Promise<{
|
|
42
|
-
code: number;
|
|
43
|
-
msg: string;
|
|
44
|
-
success: boolean;
|
|
45
|
-
}>;
|
|
46
|
-
/**
|
|
47
|
-
* 根据key获取值
|
|
48
|
-
* @param key 数据key
|
|
49
|
-
*/
|
|
50
|
-
get(key: string): Promise<
|
|
51
|
-
| {
|
|
52
|
-
code: number;
|
|
53
|
-
msg: string;
|
|
54
|
-
data: any[];
|
|
55
|
-
success: true;
|
|
56
|
-
}
|
|
57
|
-
| {
|
|
58
|
-
code: number;
|
|
59
|
-
msg: string;
|
|
60
|
-
error: Error;
|
|
61
|
-
result: any;
|
|
62
|
-
}
|
|
63
|
-
>;
|
|
64
|
-
/**
|
|
65
|
-
* 正则获取数据
|
|
66
|
-
* @param key 数据键
|
|
67
|
-
*/
|
|
68
|
-
regexpGet(key: string): Promise<
|
|
69
|
-
| {
|
|
70
|
-
code: number;
|
|
71
|
-
msg: string;
|
|
72
|
-
data: any[];
|
|
73
|
-
success: true;
|
|
74
|
-
}
|
|
75
|
-
| {
|
|
76
|
-
code: number;
|
|
77
|
-
msg: string;
|
|
78
|
-
error: Error;
|
|
79
|
-
result: any;
|
|
80
|
-
}
|
|
81
|
-
>;
|
|
82
|
-
/**
|
|
83
|
-
* 删除数据
|
|
84
|
-
* @param key 数据键
|
|
85
|
-
*/
|
|
86
|
-
delete(key: string): Promise<
|
|
87
|
-
| {
|
|
88
|
-
code: number;
|
|
89
|
-
msg: string;
|
|
90
|
-
success: true;
|
|
91
|
-
}
|
|
92
|
-
| {
|
|
93
|
-
code: number;
|
|
94
|
-
msg: string;
|
|
95
|
-
error: Error;
|
|
96
|
-
}
|
|
97
|
-
>;
|
|
98
|
-
/**
|
|
99
|
-
* 删除所有数据
|
|
100
|
-
*/
|
|
101
|
-
deleteAll(): Promise<
|
|
102
|
-
| {
|
|
103
|
-
code: number;
|
|
104
|
-
msg: string;
|
|
105
|
-
error: Error;
|
|
106
|
-
result: any;
|
|
107
|
-
}
|
|
108
|
-
| {
|
|
109
|
-
code: number;
|
|
110
|
-
msg: string;
|
|
111
|
-
success: true;
|
|
112
|
-
}
|
|
113
|
-
>;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/** indexedDB */
|
|
117
|
-
declare interface UtilsIndexedDB {
|
|
118
|
-
/**
|
|
119
|
-
* @param dbName 数据存储名,默认为:default_db
|
|
120
|
-
* @param storeName 表名,默认为:default_form
|
|
121
|
-
* @param dbVersion indexDB的版本号,默认为:1
|
|
122
|
-
*/
|
|
123
|
-
new (
|
|
124
|
-
dbName?: string,
|
|
125
|
-
storeName?: string,
|
|
126
|
-
dbVersion?: number
|
|
127
|
-
): UtilsIndexedDBConstructor;
|
|
128
|
-
}
|
|
@@ -1,355 +0,0 @@
|
|
|
1
|
-
const indexedDB = function (
|
|
2
|
-
dbName = "default_db",
|
|
3
|
-
storeName = "default_form",
|
|
4
|
-
dbVersion = 1
|
|
5
|
-
) {
|
|
6
|
-
this.dbName = dbName;
|
|
7
|
-
/* websql的版本号,由于ios的问题,版本号的写法不一样 */
|
|
8
|
-
this.slqVersion = "1";
|
|
9
|
-
this.dbVersion = dbVersion;
|
|
10
|
-
this.storeName = storeName;
|
|
11
|
-
/* 监听IndexDB */
|
|
12
|
-
this.indexedDB =
|
|
13
|
-
window.indexedDB ||
|
|
14
|
-
window.mozIndexedDB ||
|
|
15
|
-
window.webkitIndexedDB ||
|
|
16
|
-
window.msIndexedDB;
|
|
17
|
-
if (!this.indexedDB) {
|
|
18
|
-
alert("很抱歉,您的浏览器不支持indexedDB");
|
|
19
|
-
}
|
|
20
|
-
/* 缓存数据库,避免同一个页面重复创建和销毁 */
|
|
21
|
-
this.db = {};
|
|
22
|
-
this.store = null;
|
|
23
|
-
this.errorCode = {
|
|
24
|
-
/* 错误码 */
|
|
25
|
-
success: {
|
|
26
|
-
code: 200,
|
|
27
|
-
msg: "操作成功",
|
|
28
|
-
},
|
|
29
|
-
error: {
|
|
30
|
-
code: 401,
|
|
31
|
-
msg: "操作失败",
|
|
32
|
-
},
|
|
33
|
-
open: { code: 91001, msg: "打开数据库失败" },
|
|
34
|
-
save: { code: 91002, msg: "保存数据失败" },
|
|
35
|
-
get: { code: 91003, msg: "获取数据失败" },
|
|
36
|
-
delete: { code: 91004, msg: "删除数据失败" },
|
|
37
|
-
deleteAll: { code: 91005, msg: "清空数据库失败" },
|
|
38
|
-
};
|
|
39
|
-
let that = this;
|
|
40
|
-
/**
|
|
41
|
-
* 创建 “表”
|
|
42
|
-
* @param {string} dbName 表名
|
|
43
|
-
* @returns
|
|
44
|
-
*/
|
|
45
|
-
this.createStore = function (dbName) {
|
|
46
|
-
let txn, store;
|
|
47
|
-
if (that.indexedDB) {
|
|
48
|
-
/* 如果是支持IndexDB的 */
|
|
49
|
-
txn = that.db[dbName].transaction(that.storeName, "readwrite");
|
|
50
|
-
/* IndexDB的读写权限 */
|
|
51
|
-
store = txn.objectStore(that.storeName);
|
|
52
|
-
}
|
|
53
|
-
return store;
|
|
54
|
-
};
|
|
55
|
-
/**
|
|
56
|
-
* 打开数据库
|
|
57
|
-
* @param {function} callback 回调
|
|
58
|
-
* @param {string} dbName 数据库名
|
|
59
|
-
*/
|
|
60
|
-
this.open = function (callback, dbName) {
|
|
61
|
-
/* 打开数据库 */
|
|
62
|
-
if (that.indexedDB) {
|
|
63
|
-
/* 如果支持IndexDB */
|
|
64
|
-
if (!that.db[dbName]) {
|
|
65
|
-
/* 如果缓存中没有,则进行数据库的创建或打开,提高效率 */
|
|
66
|
-
let request = that.indexedDB.open(dbName, that.dbVersion);
|
|
67
|
-
request.onerror = function (e) {
|
|
68
|
-
callback({
|
|
69
|
-
code: that.errorCode.open.code,
|
|
70
|
-
msg: that.errorCode.open.msg,
|
|
71
|
-
error: e,
|
|
72
|
-
});
|
|
73
|
-
};
|
|
74
|
-
request.onsuccess = function (e) {
|
|
75
|
-
if (!that.db[dbName]) {
|
|
76
|
-
that.db[dbName] = e.target.result;
|
|
77
|
-
}
|
|
78
|
-
let store = that.createStore(dbName);
|
|
79
|
-
callback(store);
|
|
80
|
-
};
|
|
81
|
-
request.onupgradeneeded = function (e) {
|
|
82
|
-
that.db[dbName] = e.target.result;
|
|
83
|
-
let store = that.db[dbName].createObjectStore(that.storeName, {
|
|
84
|
-
keyPath: "key",
|
|
85
|
-
});
|
|
86
|
-
store.transaction.oncomplete = function (event) {
|
|
87
|
-
callback(store);
|
|
88
|
-
};
|
|
89
|
-
};
|
|
90
|
-
} else {
|
|
91
|
-
/* 如果缓存中已经打开了数据库,就直接使用 */
|
|
92
|
-
let store = that.createStore(dbName);
|
|
93
|
-
callback(store);
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
};
|
|
97
|
-
/**
|
|
98
|
-
* 保存数据到数据库
|
|
99
|
-
* @param {string} key 数据key
|
|
100
|
-
* @param {any} value 数据值
|
|
101
|
-
* @returns {Promise< {
|
|
102
|
-
* code: number,
|
|
103
|
-
* msg: string,
|
|
104
|
-
* success: boolean
|
|
105
|
-
* }>}
|
|
106
|
-
*/
|
|
107
|
-
this.save = async function (key, value) {
|
|
108
|
-
if (that.indexedDB) {
|
|
109
|
-
return new Promise((resolve, reject) => {
|
|
110
|
-
let dbName = that.dbName;
|
|
111
|
-
let inData = {
|
|
112
|
-
key: key,
|
|
113
|
-
value: value,
|
|
114
|
-
};
|
|
115
|
-
that.open(function (result) {
|
|
116
|
-
let error = result.hasOwnProperty("error");
|
|
117
|
-
if (error) {
|
|
118
|
-
resolve(result);
|
|
119
|
-
} else {
|
|
120
|
-
let request = result.put(inData);
|
|
121
|
-
request.onsuccess = function (e) {
|
|
122
|
-
/* 保存成功有success 字段 */
|
|
123
|
-
resolve({
|
|
124
|
-
code: that.errorCode.success.code,
|
|
125
|
-
msg: that.errorCode.success.msg,
|
|
126
|
-
success: true,
|
|
127
|
-
});
|
|
128
|
-
};
|
|
129
|
-
request.onerror = function (e) {
|
|
130
|
-
resolve({
|
|
131
|
-
code: that.errorCode.save.code,
|
|
132
|
-
msg: that.errorCode.save.msg,
|
|
133
|
-
error: e,
|
|
134
|
-
});
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
}, dbName);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
};
|
|
141
|
-
/**
|
|
142
|
-
* 根据key获取值
|
|
143
|
-
* @param {string} key 数据key
|
|
144
|
-
* @returns {Promise< {
|
|
145
|
-
* code: number,
|
|
146
|
-
* msg: string,
|
|
147
|
-
* data: [...any],
|
|
148
|
-
* success: true
|
|
149
|
-
* }| {
|
|
150
|
-
* code: number,
|
|
151
|
-
* msg: string,
|
|
152
|
-
* error: Error,
|
|
153
|
-
* result: any,
|
|
154
|
-
* } >}
|
|
155
|
-
*/
|
|
156
|
-
this.get = async function (key) {
|
|
157
|
-
return new Promise((resolve, reject) => {
|
|
158
|
-
let dbName = that.dbName;
|
|
159
|
-
if (that.indexedDB) {
|
|
160
|
-
that.open(function (result) {
|
|
161
|
-
/* 判断返回的数据中是否有error字段 */
|
|
162
|
-
let error = result.hasOwnProperty("error");
|
|
163
|
-
if (error) {
|
|
164
|
-
reject({
|
|
165
|
-
code: that.errorCode.open.get,
|
|
166
|
-
msg: that.errorCode.get.msg,
|
|
167
|
-
error: error,
|
|
168
|
-
result: result,
|
|
169
|
-
});
|
|
170
|
-
} else {
|
|
171
|
-
let request = result.get(key);
|
|
172
|
-
request.onsuccess = function (e) {
|
|
173
|
-
let result = e.target.result;
|
|
174
|
-
let data = result ? result.value : void 0;
|
|
175
|
-
resolve({
|
|
176
|
-
code: data
|
|
177
|
-
? that.errorCode.success.code
|
|
178
|
-
: that.errorCode.error.code,
|
|
179
|
-
msg: data
|
|
180
|
-
? that.errorCode.success.msg
|
|
181
|
-
: that.errorCode.error.msg,
|
|
182
|
-
data: data || [],
|
|
183
|
-
success: true,
|
|
184
|
-
});
|
|
185
|
-
};
|
|
186
|
-
request.onerror = function (e) {
|
|
187
|
-
reject({
|
|
188
|
-
code: that.errorCode.get.code,
|
|
189
|
-
msg: that.errorCode.get.msg,
|
|
190
|
-
result: result,
|
|
191
|
-
error: e,
|
|
192
|
-
});
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
}, dbName);
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
};
|
|
199
|
-
/**
|
|
200
|
-
* 正则获取数据
|
|
201
|
-
* @param {string} key 数据键
|
|
202
|
-
* @returns { Promise<{
|
|
203
|
-
* code: number,
|
|
204
|
-
* msg: string,
|
|
205
|
-
* data: [...any],
|
|
206
|
-
* success: true
|
|
207
|
-
* }|{
|
|
208
|
-
* code: number,
|
|
209
|
-
* msg: string,
|
|
210
|
-
* error: Error,
|
|
211
|
-
* result: any,
|
|
212
|
-
* }> }
|
|
213
|
-
*/
|
|
214
|
-
this.regexpGet = async function (key) {
|
|
215
|
-
let list = [];
|
|
216
|
-
return new Promise((resolve, reject) => {
|
|
217
|
-
/* 正则查询 */
|
|
218
|
-
let dbName = that.dbName;
|
|
219
|
-
if (that.indexedDB) {
|
|
220
|
-
that.open(function (result) {
|
|
221
|
-
/* 判断返回的数据中是否有error字段 */
|
|
222
|
-
let error = result.hasOwnProperty("error");
|
|
223
|
-
if (error) {
|
|
224
|
-
reject({
|
|
225
|
-
code: that.errorCode.open.get,
|
|
226
|
-
msg: that.errorCode.get.msg,
|
|
227
|
-
error: error,
|
|
228
|
-
result: result,
|
|
229
|
-
});
|
|
230
|
-
} else {
|
|
231
|
-
let request = result.getAll();
|
|
232
|
-
request.onsuccess = function (e) {
|
|
233
|
-
let result = e.target.result;
|
|
234
|
-
if (result.length !== 0) {
|
|
235
|
-
result.forEach((item, index) => {
|
|
236
|
-
if (item["key"].match(key)) {
|
|
237
|
-
let concatList = item["value"];
|
|
238
|
-
concatList["key"] = item["key"];
|
|
239
|
-
list = [...list, concatList];
|
|
240
|
-
}
|
|
241
|
-
});
|
|
242
|
-
}
|
|
243
|
-
resolve({
|
|
244
|
-
code: that.errorCode.success.code,
|
|
245
|
-
msg: that.errorCode.success.msg,
|
|
246
|
-
data: list,
|
|
247
|
-
success: true,
|
|
248
|
-
});
|
|
249
|
-
};
|
|
250
|
-
request.onerror = function (e) {
|
|
251
|
-
reject({
|
|
252
|
-
code: that.errorCode.get.code,
|
|
253
|
-
msg: that.errorCode.get.msg,
|
|
254
|
-
result: result,
|
|
255
|
-
error: e,
|
|
256
|
-
});
|
|
257
|
-
};
|
|
258
|
-
}
|
|
259
|
-
}, dbName);
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
};
|
|
263
|
-
/**
|
|
264
|
-
* 删除数据
|
|
265
|
-
* @param {string} key 数据键
|
|
266
|
-
* @returns {Promise<{
|
|
267
|
-
* code: number,
|
|
268
|
-
* msg: string,
|
|
269
|
-
* success: true,
|
|
270
|
-
* }|{
|
|
271
|
-
* code: number,
|
|
272
|
-
* msg: string,
|
|
273
|
-
* error: Error,
|
|
274
|
-
* }>}
|
|
275
|
-
*/
|
|
276
|
-
this.delete = async function (key) {
|
|
277
|
-
return new Promise((resolve, reject) => {
|
|
278
|
-
/* 根据key删除某条数据 */
|
|
279
|
-
let dbName = that.dbName;
|
|
280
|
-
if (that.indexedDB) {
|
|
281
|
-
that.open(function (result) {
|
|
282
|
-
let error = result.hasOwnProperty("error");
|
|
283
|
-
if (error) {
|
|
284
|
-
resolve(result);
|
|
285
|
-
} else {
|
|
286
|
-
let request = result.get(key);
|
|
287
|
-
request.onsuccess = function (e) {
|
|
288
|
-
let recode = e.target.result;
|
|
289
|
-
if (recode) {
|
|
290
|
-
request = result.delete(key);
|
|
291
|
-
}
|
|
292
|
-
resolve({
|
|
293
|
-
code: recode
|
|
294
|
-
? that.errorCode.success.code
|
|
295
|
-
: that.errorCode.error.code,
|
|
296
|
-
msg: recode
|
|
297
|
-
? that.errorCode.success.msg
|
|
298
|
-
: that.errorCode.error.msg,
|
|
299
|
-
success: true,
|
|
300
|
-
});
|
|
301
|
-
};
|
|
302
|
-
request.onerror = function (e) {
|
|
303
|
-
resolve({
|
|
304
|
-
code: that.errorCode.delete.code,
|
|
305
|
-
msg: that.errorCode.delete.msg,
|
|
306
|
-
error: e,
|
|
307
|
-
});
|
|
308
|
-
};
|
|
309
|
-
}
|
|
310
|
-
}, dbName);
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
};
|
|
314
|
-
/**
|
|
315
|
-
* 删除所有数据
|
|
316
|
-
* @returns {Promise<{
|
|
317
|
-
* code: number,
|
|
318
|
-
* msg: string,
|
|
319
|
-
* error: Error,
|
|
320
|
-
* result: any,
|
|
321
|
-
* }|{
|
|
322
|
-
* code: number,
|
|
323
|
-
* msg: string,
|
|
324
|
-
* success: true,
|
|
325
|
-
* }>}
|
|
326
|
-
*/
|
|
327
|
-
this.deleteAll = async function () {
|
|
328
|
-
return new Promise((resolve, reject) => {
|
|
329
|
-
/* 清空数据库 */
|
|
330
|
-
let dbName = that.dbName;
|
|
331
|
-
if (that.indexedDB) {
|
|
332
|
-
that.open(function (result) {
|
|
333
|
-
let error = result.hasOwnProperty("error");
|
|
334
|
-
if (error) {
|
|
335
|
-
resolve({
|
|
336
|
-
code: that.errorCode.deleteAll.code,
|
|
337
|
-
msg: that.errorCode.deleteAll.msg,
|
|
338
|
-
error: error,
|
|
339
|
-
result: result,
|
|
340
|
-
});
|
|
341
|
-
} else {
|
|
342
|
-
result.clear();
|
|
343
|
-
resolve({
|
|
344
|
-
code: that.errorCode.success.code,
|
|
345
|
-
msg: that.errorCode.success.msg,
|
|
346
|
-
success: true,
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
}, dbName);
|
|
350
|
-
}
|
|
351
|
-
});
|
|
352
|
-
};
|
|
353
|
-
};
|
|
354
|
-
|
|
355
|
-
export { indexedDB };
|
package/src/tryCatch/index.d.ts
DELETED
package/src/tryCatch/tryCatch.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*
|
|
3
|
-
* @param {...any} args
|
|
4
|
-
* @returns
|
|
5
|
-
*/
|
|
6
|
-
const TryCatch = function (...args) {
|
|
7
|
-
/* 定义变量和函数 */
|
|
8
|
-
let callbackFunction = null;
|
|
9
|
-
let context = null;
|
|
10
|
-
let handleError = null;
|
|
11
|
-
let defaultDetails = {
|
|
12
|
-
log: true,
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* @function tryCatchObj
|
|
16
|
-
* @description 空函数,用于链式调用。
|
|
17
|
-
*/
|
|
18
|
-
function tryCatchObj() {}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* 配置
|
|
22
|
-
* @param {{
|
|
23
|
-
* log: boolean
|
|
24
|
-
* }} paramDetails
|
|
25
|
-
*/
|
|
26
|
-
tryCatchObj.config = function (paramDetails) {
|
|
27
|
-
defaultDetails = Utils.assign(defaultDetails, paramDetails);
|
|
28
|
-
return tryCatchObj;
|
|
29
|
-
};
|
|
30
|
-
/**
|
|
31
|
-
* 设置错误处理函数。
|
|
32
|
-
* @param {function|string} handler 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
33
|
-
* @returns 返回 tryCatchObj 函数。
|
|
34
|
-
*/
|
|
35
|
-
tryCatchObj.error = function (handler) {
|
|
36
|
-
handleError = handler;
|
|
37
|
-
return tryCatchObj;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
42
|
-
* @param {function|string} callback 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
43
|
-
* @param {object|null} [__context__] 待执行函数的作用域,用于apply指定
|
|
44
|
-
* @returns 如果函数有返回值,则返回该返回值;否则返回 tryCatchObj 函数以支持链式调用。
|
|
45
|
-
* @throws {Error} 如果传入参数不符合要求,则会抛出相应类型的错误。
|
|
46
|
-
*/
|
|
47
|
-
tryCatchObj.run = function (callback, __context__) {
|
|
48
|
-
callbackFunction = callback;
|
|
49
|
-
context = __context__;
|
|
50
|
-
let result = executeTryCatch(callbackFunction, handleError, context);
|
|
51
|
-
return result !== void 0 ? result : tryCatchObj;
|
|
52
|
-
};
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 执行传入的函数并捕获其可能抛出的错误,并通过传入的错误处理函数进行处理。
|
|
56
|
-
* @param {function|string} callback - 待执行函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
57
|
-
* @param {function|string|null} handleErrorFunc - 错误处理函数,可以是 function 或者 string 类型。如果是 string 类型,则会被当做代码进行执行。
|
|
58
|
-
* @param {object|null} funcThis - 待执行函数的作用域,用于apply指定
|
|
59
|
-
* @returns {any|undefined} - 如果函数有返回值,则返回该返回值;否则返回 undefined。
|
|
60
|
-
*/
|
|
61
|
-
function executeTryCatch(callback, handleErrorFunc, funcThis) {
|
|
62
|
-
let result = void 0;
|
|
63
|
-
try {
|
|
64
|
-
if (typeof callback === "string") {
|
|
65
|
-
(function () {
|
|
66
|
-
eval(callback);
|
|
67
|
-
}).apply(funcThis, args);
|
|
68
|
-
} else {
|
|
69
|
-
result = callback.apply(funcThis, args);
|
|
70
|
-
}
|
|
71
|
-
} catch (error) {
|
|
72
|
-
if (defaultDetails.log) {
|
|
73
|
-
console.log(
|
|
74
|
-
`%c ${callback?.name ? callback?.name : callback + "出现错误"} `,
|
|
75
|
-
"color: #f20000"
|
|
76
|
-
);
|
|
77
|
-
console.log(`%c 错误原因:${error}`, "color: #f20000");
|
|
78
|
-
console.trace(callback);
|
|
79
|
-
}
|
|
80
|
-
if (handleErrorFunc) {
|
|
81
|
-
if (typeof handleErrorFunc === "string") {
|
|
82
|
-
result = function () {
|
|
83
|
-
return eval(handleErrorFunc);
|
|
84
|
-
}.apply(funcThis, [...args, error]);
|
|
85
|
-
} else {
|
|
86
|
-
result = handleErrorFunc.apply(funcThis, [...args, error]);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return result;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// 返回 tryCatchObj 函数
|
|
94
|
-
return tryCatchObj;
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
export {
|
|
99
|
-
TryCatch
|
|
100
|
-
}
|