util-helpers 5.7.7 → 5.8.0

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.
@@ -0,0 +1,54 @@
1
+ var ConcurrencyController = (function () {
2
+ function ConcurrencyController(maxConcurrency) {
3
+ if (maxConcurrency === void 0) { maxConcurrency = 2; }
4
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
5
+ this.runningCount = 0;
6
+ this.queue = [];
7
+ this.isPaused = false;
8
+ }
9
+ ConcurrencyController.prototype.add = function (task) {
10
+ var _this = this;
11
+ return new Promise(function (resolve, reject) {
12
+ _this.queue.push({ task: task, resolve: resolve, reject: reject });
13
+ _this._run();
14
+ });
15
+ };
16
+ ConcurrencyController.prototype._run = function () {
17
+ var _this = this;
18
+ if (this.isPaused)
19
+ return;
20
+ while (this.runningCount < this.maxConcurrency && this.queue.length > 0) {
21
+ var _a = this.queue.shift(), task = _a.task, resolve = _a.resolve, reject = _a.reject;
22
+ this.runningCount++;
23
+ task()
24
+ .then(resolve)
25
+ .catch(reject)
26
+ .finally(function () {
27
+ _this.runningCount--;
28
+ _this._run();
29
+ });
30
+ }
31
+ };
32
+ ConcurrencyController.prototype.getStatus = function () {
33
+ return {
34
+ running: this.runningCount,
35
+ waiting: this.queue.length,
36
+ maxConcurrency: this.maxConcurrency,
37
+ paused: this.isPaused
38
+ };
39
+ };
40
+ ConcurrencyController.prototype.setMaxConcurrency = function (maxConcurrency) {
41
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
42
+ this._run();
43
+ };
44
+ ConcurrencyController.prototype.pause = function () {
45
+ this.isPaused = true;
46
+ };
47
+ ConcurrencyController.prototype.resume = function () {
48
+ this.isPaused = false;
49
+ this._run();
50
+ };
51
+ return ConcurrencyController;
52
+ }());
53
+
54
+ export { ConcurrencyController as default };
package/esm/VERSION.js CHANGED
@@ -1,3 +1,3 @@
1
- var VERSION = "5.7.7";
1
+ var VERSION = "5.8.0";
2
2
 
3
3
  export { VERSION as default };
package/esm/index.js CHANGED
@@ -68,3 +68,4 @@ export { default as findTreeSelect } from './findTreeSelect.js';
68
68
  export { setDisableWarning } from './utils/config.js';
69
69
  export { default as VERSION } from './VERSION.js';
70
70
  export { default as AsyncMemo } from './AsyncMemo.js';
71
+ export { default as ConcurrencyController } from './ConcurrencyController.js';
package/esm/isIdCard.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { toString } from 'ut2';
2
2
 
3
- var regIdCard = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)$/i;
3
+ var regIdCard = /^[1-9]\d{5}(19|20)?\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)?$/i;
4
4
  function check(id) {
5
5
  var index, sum;
6
6
  for (sum = index = 0; index < 17; index++) {
@@ -16,10 +16,16 @@ function check(id) {
16
16
  }
17
17
  function isIdCard(value, options) {
18
18
  if (options === void 0) { options = {}; }
19
- var _a = options.checkCode, checkCode = _a === void 0 ? true : _a;
19
+ var _a = options.loose, loose = _a === void 0 ? false : _a, _b = options.checkCode, checkCode = _b === void 0 ? true : _b;
20
20
  var valueStr = toString(value);
21
+ if (valueStr.length === 15 && loose) {
22
+ return regIdCard.test(valueStr);
23
+ }
21
24
  if (valueStr.length === 18 && regIdCard.test(valueStr)) {
22
- return checkCode ? check(valueStr) : true;
25
+ if (checkCode) {
26
+ return check(valueStr);
27
+ }
28
+ return true;
23
29
  }
24
30
  return false;
25
31
  }
@@ -0,0 +1,56 @@
1
+ 'use strict';
2
+
3
+ var ConcurrencyController = (function () {
4
+ function ConcurrencyController(maxConcurrency) {
5
+ if (maxConcurrency === void 0) { maxConcurrency = 2; }
6
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
7
+ this.runningCount = 0;
8
+ this.queue = [];
9
+ this.isPaused = false;
10
+ }
11
+ ConcurrencyController.prototype.add = function (task) {
12
+ var _this = this;
13
+ return new Promise(function (resolve, reject) {
14
+ _this.queue.push({ task: task, resolve: resolve, reject: reject });
15
+ _this._run();
16
+ });
17
+ };
18
+ ConcurrencyController.prototype._run = function () {
19
+ var _this = this;
20
+ if (this.isPaused)
21
+ return;
22
+ while (this.runningCount < this.maxConcurrency && this.queue.length > 0) {
23
+ var _a = this.queue.shift(), task = _a.task, resolve = _a.resolve, reject = _a.reject;
24
+ this.runningCount++;
25
+ task()
26
+ .then(resolve)
27
+ .catch(reject)
28
+ .finally(function () {
29
+ _this.runningCount--;
30
+ _this._run();
31
+ });
32
+ }
33
+ };
34
+ ConcurrencyController.prototype.getStatus = function () {
35
+ return {
36
+ running: this.runningCount,
37
+ waiting: this.queue.length,
38
+ maxConcurrency: this.maxConcurrency,
39
+ paused: this.isPaused
40
+ };
41
+ };
42
+ ConcurrencyController.prototype.setMaxConcurrency = function (maxConcurrency) {
43
+ this.maxConcurrency = Math.max(maxConcurrency, 1);
44
+ this._run();
45
+ };
46
+ ConcurrencyController.prototype.pause = function () {
47
+ this.isPaused = true;
48
+ };
49
+ ConcurrencyController.prototype.resume = function () {
50
+ this.isPaused = false;
51
+ this._run();
52
+ };
53
+ return ConcurrencyController;
54
+ }());
55
+
56
+ module.exports = ConcurrencyController;
package/lib/VERSION.js CHANGED
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "5.7.7";
3
+ var VERSION = "5.8.0";
4
4
 
5
5
  module.exports = VERSION;
package/lib/index.js CHANGED
@@ -70,6 +70,7 @@ var findTreeSelect = require('./findTreeSelect.js');
70
70
  var config = require('./utils/config.js');
71
71
  var VERSION = require('./VERSION.js');
72
72
  var AsyncMemo = require('./AsyncMemo.js');
73
+ var ConcurrencyController = require('./ConcurrencyController.js');
73
74
 
74
75
 
75
76
 
@@ -143,3 +144,4 @@ exports.findTreeSelect = findTreeSelect;
143
144
  exports.setDisableWarning = config.setDisableWarning;
144
145
  exports.VERSION = VERSION;
145
146
  exports.AsyncMemo = AsyncMemo;
147
+ exports.ConcurrencyController = ConcurrencyController;
package/lib/isIdCard.js CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  var ut2 = require('ut2');
4
4
 
5
- var regIdCard = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)$/i;
5
+ var regIdCard = /^[1-9]\d{5}(19|20)?\d{2}((0[1-9])|(1[012]))(([0-2][1-9])|10|20|30|31)\d{3}(\d|X)?$/i;
6
6
  function check(id) {
7
7
  var index, sum;
8
8
  for (sum = index = 0; index < 17; index++) {
@@ -18,10 +18,16 @@ function check(id) {
18
18
  }
19
19
  function isIdCard(value, options) {
20
20
  if (options === void 0) { options = {}; }
21
- var _a = options.checkCode, checkCode = _a === void 0 ? true : _a;
21
+ var _a = options.loose, loose = _a === void 0 ? false : _a, _b = options.checkCode, checkCode = _b === void 0 ? true : _b;
22
22
  var valueStr = ut2.toString(value);
23
+ if (valueStr.length === 15 && loose) {
24
+ return regIdCard.test(valueStr);
25
+ }
23
26
  if (valueStr.length === 18 && regIdCard.test(valueStr)) {
24
- return checkCode ? check(valueStr) : true;
27
+ if (checkCode) {
28
+ return check(valueStr);
29
+ }
30
+ return true;
25
31
  }
26
32
  return false;
27
33
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "5.7.7",
3
+ "version": "5.8.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -1,9 +1,70 @@
1
+ type AsyncFunc<T = any> = () => Promise<T>;
2
+ /**
3
+ * 并发控制器
4
+ *
5
+ * @class
6
+ * @param {number} [maxConcurrency=2] 最大并发数量。默认 `2`。
7
+ * @example
8
+ * // 最多同时运行 3 个异步任务
9
+ * const controller = new ConcurrencyController(3);
10
+ *
11
+ * const urls = ['/api/1', '/api/2', '/api/3', '/api/4', '/api/5', '/api/6'];
12
+ * const results = await Promise.all(urls.map(url => controller.add(() => fetch(url))));
13
+ *
14
+ * // 获取状态
15
+ * controller.getStatus();
16
+ *
17
+ * // 暂停队列
18
+ * controller.pause();
19
+ *
20
+ * // 恢复队列
21
+ * controller.resume();
22
+ *
23
+ * // 设置最大并发数量,并立即运行队列中的任务。
24
+ * controller.setMaxConcurrency(4);
25
+ *
26
+ */
1
27
  declare class ConcurrencyController {
2
28
  private maxConcurrency;
3
29
  private runningCount;
4
30
  private queue;
5
- constructor(maxConcurrency: number);
6
- add(task: () => void | Promise<any>): void;
7
- private run;
31
+ private isPaused;
32
+ constructor(maxConcurrency?: number);
33
+ /**
34
+ * 加入队列
35
+ *
36
+ * @param {Function} task 异步任务函数。
37
+ * @returns 任务的 Promise 对象。
38
+ */
39
+ add<T>(task: AsyncFunc<T>): Promise<T>;
40
+ /**
41
+ * 运行队列中的任务
42
+ *
43
+ * @private
44
+ */
45
+ private _run;
46
+ /**
47
+ * 获取状态
48
+ */
49
+ getStatus(): {
50
+ running: number;
51
+ waiting: number;
52
+ maxConcurrency: number;
53
+ paused: boolean;
54
+ };
55
+ /**
56
+ * 设置最大并发数量,并立即运行队列中的任务。
57
+ *
58
+ * @param {number} maxConcurrency 最大并发数量。不能小于 `1`。
59
+ */
60
+ setMaxConcurrency(maxConcurrency: number): void;
61
+ /**
62
+ * 暂停队列
63
+ */
64
+ pause(): void;
65
+ /**
66
+ * 恢复队列
67
+ */
68
+ resume(): void;
8
69
  }
9
70
  export default ConcurrencyController;
package/types/index.d.ts CHANGED
@@ -141,3 +141,4 @@ export { setDisableWarning } from './utils/config';
141
141
  import VERSION from './VERSION';
142
142
  export { VERSION };
143
143
  export { default as AsyncMemo } from './AsyncMemo';
144
+ export { default as ConcurrencyController } from './ConcurrencyController';
@@ -1,4 +1,7 @@
1
1
  type Options = {
2
+ /**
3
+ * @deprecated 即将废弃。
4
+ */
2
5
  loose?: boolean;
3
6
  checkCode?: boolean;
4
7
  };