nhanh-pure-function 1.2.4 → 1.2.6

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/lib/User.d.ts CHANGED
@@ -128,47 +128,6 @@ export function _Exhibit_details<T>(
128
128
  }
129
129
  ): T;
130
130
 
131
- // 定义加载状态更新函数类型
132
- type LoadingStateUpdater = (newState: boolean) => void;
133
- // 定义加载控制器的类型
134
- interface LoadingController {
135
- invokers: Set<any>; // 假设invoker可以是任何类型
136
- timer: number | null;
137
- startTime: number;
138
- loadingState: LoadingStateUpdater;
139
- delayTime: number;
140
- minDisplayTime: number;
141
- }
142
- /** 多组loading控制器 */
143
- export class _LoadingController {
144
- #controllersCollection: Map<string, LoadingController>;
145
-
146
- // addController方法的类型定义
147
- addController(
148
- key: string,
149
- config: {
150
- loadingState: LoadingStateUpdater;
151
- delayTime?: number;
152
- minDisplayTime?: number;
153
- }
154
- ): void;
155
-
156
- // deleteController方法的类型定义
157
- deleteController(key: string): void;
158
-
159
- // getController方法的类型定义
160
- getController(key?: string): LoadingController;
161
-
162
- // resetController方法的类型定义
163
- resetController(key: string): void;
164
-
165
- // startLoading方法的类型定义
166
- startLoading(invoker: any, key?: string): void;
167
-
168
- // stopLoading方法的类型定义
169
- stopLoading(invoker: any, key?: string): void;
170
- }
171
-
172
131
  type UiLibrary = "naiveUI" | "ElementPlus" | "Element";
173
132
  /**
174
133
  * 点击非指定dom(包含子级dom)时执行 callback
package/lib/User.js CHANGED
@@ -294,97 +294,6 @@ export function _Exhibit_details(data, options = {}) {
294
294
  return data;
295
295
  }
296
296
 
297
- /** 多组loading控制器 */
298
- export class _LoadingController {
299
- #controllersCollection = new Map();
300
- constructor() {}
301
-
302
- addController(key = "default", config) {
303
- if (this.#controllersCollection.has(key))
304
- throw new Error("key为: " + key + " 的loading控制器已存在, 请重命名。");
305
-
306
- const {
307
- loadingState /** 更新/获取 loading 状态的方法 */,
308
- delayTime = 200 /** 延迟时间 */,
309
- minDisplayTime = 400 /** 最少显示时间 */,
310
- } = config;
311
- this.#controllersCollection.set(key, {
312
- invokers: new Set(),
313
- timer: null,
314
- startTime: 0,
315
- loadingState,
316
- delayTime,
317
- minDisplayTime,
318
- });
319
- }
320
-
321
- deleteController(key) {
322
- this.#controllersCollection.delete(key);
323
- }
324
-
325
- getController(key = "default") {
326
- const controller = this.#controllersCollection.get(key);
327
- if (!controller)
328
- throw new Error("还未添加key为: " + key + " 的loading控制器");
329
- return controller;
330
- }
331
-
332
- resetController(key) {
333
- const controller = this.getController(key);
334
- const { invokers, loadingState, timer } = controller;
335
- invokers.clear();
336
- loadingState(false);
337
- controller.startTime = 0;
338
- if (timer) clearTimeout(timer);
339
- controller.timer = null;
340
- }
341
-
342
- startLoading(invoker, key) {
343
- const controller = this.getController(key);
344
- const { invokers, timer, loadingState, delayTime } = controller;
345
-
346
- invokers.add(invoker);
347
-
348
- if (invokers.size > 1) return;
349
-
350
- if (delayTime) {
351
- if (timer) {
352
- return;
353
- } else if (!loadingState()) {
354
- controller.timer = setTimeout(() => {
355
- loadingState(true);
356
- controller.startTime = +new Date();
357
- controller.timer = null;
358
- }, delayTime);
359
- }
360
- } else {
361
- loadingState(true);
362
- controller.startTime = +new Date();
363
- }
364
- }
365
-
366
- stopLoading(invoker, key) {
367
- const controller = this.getController(key);
368
- const { invokers, startTime, minDisplayTime } = controller;
369
-
370
- const isFinished = invokers.has(invoker) && invokers.size == 1;
371
-
372
- if (isFinished) {
373
- const displayTime = +new Date() - startTime;
374
- if (displayTime >= minDisplayTime) {
375
- this.resetController(key);
376
- } else {
377
- setTimeout(
378
- () => this.stopLoading(invoker, key),
379
- displayTime - minDisplayTime
380
- );
381
- }
382
- } else {
383
- invokers.delete(invoker);
384
- }
385
- }
386
- }
387
-
388
297
  /**
389
298
  * 点击非指定dom(包含子级dom)时执行 callback
390
299
  * @param querySelector 允许点击的 dom 顶层祖先元素选择器
package/lib/Utility.d.ts CHANGED
@@ -81,7 +81,7 @@ export function _MergeObjects<T, T1>(A: T, B: T1): T & T1;
81
81
  export function _TimeTransition(
82
82
  time: number | Date,
83
83
  template: string,
84
- pad: boolean
84
+ pad?: boolean
85
85
  ): string;
86
86
 
87
87
  /**
@@ -202,3 +202,35 @@ export function _Throttle<T extends Function>(
202
202
  fn: T,
203
203
  delay: number
204
204
  ): (...args: ExtractParameters<T>) => void;
205
+
206
+ /**
207
+ * 数据类型
208
+ * @param {any} value
209
+ * @returns string
210
+ */
211
+ export function _DataType(
212
+ value: string
213
+ ):
214
+ | "string"
215
+ | "number"
216
+ | "bigint"
217
+ | "boolean"
218
+ | "symbol"
219
+ | "undefined"
220
+ | "object"
221
+ | "function"
222
+ | "array"
223
+ | "null";
224
+
225
+ /**
226
+ * 复制到剪贴板
227
+ * @param {string} text
228
+ */
229
+ export function _CopyToClipboard(text: string): Promise<void>;
230
+
231
+ /**
232
+ * 格式化文件大小
233
+ * @param {number} size
234
+ * @returns string
235
+ */
236
+ export function _FormatFileSize(size: number): string;
package/lib/Utility.js CHANGED
@@ -335,7 +335,15 @@ export function _CreateAndDownloadFile(content, fileName, options) {
335
335
  * @returns {Object}
336
336
  */
337
337
  export function _GetQueryParams(url) {
338
- return Object.fromEntries(new URL(url).searchParams);
338
+ const queryString = url.split("?")[1] || "";
339
+ const params = new URLSearchParams(queryString);
340
+ const result = {};
341
+
342
+ params.forEach((value, key) => {
343
+ result[key] = value;
344
+ });
345
+
346
+ return result;
339
347
  }
340
348
 
341
349
  /**
@@ -381,3 +389,100 @@ export function _Throttle(fn, delay) {
381
389
  }
382
390
  };
383
391
  }
392
+
393
+ /**
394
+ * 数据类型
395
+ * @param {any} value
396
+ * @returns string
397
+ */
398
+ export function _DataType(value) {
399
+ if (Array.isArray(value)) return "array";
400
+ if (value === null) return "null";
401
+ return typeof value;
402
+ }
403
+
404
+ /**
405
+ * 复制到剪贴板
406
+ * @param {string} text
407
+ */
408
+ export function _CopyToClipboard(text) {
409
+ const handleSuccess = () => Promise.resolve();
410
+ const handleError = (error) => {
411
+ console.error(error);
412
+ return Promise.reject(error);
413
+ };
414
+
415
+ /** 最新方式 */
416
+ function writeText() {
417
+ return navigator.clipboard
418
+ .writeText(text)
419
+ .then(handleSuccess)
420
+ .catch(handleError);
421
+ }
422
+ /** 旧方式 - createRange */
423
+ function createRange() {
424
+ const div = document.createElement("div");
425
+ div.innerText = text;
426
+ document.body.appendChild(div);
427
+
428
+ const range = document.createRange();
429
+ range.selectNodeContents(div);
430
+ const selection = window.getSelection();
431
+
432
+ let isFinished = false;
433
+ if (selection) {
434
+ selection.removeAllRanges();
435
+ selection.addRange(range);
436
+
437
+ isFinished = document.execCommand("copy");
438
+ }
439
+ document.body.removeChild(div);
440
+ return isFinished ? Promise.resolve() : Promise.reject();
441
+ }
442
+ /** 旧方式 - execCommand */
443
+ function execCommand() {
444
+ const textarea = document.createElement("textarea");
445
+ textarea.value = text;
446
+ document.body.appendChild(textarea);
447
+
448
+ textarea.select();
449
+ textarea.setSelectionRange(0, text.length); // 对于移动设备
450
+
451
+ let isFinished = false;
452
+
453
+ /** aria-hidden 及 tabindex 可能会影响聚焦元素 */
454
+ if (document.activeElement === textarea)
455
+ isFinished = document.execCommand("Copy", true);
456
+
457
+ document.body.removeChild(textarea);
458
+ return isFinished ? Promise.resolve() : Promise.reject();
459
+ }
460
+
461
+ function old() {
462
+ return createRange()
463
+ .then(handleSuccess)
464
+ .catch(() => {
465
+ execCommand()
466
+ .then(handleSuccess)
467
+ .catch(() => handleError("复制方式尽皆失效"));
468
+ });
469
+ }
470
+
471
+ if (navigator.clipboard) return writeText().catch(old);
472
+ return old();
473
+ }
474
+
475
+ /**
476
+ * 格式化文件大小
477
+ * @param {number} size
478
+ * @returns string
479
+ */
480
+ export function _FormatFileSize(size) {
481
+ const units = ["B", "KB", "MB", "GB", "TB", "PB"];
482
+ let unitIndex = 0;
483
+ while (size > 1024) {
484
+ size /= 1024;
485
+ unitIndex++;
486
+ }
487
+ return `${Math.round(size * 100) / 100} ${units[unitIndex]}`;
488
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nhanh-pure-function",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "纯函数工具",
5
5
  "main": "lib/Index.js",
6
6
  "scripts": {