ddan-js 2.9.3 → 2.9.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.
Files changed (133) hide show
  1. package/bin/ddan-js.browser.js +1 -1
  2. package/bin/ddan-js.esm.js +1 -1
  3. package/bin/ddan-js.js +1 -1
  4. package/bin/types/browser.d.ts +3 -3
  5. package/bin/types/index.d.ts +4 -3
  6. package/bin/types/modules/fetch.d.ts +1 -1
  7. package/bin/types/modules/node/index.d.ts +2 -0
  8. package/package.json +18 -13
  9. package/bin/lib/browser.js +0 -111
  10. package/bin/lib/class/event.js +0 -104
  11. package/bin/lib/class/getset.js +0 -12
  12. package/bin/lib/class/icon.js +0 -94
  13. package/bin/lib/class/joker.js +0 -121
  14. package/bin/lib/class/kvalue.js +0 -29
  15. package/bin/lib/class/mapping.js +0 -82
  16. package/bin/lib/class/persist.js +0 -65
  17. package/bin/lib/class/pipeTask.js +0 -42
  18. package/bin/lib/class/pipeparallel.js +0 -24
  19. package/bin/lib/class/store.js +0 -87
  20. package/bin/lib/class/tracker.js +0 -125
  21. package/bin/lib/class/watermark.js +0 -63
  22. package/bin/lib/common/_Symbol.js +0 -5
  23. package/bin/lib/common/_freeGlobal.js +0 -5
  24. package/bin/lib/common/_object.js +0 -40
  25. package/bin/lib/common/_root.js +0 -10
  26. package/bin/lib/common/internal/asciiToArray.js +0 -13
  27. package/bin/lib/common/internal/castSlice.js +0 -18
  28. package/bin/lib/common/internal/createCaseFirst.js +0 -30
  29. package/bin/lib/common/internal/hasUnicode.js +0 -26
  30. package/bin/lib/common/internal/nodeTypes.js +0 -26
  31. package/bin/lib/common/internal/slice.js +0 -46
  32. package/bin/lib/common/internal/stringToArray.js +0 -18
  33. package/bin/lib/common/internal/unicodeToArray.js +0 -40
  34. package/bin/lib/common/internal/unicodeWords.js +0 -67
  35. package/bin/lib/index.js +0 -114
  36. package/bin/lib/modules/browser/ecdh.js +0 -124
  37. package/bin/lib/modules/browser/index.js +0 -4
  38. package/bin/lib/modules/cdn.js +0 -62
  39. package/bin/lib/modules/convert/base.js +0 -125
  40. package/bin/lib/modules/convert/base64.js +0 -114
  41. package/bin/lib/modules/convert/index.js +0 -99
  42. package/bin/lib/modules/convert/md5.js +0 -222
  43. package/bin/lib/modules/convert/pkcs.js +0 -16
  44. package/bin/lib/modules/convert/utf8.js +0 -106
  45. package/bin/lib/modules/crypto/base64.js +0 -14
  46. package/bin/lib/modules/crypto/const.js +0 -7
  47. package/bin/lib/modules/crypto/index.js +0 -30
  48. package/bin/lib/modules/crypto/rsa.js +0 -143
  49. package/bin/lib/modules/crypto/tea.js +0 -79
  50. package/bin/lib/modules/crypto/uuid.js +0 -34
  51. package/bin/lib/modules/css.js +0 -137
  52. package/bin/lib/modules/decorator.js +0 -14
  53. package/bin/lib/modules/fetch.js +0 -82
  54. package/bin/lib/modules/gbk.js +0 -46
  55. package/bin/lib/modules/hook/base.js +0 -60
  56. package/bin/lib/modules/hook/hooker.js +0 -39
  57. package/bin/lib/modules/hook/index.js +0 -49
  58. package/bin/lib/modules/hook/log.js +0 -54
  59. package/bin/lib/modules/hook/modules/bezier.js +0 -121
  60. package/bin/lib/modules/hook/modules/debounce.js +0 -127
  61. package/bin/lib/modules/hook/modules/mutex.js +0 -22
  62. package/bin/lib/modules/hook/modules/pipeline.js +0 -59
  63. package/bin/lib/modules/hook/modules/polling.js +0 -20
  64. package/bin/lib/modules/hook/modules/safeTask.js +0 -25
  65. package/bin/lib/modules/hook/modules/throttle.js +0 -30
  66. package/bin/lib/modules/html.js +0 -293
  67. package/bin/lib/modules/http/index.js +0 -171
  68. package/bin/lib/modules/http/interceptor.js +0 -22
  69. package/bin/lib/modules/list/base.js +0 -37
  70. package/bin/lib/modules/list/index.js +0 -90
  71. package/bin/lib/modules/logger.js +0 -55
  72. package/bin/lib/modules/math/index.js +0 -118
  73. package/bin/lib/modules/mini/diff.js +0 -97
  74. package/bin/lib/modules/mini/http/index.js +0 -155
  75. package/bin/lib/modules/mini/index.js +0 -71
  76. package/bin/lib/modules/node/aliyun.js +0 -156
  77. package/bin/lib/modules/node/brotli.js +0 -46
  78. package/bin/lib/modules/node/child.js +0 -18
  79. package/bin/lib/modules/node/ecdh-web.js +0 -11
  80. package/bin/lib/modules/node/ecdh.js +0 -115
  81. package/bin/lib/modules/node/file.js +0 -25
  82. package/bin/lib/modules/node/index.js +0 -11
  83. package/bin/lib/modules/node/proxy.js +0 -147
  84. package/bin/lib/modules/node/rsa-web.js +0 -19
  85. package/bin/lib/modules/node/socks5.js +0 -424
  86. package/bin/lib/modules/obj/index.js +0 -184
  87. package/bin/lib/modules/qs/base.js +0 -5
  88. package/bin/lib/modules/qs/decode.js +0 -45
  89. package/bin/lib/modules/qs/encode.js +0 -53
  90. package/bin/lib/modules/qs/index.js +0 -114
  91. package/bin/lib/modules/regex.js +0 -16
  92. package/bin/lib/modules/rsa.js +0 -18
  93. package/bin/lib/modules/rule/async-validator/index.js +0 -306
  94. package/bin/lib/modules/rule/async-validator/interface.js +0 -2
  95. package/bin/lib/modules/rule/async-validator/messages.js +0 -59
  96. package/bin/lib/modules/rule/async-validator/rule/enum.js +0 -13
  97. package/bin/lib/modules/rule/async-validator/rule/index.js +0 -16
  98. package/bin/lib/modules/rule/async-validator/rule/pattern.js +0 -27
  99. package/bin/lib/modules/rule/async-validator/rule/range.js +0 -54
  100. package/bin/lib/modules/rule/async-validator/rule/required.js +0 -12
  101. package/bin/lib/modules/rule/async-validator/rule/type.js +0 -98
  102. package/bin/lib/modules/rule/async-validator/rule/url.js +0 -55
  103. package/bin/lib/modules/rule/async-validator/rule/whitespace.js +0 -20
  104. package/bin/lib/modules/rule/async-validator/util.js +0 -255
  105. package/bin/lib/modules/rule/async-validator/validator/any.js +0 -16
  106. package/bin/lib/modules/rule/async-validator/validator/array.js +0 -19
  107. package/bin/lib/modules/rule/async-validator/validator/boolean.js +0 -20
  108. package/bin/lib/modules/rule/async-validator/validator/date.js +0 -31
  109. package/bin/lib/modules/rule/async-validator/validator/enum.js +0 -20
  110. package/bin/lib/modules/rule/async-validator/validator/float.js +0 -20
  111. package/bin/lib/modules/rule/async-validator/validator/index.js +0 -36
  112. package/bin/lib/modules/rule/async-validator/validator/integer.js +0 -20
  113. package/bin/lib/modules/rule/async-validator/validator/method.js +0 -19
  114. package/bin/lib/modules/rule/async-validator/validator/number.js +0 -23
  115. package/bin/lib/modules/rule/async-validator/validator/object.js +0 -19
  116. package/bin/lib/modules/rule/async-validator/validator/pattern.js +0 -19
  117. package/bin/lib/modules/rule/async-validator/validator/regexp.js +0 -19
  118. package/bin/lib/modules/rule/async-validator/validator/required.js +0 -10
  119. package/bin/lib/modules/rule/async-validator/validator/string.js +0 -24
  120. package/bin/lib/modules/rule/async-validator/validator/type.js +0 -20
  121. package/bin/lib/modules/rule/index.js +0 -67
  122. package/bin/lib/modules/string/index.js +0 -121
  123. package/bin/lib/modules/string/words.js +0 -33
  124. package/bin/lib/modules/time/const.js +0 -11
  125. package/bin/lib/modules/time/dtime.js +0 -132
  126. package/bin/lib/modules/time/frame.js +0 -49
  127. package/bin/lib/modules/time/index.js +0 -84
  128. package/bin/lib/typings/index.js +0 -2
  129. package/bin/lib/util/function.js +0 -33
  130. package/bin/lib/util/includes.js +0 -96
  131. package/bin/lib/util/index.js +0 -26
  132. package/bin/lib/util/is.js +0 -148
  133. package/bin/lib/util/to.js +0 -47
@@ -1,293 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- // import math from './math'
4
- const qs_1 = require("./qs");
5
- // interface IDowloadCallback {
6
- // success?: () => void
7
- // progress?: (percentage: number, current: number, total: number) => void
8
- // fail?: (error: any) => void
9
- // }
10
- const dataURLtoFile = (dataurl, filename) => {
11
- // const _dataurl = decodeURIComponent(dataurl);
12
- let arr = dataurl.split(',');
13
- let mime = arr[0].match(/:(.*?);/)[1];
14
- let bstr = window.atob(arr[1]);
15
- let n = bstr.length;
16
- let u8arr = new Uint8Array(n);
17
- while (n--) {
18
- u8arr[n] = bstr.charCodeAt(n);
19
- }
20
- return new File([u8arr], filename, { type: mime });
21
- };
22
- const dataURLtoBlob = (dataurl) => {
23
- let arr = dataurl.split(',');
24
- let mime = arr[0].match(/:(.*?);/)[1];
25
- let bstr = window.atob(arr[1]);
26
- let n = bstr.length;
27
- let u8arr = new Uint8Array(n);
28
- while (n--) {
29
- u8arr[n] = bstr.charCodeAt(n);
30
- }
31
- return new Blob([u8arr], { type: mime });
32
- };
33
- const blobToFile = (blob, fileName) => {
34
- blob.lastModifiedDate = new Date();
35
- blob.name = fileName;
36
- return blob;
37
- };
38
- const readAsDataURL = (file, cb) => {
39
- var reader = new FileReader();
40
- reader.readAsDataURL(file);
41
- // 监听文件读取结束后事件
42
- reader.onloadend = function (e) {
43
- cb && cb(e.target?.result);
44
- };
45
- };
46
- const _downloadUrl = (url, filename) => {
47
- try {
48
- const ele = document.createElement('a');
49
- ele.style.display = 'none';
50
- ele.href = url;
51
- ele.download = filename;
52
- document.body.appendChild(ele);
53
- ele.click();
54
- document.body.removeChild(ele);
55
- }
56
- catch (err) {
57
- console.error(`[ddan] _downloadUrl ${url}`, err);
58
- }
59
- };
60
- const downloadUrl = (url, filename = '', checkSomeOrigin = false
61
- // opts?: {
62
- // success?: () => void
63
- // progress?: (percentage: number, current: number, total: number) => void
64
- // fail?: (error: any) => void
65
- // }
66
- ) => {
67
- try {
68
- if (!url)
69
- return;
70
- const map = qs_1.default.parseUrl(url);
71
- const _filename = filename || map.name;
72
- const sameOrigin = !filename || !checkSomeOrigin || !map.origin || map.origin === window.location.origin;
73
- if (sameOrigin) {
74
- _downloadUrl(url, _filename);
75
- // opts?.success && opts?.success()
76
- }
77
- else {
78
- const params = map.params;
79
- params.t = Date.now();
80
- const _url = `${map.url}?${qs_1.default.stringify(params, { uri: false })}`;
81
- const xhr = new XMLHttpRequest();
82
- xhr.open('GET', _url, true);
83
- xhr.responseType = 'blob';
84
- // xhr.onprogress = function (ev) {
85
- // if (ev.lengthComputable && opts?.progress) {
86
- // const percentage = math.float((100.0 * ev.loaded) / ev.total, { digits: 2 })
87
- // opts?.progress(percentage, ev.loaded, ev.total) // 调用回调函数更新进度
88
- // }
89
- // }
90
- xhr.onload = function () {
91
- downloadFile(xhr.response, _filename);
92
- // opts?.success && opts?.success()
93
- };
94
- xhr.onerror = function (e) {
95
- console.error(`[ddan] downloadUrl ${url}`, e);
96
- // opts?.fail && opts?.fail(e)
97
- };
98
- xhr.send();
99
- }
100
- }
101
- catch (err) {
102
- console.error(`[ddan] downloadUrl ${url}`, err);
103
- }
104
- };
105
- const getBlob = (url) => {
106
- return new Promise((resolve) => {
107
- const xhr = new XMLHttpRequest();
108
- xhr.open('GET', url, true);
109
- xhr.responseType = 'blob';
110
- xhr.onload = () => {
111
- if (xhr.status === 200) {
112
- resolve(xhr.response);
113
- }
114
- };
115
- xhr.send();
116
- });
117
- };
118
- const downloadFile = (data, filename = '') => {
119
- try {
120
- if (!data)
121
- return;
122
- const href = URL?.createObjectURL(data);
123
- if (!href)
124
- return;
125
- downloadUrl(href, filename);
126
- URL?.revokeObjectURL(href);
127
- }
128
- catch (err) {
129
- console.error(`[ddan] downloadFile`, err);
130
- }
131
- };
132
- const download = (urlOrFile, filename = '') => {
133
- if (!urlOrFile)
134
- return;
135
- if (typeof urlOrFile === 'string') {
136
- downloadUrl(urlOrFile, filename);
137
- return;
138
- }
139
- downloadFile(urlOrFile, filename);
140
- };
141
- const downloadImage = (url) => {
142
- try {
143
- if (!url)
144
- return;
145
- const name = url.match(/\/(\w+.\w+)$/)[1];
146
- //下载群二维码
147
- const img = new Image();
148
- img.onload = () => {
149
- let canvas = document.createElement('canvas');
150
- canvas.width = img.width;
151
- canvas.height = img.height;
152
- let context = canvas.getContext('2d');
153
- context && context.drawImage(img, 0, 0, img.width, img.height);
154
- let base64 = canvas.toDataURL(); //得到图片的base64编码数据
155
- downloadUrl(base64, name);
156
- };
157
- img.src = url; // 将canvas对象转换为图片的data url
158
- img.setAttribute('crossOrigin', 'Anonymous');
159
- }
160
- catch (err) {
161
- console.error(`[ddan] downloadImage`, err);
162
- }
163
- return Promise.resolve();
164
- };
165
- const watermark = (text, { width = 400, height = 300, angle = 0, fillStyle = 'rgba(0, 0, 0, 0.2)', font = '16px Arial', textAlign = 'left', textBaseline = 'middle', } = {}) => {
166
- try {
167
- if (!text)
168
- return '';
169
- const canvas = document.createElement('canvas');
170
- canvas.width = width;
171
- canvas.height = height;
172
- const ctx = canvas.getContext('2d');
173
- if (!ctx)
174
- return '';
175
- angle && ctx.rotate((angle * Math.PI) / 180);
176
- fillStyle && (ctx.fillStyle = fillStyle);
177
- font && (ctx.font = font);
178
- textAlign && (ctx.textAlign = textAlign);
179
- textBaseline && (ctx.textBaseline = textBaseline);
180
- ctx.fillText(text, canvas.width / 10, canvas.height / 2);
181
- return `url(${canvas.toDataURL()})`; // "image/png" 默认
182
- }
183
- catch (err) {
184
- console.error(`[ddan] watermark`, err);
185
- return '';
186
- }
187
- };
188
- const copyText = (text, legacy = false) => {
189
- return new Promise((resolve) => {
190
- if (legacy || !navigator.clipboard) {
191
- resolve(legacyCopy(text));
192
- return;
193
- }
194
- queryPermission('clipboard-write')
195
- .then((state) => {
196
- if (isAllowed(state, false)) {
197
- navigator.clipboard
198
- .writeText(text)
199
- .then(() => resolve(true))
200
- .catch(() => resolve(legacyCopy(text)));
201
- }
202
- else {
203
- resolve(legacyCopy(text));
204
- }
205
- })
206
- .catch(() => {
207
- resolve(legacyCopy(text));
208
- });
209
- });
210
- };
211
- const legacyCopy = (text) => {
212
- try {
213
- const _textarea = document.createElement('textarea');
214
- document.body.appendChild(_textarea);
215
- // 隐藏此输入框
216
- _textarea.style.position = 'fixed';
217
- _textarea.style.clip = 'rect(0 0 0 0)';
218
- _textarea.style.top = '10px';
219
- _textarea.style.opacity = '0';
220
- // 赋值
221
- _textarea.value = text ?? '';
222
- // 选中
223
- _textarea.select();
224
- // 复制
225
- const result = document.execCommand('copy');
226
- // 移除输入框
227
- document.body.removeChild(_textarea);
228
- return result;
229
- }
230
- catch (err) {
231
- console.error(`[ddan] copyText`, err);
232
- return false;
233
- }
234
- };
235
- const queryPermission = (name, def = 'denied') => {
236
- return new Promise((resolve) => {
237
- const permissions = navigator?.permissions;
238
- if (!permissions || !permissions.query)
239
- return resolve(def);
240
- const desc = { name };
241
- permissions
242
- .query(desc)
243
- .then((res) => resolve(res.state))
244
- .catch(() => resolve(def));
245
- });
246
- };
247
- const isAllowed = (status, prompt = true) => {
248
- if (prompt && status === 'prompt')
249
- return true;
250
- return status === 'granted';
251
- };
252
- const takeHtmlLinks = (htmlContent) => {
253
- if (!htmlContent)
254
- return [];
255
- const regexs = [
256
- /<script[^>]+src="([^"]+)"/g,
257
- /<link[^>]+rel="stylesheet"[^>]+href="([^"]+)"/g,
258
- /<link[^>]+rel="icon"[^>]+href="([^"]+)"/g,
259
- ];
260
- const results = [];
261
- regexs.forEach((e) => {
262
- const r = takeHtml(htmlContent, e);
263
- results.push(r);
264
- });
265
- return [].concat(...results);
266
- };
267
- const takeHtml = (htmlContent, regex) => {
268
- if (!htmlContent || !regex)
269
- return [];
270
- const results = [];
271
- let match;
272
- while ((match = regex.exec(htmlContent)) !== null) {
273
- results.push(match[1]);
274
- }
275
- return results;
276
- };
277
- exports.default = {
278
- dataURLtoFile,
279
- dataURLtoBlob,
280
- blobToFile,
281
- readAsDataURL,
282
- downloadUrl,
283
- download,
284
- downloadFile,
285
- downloadImage,
286
- watermark,
287
- copyText,
288
- queryPermission,
289
- isAllowed,
290
- legacyCopy,
291
- takeHtmlLinks,
292
- takeHtml,
293
- };
@@ -1,171 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const interceptor_1 = require("./interceptor");
4
- const util_1 = require("../../util");
5
- const qs_1 = require("../qs");
6
- const request = ({ url, options }) => {
7
- const headers = {
8
- "Content-Type": "application/json; charset=utf-8",
9
- ...(options.headers || {})
10
- };
11
- options.headers = headers;
12
- return new Promise((resolve, reject) => {
13
- fetch(url, options)
14
- .then(result => {
15
- resolve(result.json());
16
- })
17
- .catch((error) => {
18
- reject(error);
19
- });
20
- });
21
- };
22
- function handleResponse(result) {
23
- const { response } = result;
24
- return response.code === 0
25
- ? Promise.resolve(response)
26
- : Promise.reject(response);
27
- }
28
- function handleDefaultRequest(httpRequest) {
29
- const { url = "", host, options } = httpRequest;
30
- const result = httpRequest;
31
- if (!url.startsWith("http://") && !url.startsWith("https://")) {
32
- result.url = (host || "") + url;
33
- }
34
- if (options.method.toUpperCase() === "GET" &&
35
- options.body) {
36
- const data = JSON.parse(options.body);
37
- if (util_1.default.isPlainObject(data)) {
38
- // const query = Object.keys(data)
39
- // .map(
40
- // (key) => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`
41
- // )
42
- // .join("&");
43
- const query = qs_1.default.stringify(data);
44
- const symbol = url.indexOf("?") === -1 ? "?" : "&";
45
- result.url = `${result.url || ""}${symbol}${query}`;
46
- delete result.options.body;
47
- }
48
- }
49
- if (options && options.plugins) {
50
- delete result.options?.plugins;
51
- }
52
- if (options && options.model) {
53
- delete result.options?.model;
54
- }
55
- return result;
56
- }
57
- class Http {
58
- host = "";
59
- reqConfig = {};
60
- handleError;
61
- interceptors = {
62
- request: new interceptor_1.default(),
63
- response: new interceptor_1.default(),
64
- };
65
- constructor(_) {
66
- this.host = _.host || "";
67
- this.reqConfig = _.config;
68
- this.interceptors.request.use(_.handleRequest || []);
69
- this.interceptors.request.use(handleDefaultRequest);
70
- this.interceptors.response.use(_.handleResponse || handleResponse);
71
- this.handleError = _.handleError;
72
- }
73
- // 阻塞请求的锁id
74
- static blockId = "blockid";
75
- // 请求锁字典
76
- static requestLock = {};
77
- /**
78
- * 加锁
79
- * @param {string} id
80
- * @param {function} func
81
- */
82
- lockR(id, func) {
83
- Http.requestLock[id] = {
84
- lock: true,
85
- request: func(),
86
- };
87
- }
88
- /**
89
- * 解锁
90
- * @param {string} id
91
- */
92
- unlockR(id) {
93
- delete Http.requestLock[id];
94
- }
95
- /**
96
- * 根据URL和请求参数构造唯一标识符
97
- * @param {object} req
98
- */
99
- getIdFromReq(req) {
100
- const { url, data = {} } = req;
101
- return url + JSON.stringify(data);
102
- }
103
- /**
104
- * 根据URL构造唯一标识符
105
- * @param {object} req
106
- */
107
- getIdFromReqUrl(req) {
108
- return req.url || "";
109
- }
110
- async fetch(name, payload) {
111
- const _options = util_1.default.copy(this.reqConfig[name]) || {};
112
- const config = {
113
- host: this.host,
114
- url: _options.url || "",
115
- options: {
116
- ...(_options || {}),
117
- body: JSON.stringify({
118
- ...(payload || {}),
119
- }),
120
- }
121
- };
122
- try {
123
- const reqOptions = await this.interceptors.request.invoke(config);
124
- delete reqOptions.options.plugins;
125
- let result;
126
- // 处理阻塞/去抖/普通请求
127
- if (_options.model === "block") {
128
- this.lockR(Http.blockId, () => request(reqOptions));
129
- result = await Http.requestLock[Http.blockId].request;
130
- // note: 在具体的回调函数中主动 unlock
131
- }
132
- else {
133
- if (Http.requestLock[Http.blockId] &&
134
- Http.requestLock[Http.blockId].lock) {
135
- await Http.requestLock[Http.blockId].request;
136
- }
137
- //去抖模式
138
- if (_options.model === "debounce" || _options.model === "debounceUrl") {
139
- let id;
140
- if (_options.model === "debounceUrl") {
141
- // 仅针对url去抖的临时方案
142
- id = this.getIdFromReqUrl(reqOptions);
143
- }
144
- else {
145
- id = this.getIdFromReq(reqOptions);
146
- }
147
- if (!Http.requestLock[id]) {
148
- this.lockR(id, () => request(reqOptions));
149
- }
150
- result = await Http.requestLock[id].request;
151
- this.unlockR(id);
152
- }
153
- else {
154
- result = await request(reqOptions);
155
- }
156
- }
157
- return this.interceptors.response.invoke({
158
- response: result,
159
- config: _options,
160
- refetch: () => {
161
- return { sig: "REFETCH", result: this.fetch(name, payload) };
162
- },
163
- });
164
- }
165
- catch (error) {
166
- this.handleError && this.handleError(error);
167
- return Promise.reject(error);
168
- }
169
- }
170
- }
171
- exports.default = Http;
@@ -1,22 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- class Interceptor {
4
- list = [];
5
- constructor() {
6
- this.invoke = this.invoke.bind(this);
7
- }
8
- use(func) {
9
- this.list = this.list.concat(func);
10
- }
11
- invoke(result) {
12
- return this.list.reduce((acc, func) => acc.then((res) => func(res)), Promise.resolve(result)).catch((res) => {
13
- if (res.sig === "REFETCH") {
14
- return Promise.resolve(res.result);
15
- }
16
- else {
17
- return Promise.reject(res);
18
- }
19
- });
20
- }
21
- }
22
- exports.default = Interceptor;
@@ -1,37 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- /**
4
- * 分组
5
- * @param list 列表
6
- * @param key 关键字
7
- * @returns
8
- */
9
- function groupBy(list, key) {
10
- return list.reduce((prev, current) => {
11
- (prev[current[key]] = prev[current[key]] || []).push(current);
12
- return prev;
13
- }, {});
14
- }
15
- ;
16
- function first(list) {
17
- if (!list)
18
- return undefined;
19
- const len = list.length;
20
- if (len <= 0)
21
- return undefined;
22
- return list[0];
23
- }
24
- function last(list) {
25
- if (!list)
26
- return undefined;
27
- const len = list.length;
28
- if (len <= 0)
29
- return undefined;
30
- return list[len - 1];
31
- }
32
- function toList(val) {
33
- if (Array.isArray(val))
34
- return val;
35
- return val ? [val] : [];
36
- }
37
- exports.default = { groupBy, first, last, toList };
@@ -1,90 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const is_1 = require("../../util/is");
4
- const math_1 = require("../math");
5
- const base_1 = require("./base");
6
- /**
7
- * 分步动作
8
- * @param list 列表
9
- * @param func 动作function
10
- * @param stepCount 每步数量
11
- * @returns
12
- */
13
- function stepAction(list, func, stepCount = 100) {
14
- if (!is_1.default.isFunction(func))
15
- return;
16
- if (!is_1.default.isArray(list) || list.length <= 0)
17
- return;
18
- let start = 0;
19
- while (list.length > start) {
20
- let endIndex = stepCount + start;
21
- let arr = list.slice(start, endIndex);
22
- func && func(arr);
23
- start = endIndex;
24
- }
25
- }
26
- /**
27
- * 跳过数据
28
- * @param list 列表
29
- * @param count 跳过数量
30
- * @returns
31
- */
32
- function skip(list, count) {
33
- if (!is_1.default.isArray(list) || list.length <= 0)
34
- return [];
35
- const start = count;
36
- return list.slice(start);
37
- }
38
- ;
39
- /**
40
- * 提取数据
41
- * @param list 列表
42
- * @param count 提取数量
43
- * @param skip 跳过数量
44
- * @returns
45
- */
46
- function take(list, count, skip = 0) {
47
- if (!is_1.default.isArray(list) || list.length <= 0)
48
- return [];
49
- const start = skip || 0;
50
- if (start >= list.length)
51
- return [];
52
- const endIndex = count + start;
53
- return list.slice(start, list.length < endIndex ? list.length : endIndex) || [];
54
- }
55
- ;
56
- /**
57
- * 去重
58
- * @param list
59
- * @returns
60
- */
61
- function distinct(list) {
62
- if (!is_1.default.isArray(list) || list.length <= 0)
63
- return [];
64
- return [...new Set(list)];
65
- }
66
- ;
67
- function randoms(list, count = 1, repeat = false) {
68
- const max = list.length || 0;
69
- const arr = math_1.default.randoms(max, count, repeat);
70
- return arr.map(e => list[e]) || [];
71
- }
72
- ;
73
- const toKV = (list, key, value) => {
74
- const result = {};
75
- if (!key || !value)
76
- return result;
77
- (list || []).forEach(ele => {
78
- if (!ele || !is_1.default.isObject(ele))
79
- return;
80
- const _key = ele[key];
81
- if (is_1.default.isString(_key) && _key !== "") {
82
- result[_key] = ele[value];
83
- }
84
- else if (is_1.default.isNumber(_key)) {
85
- result[_key] = ele[value];
86
- }
87
- });
88
- return result;
89
- };
90
- exports.default = { ...base_1.default, stepAction, skip, take, distinct, randoms, toKV };
@@ -1,55 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const time_1 = require("./time");
4
- const __config = {
5
- overrode: false,
6
- tag: false,
7
- format: 'hh:mm:ss.SSS'
8
- };
9
- const override = () => {
10
- if (__config.overrode)
11
- return;
12
- __config.overrode = true;
13
- const rewriteFunctions = ['log', 'info', 'warn', 'error'];
14
- rewriteFunctions.forEach((key) => {
15
- const rewrite = console[key];
16
- console[key] = (...args) => {
17
- _apply(rewrite, key, ...args);
18
- };
19
- });
20
- };
21
- const config = ({ tag = __config.tag, format = __config.format } = {}) => {
22
- __config.tag = tag;
23
- __config.format = format;
24
- };
25
- const _override = (key) => {
26
- return (...args) => {
27
- const _func = console[key];
28
- if (__config.overrode) {
29
- _func(...args);
30
- return;
31
- }
32
- _apply(_func, key, ...args);
33
- };
34
- };
35
- const _apply = (handler, key, ...args) => {
36
- const tDesc = time_1.default.format(new Date(), __config.format);
37
- const kDesc = __config.tag ? `[${key}]` : '';
38
- if (kDesc && tDesc) {
39
- handler.apply(console, [`${tDesc} ${kDesc}`, ...args]);
40
- }
41
- else if (tDesc) {
42
- handler.apply(console, [`${tDesc}`, ...args]);
43
- }
44
- else if (kDesc) {
45
- handler.apply(console, [`${kDesc}`, ...args]);
46
- }
47
- else {
48
- handler.apply(console, [...args]);
49
- }
50
- };
51
- const log = _override("log");
52
- const info = _override("info");
53
- const warn = _override("warn");
54
- const error = _override("error");
55
- exports.default = { override, log, info, warn, error, config };