util-helpers 5.7.8 → 5.8.1

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.8";
1
+ var VERSION = "5.8.1";
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 reg = /^[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++) {
@@ -19,9 +19,9 @@ function isIdCard(value, options) {
19
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
21
  if (valueStr.length === 15 && loose) {
22
- return regIdCard.test(valueStr);
22
+ return reg.test(valueStr);
23
23
  }
24
- if (valueStr.length === 18 && regIdCard.test(valueStr)) {
24
+ if (valueStr.length === 18 && reg.test(valueStr)) {
25
25
  if (checkCode) {
26
26
  return check(valueStr);
27
27
  }
@@ -18,7 +18,10 @@ function internalRandomString(len, pool, prefix) {
18
18
  var randomString = function (len, pool) {
19
19
  if (len === void 0) { len = 0; }
20
20
  var _pool;
21
- if (typeof pool !== 'string') {
21
+ if (Array.isArray(pool)) {
22
+ _pool = pool.map(function (p) { return chars[p]; }).join('');
23
+ }
24
+ else if (typeof pool !== 'string') {
22
25
  _pool = allChars;
23
26
  }
24
27
  else if (chars[pool]) {
@@ -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.8";
3
+ var VERSION = "5.8.1";
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 reg = /^[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++) {
@@ -21,9 +21,9 @@ function isIdCard(value, options) {
21
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
23
  if (valueStr.length === 15 && loose) {
24
- return regIdCard.test(valueStr);
24
+ return reg.test(valueStr);
25
25
  }
26
- if (valueStr.length === 18 && regIdCard.test(valueStr)) {
26
+ if (valueStr.length === 18 && reg.test(valueStr)) {
27
27
  if (checkCode) {
28
28
  return check(valueStr);
29
29
  }
@@ -20,7 +20,10 @@ function internalRandomString(len, pool, prefix) {
20
20
  var randomString = function (len, pool) {
21
21
  if (len === void 0) { len = 0; }
22
22
  var _pool;
23
- if (typeof pool !== 'string') {
23
+ if (Array.isArray(pool)) {
24
+ _pool = pool.map(function (p) { return chars[p]; }).join('');
25
+ }
26
+ else if (typeof pool !== 'string') {
24
27
  _pool = allChars;
25
28
  }
26
29
  else if (chars[pool]) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "5.7.8",
3
+ "version": "5.8.1",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -9,9 +9,9 @@
9
9
  "sideEffects": false,
10
10
  "types": "types/index.d.ts",
11
11
  "scripts": {
12
- "test": "jest --verbose",
13
- "test:coverage": "jest --coverage",
14
- "test:coverage:local": "cross-env COVERAGE_LOCAL=1 jest --coverage && open ./coverage/lcov-report/index.html",
12
+ "test": "jest --verbose --maxWorkers=50%",
13
+ "test:coverage": "jest --coverage --maxWorkers=50%",
14
+ "test:coverage:local": "cross-env COVERAGE_LOCAL=1 jest --coverage --maxWorkers=50% && open ./coverage/lcov-report/index.html",
15
15
  "build": "rm -rf esm lib dist && rollup -c && npm run build:types",
16
16
  "build:types": "rm -rf types && tsc -p tsconfig.build.json -d --outDir types --emitDeclarationOnly",
17
17
  "build:doc": "npm run doc",
@@ -3,8 +3,25 @@ type AsyncFunc<T = any> = () => Promise<T>;
3
3
  * 并发控制器
4
4
  *
5
5
  * @class
6
- * @param {number} [maxConcurrency=2] 最大并发数量。
6
+ * @param {number} [maxConcurrency=2] 最大并发数量。默认 `2`。
7
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);
8
25
  *
9
26
  */
10
27
  declare class ConcurrencyController {
@@ -13,15 +30,41 @@ declare class ConcurrencyController {
13
30
  private queue;
14
31
  private isPaused;
15
32
  constructor(maxConcurrency?: number);
33
+ /**
34
+ * 加入队列
35
+ *
36
+ * @param {Function} task 异步任务函数。
37
+ * @returns 任务的 Promise 对象。
38
+ */
16
39
  add<T>(task: AsyncFunc<T>): Promise<T>;
17
- private run;
40
+ /**
41
+ * 运行队列中的任务
42
+ *
43
+ * @private
44
+ */
45
+ private _run;
46
+ /**
47
+ * 获取状态
48
+ */
18
49
  getStatus(): {
19
50
  running: number;
20
51
  waiting: number;
21
52
  maxConcurrency: number;
53
+ paused: boolean;
22
54
  };
55
+ /**
56
+ * 设置最大并发数量,并立即运行队列中的任务。
57
+ *
58
+ * @param {number} maxConcurrency 最大并发数量。不能小于 `1`。
59
+ */
23
60
  setMaxConcurrency(maxConcurrency: number): void;
61
+ /**
62
+ * 暂停队列
63
+ */
24
64
  pause(): void;
65
+ /**
66
+ * 恢复队列
67
+ */
25
68
  resume(): void;
26
69
  }
27
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,6 +1,6 @@
1
1
  type Options = {
2
2
  /**
3
- * @deprecated 即将废弃。
3
+ * @deprecated 即将废弃,使用`checkCode`参数替代。
4
4
  */
5
5
  loose?: boolean;
6
6
  checkCode?: boolean;
@@ -1,6 +1,12 @@
1
+ declare const chars: {
2
+ number: string;
3
+ lower: string;
4
+ upper: string;
5
+ };
1
6
  interface RandomString {
2
- (len: number, poll: 'number' | 'lower' | 'upper'): string;
7
+ <T extends keyof typeof chars>(len: number, poll: T): string;
3
8
  (len: number, poll?: string): string;
9
+ <T extends keyof typeof chars>(len: number, poll: Array<T>): string;
4
10
  }
5
11
  /**
6
12
  * 生成随机字符串
@@ -9,7 +15,7 @@ interface RandomString {
9
15
  * @alias module:Other.randomString
10
16
  * @since 4.8.0
11
17
  * @param {number} [len=0] 长度,默认`0`
12
- * @param {'number' | 'lower' | 'upper' | string} [pool='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'] 字符池,默认为数字和大小写字母。支持设置类型`number` `lower` `upper` 或字符串。
18
+ * @param {'number' | 'lower' | 'upper' | string | Array<'number' | 'lower' | 'upper'>} [pool='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'] 字符池,默认为数字和大小写字母。支持设置类型`number` `lower` `upper` 或字符串,也支持数组组合。
13
19
  * @returns {string} 随机字符串
14
20
  * @example
15
21
  *
@@ -20,6 +26,10 @@ interface RandomString {
20
26
  * randomString(5, 'abc'); // ccbcb
21
27
  * randomString(8, 'abcefg'); // bcgcfabg
22
28
  *
29
+ * // 使用数组组合
30
+ * randomString(5, ['number', 'lower']); // 1a2b3
31
+ * randomString(8, ['upper', 'number']); // A1B2C3D4
32
+ *
23
33
  */
24
34
  declare const randomString: RandomString;
25
35
  export default randomString;