util-helpers 5.5.1 → 5.7.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.
package/esm/AsyncMemo.js CHANGED
@@ -1,11 +1,12 @@
1
1
  import { __assign } from 'tslib';
2
2
  import { Cache } from 'cache2';
3
- import { isString, uniqueId } from 'ut2';
3
+ import { isString } from 'ut2';
4
4
 
5
5
  var AsyncMemo = (function () {
6
- function AsyncMemo(options) {
6
+ function AsyncMemo(options, ns) {
7
+ if (ns === void 0) { ns = 'uh_async_memo'; }
7
8
  this.promiseCache = {};
8
- this.cache = new Cache(uniqueId('uh_async_memo'), options);
9
+ this.cache = new Cache(ns, options);
9
10
  }
10
11
  AsyncMemo.prototype.run = function (asyncFn, key, options) {
11
12
  var _this = this;
package/esm/VERSION.js CHANGED
@@ -1,3 +1,3 @@
1
- var VERSION = "5.5.1";
1
+ var VERSION = "5.7.0";
2
2
 
3
3
  export { VERSION as default };
package/esm/ajax.js CHANGED
@@ -42,7 +42,7 @@ function ajax(url, options) {
42
42
  loadend: onLoadEnd
43
43
  };
44
44
  var eventKeys = objectKeys(events);
45
- eventKeys.map(function (item) {
45
+ eventKeys.forEach(function (item) {
46
46
  var func = events[item];
47
47
  if (func) {
48
48
  xhr.addEventListener(item, func);
package/esm/divide.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { isNaN } from 'ut2';
2
- import { transformEffectiveNumber, float2Fixed, checkBoundary, strip, digitLength } from './utils/math.util.js';
2
+ import { transformEffectiveNumber, float2Fixed, strip, digitLength, checkBoundary } from './utils/math.util.js';
3
3
  import times from './times.js';
4
4
 
5
5
  function divide() {
package/esm/index.js CHANGED
@@ -20,7 +20,7 @@ export { default as isHMCard } from './isHMCard.js';
20
20
  export { default as isTWCard } from './isTWCard.js';
21
21
  export { default as isSwiftCode } from './isSwiftCode.js';
22
22
  export { default as isValidNumber } from './isValidNumber.js';
23
- export { default as checkResult } from './checkResult.js';
23
+ export { default as isUnifiedIdentifier } from './isUnifiedIdentifier.js';
24
24
  export { default as formatMoney } from './formatMoney.js';
25
25
  export { default as formatBankCard } from './formatBankCard.js';
26
26
  export { default as replaceChar } from './replaceChar.js';
@@ -57,6 +57,7 @@ export { default as calculateCursorPosition } from './calculateCursorPosition.js
57
57
  export { default as getExtname } from './getExtname.js';
58
58
  export { default as randomString } from './randomString.js';
59
59
  export { default as strlen } from './strlen.js';
60
+ export { default as checkResult } from './checkResult.js';
60
61
  export { default as transformFieldNames } from './transformFieldNames.js';
61
62
  export { default as listToTree } from './listToTree.js';
62
63
  export { default as treeToList } from './treeToList.js';
@@ -1,33 +1,5 @@
1
- import { toString } from 'ut2';
1
+ import isUnifiedIdentifier from './isUnifiedIdentifier.js';
2
2
 
3
- var baseReg = /^[\dA-HJ-NPQRTUWXY]{2}\d{6}[\dA-HJ-NPQRTUWXY]{10}$/;
4
- var baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
5
- function sumCheckCode(preCode) {
6
- var total = 0;
7
- var _loop_1 = function (i) {
8
- var index = baseCodeArr.findIndex(function (item) { return item === preCode[i]; });
9
- var wf = Math.pow(3, i) % 31;
10
- total += index * wf;
11
- };
12
- for (var i = 0; i < 17; i++) {
13
- _loop_1(i);
14
- }
15
- var remainder = total % 31;
16
- var checkCodeIndex = remainder !== 0 ? 31 - remainder : 0;
17
- return baseCodeArr[checkCodeIndex];
18
- }
19
- function isSocialCreditCode(value, options) {
20
- var valueStr = toString(value);
21
- var _a = (options || {}).checkCode, needCheckCode = _a === void 0 ? true : _a;
22
- var passBaseRule = baseReg.test(valueStr);
23
- if (!needCheckCode || !passBaseRule) {
24
- return passBaseRule;
25
- }
26
- var preCode = valueStr.substring(0, 17);
27
- var lastCode = valueStr.substring(valueStr.length - 1);
28
- var checkCode = sumCheckCode(preCode);
29
- return lastCode === checkCode;
30
- }
31
- isSocialCreditCode.sumCheckCode = sumCheckCode;
3
+ var isSocialCreditCode = isUnifiedIdentifier;
32
4
 
33
5
  export { isSocialCreditCode as default };
@@ -0,0 +1,33 @@
1
+ import { toString } from 'ut2';
2
+
3
+ var baseReg = /^[\dA-HJ-NPQRTUWXY]{2}\d{6}[\dA-HJ-NPQRTUWXY]{10}$/;
4
+ var baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
5
+ function sumCheckCode(preCode) {
6
+ var total = 0;
7
+ var _loop_1 = function (i) {
8
+ var index = baseCodeArr.findIndex(function (item) { return item === preCode[i]; });
9
+ var wf = Math.pow(3, i) % 31;
10
+ total += index * wf;
11
+ };
12
+ for (var i = 0; i < 17; i++) {
13
+ _loop_1(i);
14
+ }
15
+ var remainder = total % 31;
16
+ var checkCodeIndex = remainder !== 0 ? 31 - remainder : 0;
17
+ return baseCodeArr[checkCodeIndex];
18
+ }
19
+ function isUnifiedIdentifier(value, options) {
20
+ var valueStr = toString(value);
21
+ var _a = (options || {}).checkCode, needCheckCode = _a === void 0 ? true : _a;
22
+ var passBaseRule = baseReg.test(valueStr);
23
+ if (!needCheckCode || !passBaseRule) {
24
+ return passBaseRule;
25
+ }
26
+ var preCode = valueStr.substring(0, 17);
27
+ var lastCode = valueStr.substring(valueStr.length - 1);
28
+ var checkCode = sumCheckCode(preCode);
29
+ return lastCode === checkCode;
30
+ }
31
+ isUnifiedIdentifier.sumCheckCode = sumCheckCode;
32
+
33
+ export { isUnifiedIdentifier as default };
package/lib/AsyncMemo.js CHANGED
@@ -5,9 +5,10 @@ var cache2 = require('cache2');
5
5
  var ut2 = require('ut2');
6
6
 
7
7
  var AsyncMemo = (function () {
8
- function AsyncMemo(options) {
8
+ function AsyncMemo(options, ns) {
9
+ if (ns === void 0) { ns = 'uh_async_memo'; }
9
10
  this.promiseCache = {};
10
- this.cache = new cache2.Cache(ut2.uniqueId('uh_async_memo'), options);
11
+ this.cache = new cache2.Cache(ns, options);
11
12
  }
12
13
  AsyncMemo.prototype.run = function (asyncFn, key, options) {
13
14
  var _this = this;
package/lib/VERSION.js CHANGED
@@ -1,5 +1,5 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "5.5.1";
3
+ var VERSION = "5.7.0";
4
4
 
5
5
  module.exports = VERSION;
package/lib/ajax.js CHANGED
@@ -44,7 +44,7 @@ function ajax(url, options) {
44
44
  loadend: onLoadEnd
45
45
  };
46
46
  var eventKeys = native.objectKeys(events);
47
- eventKeys.map(function (item) {
47
+ eventKeys.forEach(function (item) {
48
48
  var func = events[item];
49
49
  if (func) {
50
50
  xhr.addEventListener(item, func);
package/lib/index.js CHANGED
@@ -22,7 +22,7 @@ var isHMCard = require('./isHMCard.js');
22
22
  var isTWCard = require('./isTWCard.js');
23
23
  var isSwiftCode = require('./isSwiftCode.js');
24
24
  var isValidNumber = require('./isValidNumber.js');
25
- var checkResult = require('./checkResult.js');
25
+ var isUnifiedIdentifier = require('./isUnifiedIdentifier.js');
26
26
  var formatMoney = require('./formatMoney.js');
27
27
  var formatBankCard = require('./formatBankCard.js');
28
28
  var replaceChar = require('./replaceChar.js');
@@ -59,6 +59,7 @@ var calculateCursorPosition = require('./calculateCursorPosition.js');
59
59
  var getExtname = require('./getExtname.js');
60
60
  var randomString = require('./randomString.js');
61
61
  var strlen = require('./strlen.js');
62
+ var checkResult = require('./checkResult.js');
62
63
  var transformFieldNames = require('./transformFieldNames.js');
63
64
  var listToTree = require('./listToTree.js');
64
65
  var treeToList = require('./treeToList.js');
@@ -94,7 +95,7 @@ exports.isHMCard = isHMCard;
94
95
  exports.isTWCard = isTWCard;
95
96
  exports.isSwiftCode = isSwiftCode;
96
97
  exports.isValidNumber = isValidNumber;
97
- exports.checkResult = checkResult;
98
+ exports.isUnifiedIdentifier = isUnifiedIdentifier;
98
99
  exports.formatMoney = formatMoney;
99
100
  exports.formatBankCard = formatBankCard;
100
101
  exports.replaceChar = replaceChar;
@@ -131,6 +132,7 @@ exports.calculateCursorPosition = calculateCursorPosition;
131
132
  exports.getExtname = getExtname;
132
133
  exports.randomString = randomString;
133
134
  exports.strlen = strlen;
135
+ exports.checkResult = checkResult;
134
136
  exports.transformFieldNames = transformFieldNames;
135
137
  exports.listToTree = listToTree;
136
138
  exports.treeToList = treeToList;
@@ -1,35 +1,7 @@
1
1
  'use strict';
2
2
 
3
- var ut2 = require('ut2');
3
+ var isUnifiedIdentifier = require('./isUnifiedIdentifier.js');
4
4
 
5
- var baseReg = /^[\dA-HJ-NPQRTUWXY]{2}\d{6}[\dA-HJ-NPQRTUWXY]{10}$/;
6
- var baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
7
- function sumCheckCode(preCode) {
8
- var total = 0;
9
- var _loop_1 = function (i) {
10
- var index = baseCodeArr.findIndex(function (item) { return item === preCode[i]; });
11
- var wf = Math.pow(3, i) % 31;
12
- total += index * wf;
13
- };
14
- for (var i = 0; i < 17; i++) {
15
- _loop_1(i);
16
- }
17
- var remainder = total % 31;
18
- var checkCodeIndex = remainder !== 0 ? 31 - remainder : 0;
19
- return baseCodeArr[checkCodeIndex];
20
- }
21
- function isSocialCreditCode(value, options) {
22
- var valueStr = ut2.toString(value);
23
- var _a = (options || {}).checkCode, needCheckCode = _a === void 0 ? true : _a;
24
- var passBaseRule = baseReg.test(valueStr);
25
- if (!needCheckCode || !passBaseRule) {
26
- return passBaseRule;
27
- }
28
- var preCode = valueStr.substring(0, 17);
29
- var lastCode = valueStr.substring(valueStr.length - 1);
30
- var checkCode = sumCheckCode(preCode);
31
- return lastCode === checkCode;
32
- }
33
- isSocialCreditCode.sumCheckCode = sumCheckCode;
5
+ var isSocialCreditCode = isUnifiedIdentifier;
34
6
 
35
7
  module.exports = isSocialCreditCode;
@@ -0,0 +1,35 @@
1
+ 'use strict';
2
+
3
+ var ut2 = require('ut2');
4
+
5
+ var baseReg = /^[\dA-HJ-NPQRTUWXY]{2}\d{6}[\dA-HJ-NPQRTUWXY]{10}$/;
6
+ var baseCodeArr = '0123456789ABCDEFGHJKLMNPQRTUWXY'.split('');
7
+ function sumCheckCode(preCode) {
8
+ var total = 0;
9
+ var _loop_1 = function (i) {
10
+ var index = baseCodeArr.findIndex(function (item) { return item === preCode[i]; });
11
+ var wf = Math.pow(3, i) % 31;
12
+ total += index * wf;
13
+ };
14
+ for (var i = 0; i < 17; i++) {
15
+ _loop_1(i);
16
+ }
17
+ var remainder = total % 31;
18
+ var checkCodeIndex = remainder !== 0 ? 31 - remainder : 0;
19
+ return baseCodeArr[checkCodeIndex];
20
+ }
21
+ function isUnifiedIdentifier(value, options) {
22
+ var valueStr = ut2.toString(value);
23
+ var _a = (options || {}).checkCode, needCheckCode = _a === void 0 ? true : _a;
24
+ var passBaseRule = baseReg.test(valueStr);
25
+ if (!needCheckCode || !passBaseRule) {
26
+ return passBaseRule;
27
+ }
28
+ var preCode = valueStr.substring(0, 17);
29
+ var lastCode = valueStr.substring(valueStr.length - 1);
30
+ var checkCode = sumCheckCode(preCode);
31
+ return lastCode === checkCode;
32
+ }
33
+ isUnifiedIdentifier.sumCheckCode = sumCheckCode;
34
+
35
+ module.exports = isUnifiedIdentifier;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "5.5.1",
3
+ "version": "5.7.0",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -71,10 +71,10 @@
71
71
  "jest-environment-jsdom": "^29.7.0",
72
72
  "jsdoc": "^4.0.4",
73
73
  "lint-staged": "^13.3.0",
74
- "prettier": "^3.4.2",
75
- "rollup": "^4.28.1",
76
- "ts-jest": "^29.2.5",
77
- "typescript": "^5.7.2"
74
+ "prettier": "^3.5.3",
75
+ "rollup": "^4.36.0",
76
+ "ts-jest": "^29.2.6",
77
+ "typescript": "^5.8.2"
78
78
  },
79
79
  "lint-staged": {
80
80
  "**/*.ts": "eslint",
@@ -88,7 +88,7 @@
88
88
  "dependencies": {
89
89
  "cache2": "^3.1.2",
90
90
  "tslib": "^2.8.1",
91
- "ut2": "^1.15.0"
91
+ "ut2": "^1.17.0"
92
92
  },
93
93
  "publishConfig": {
94
94
  "registry": "https://registry.npmjs.org/"
@@ -9,18 +9,24 @@ import { Cache, CacheOptions } from 'cache2';
9
9
  *
10
10
  * @class
11
11
  * @see {@link https://www.npmjs.com/package/cache2 cache2}
12
- * @param {Object} [options] 缓存配置项,更多配置项可参考 [`cache2`](https://www.npmjs.com/package/cache2)
13
- * @param {number} [options.max] 最大缓存数量
14
- * @param {'replaced' | 'limited'} [options.maxStrategy] 缓存策略
12
+ * @param {Object} [options] 缓存配置项,更多配置项可参考 [`cache2`](https://www.npmjs.com/package/cache2)
13
+ * @param {number} [options.max] 最大缓存数量。
14
+ * @param {'replaced' | 'limited'} [options.maxStrategy] 缓存策略。
15
+ * @param {string} [options.prefix] 缓存健前缀。
16
+ * @param {string} [ns='uh_async_memo'] 缓存命名空间。默认 `uh_async_memo`。
15
17
  * @example
16
18
  *
17
- * const asyncMemo = new AsyncMemo({ max: 20, maxStrategy: 'replaced' });
19
+ * const asyncMemo = new AsyncMemo({ max: 20, maxStrategy: 'replaced', prefix: 'some key' });
18
20
  * asyncMemo.run(()=>download({ fssid: 'a' }), 'a');
19
21
  * asyncMemo.run(()=>download({ fssid: 'b' }), 'b');
20
22
  * asyncMemo.run(()=>download({ fssid: 'a' }), 'a'); // 如果有缓存结果直接返回,如果有异步执行中,不会重复触发异步,但共享异步结果。
21
23
  *
22
24
  * asyncMemo.run(()=>download({ fssid: 'a' }), 'a', { persisted: false }); // 不读取缓存结果,但是异步执行结果还是会缓存。
23
25
  * asyncMemo.run(()=>download({ fssid: 'a' })); // 没有缓存键时,直接执行异步方法,不读取缓存结果,也不会缓存异步结果。
26
+ *
27
+ * // 自定义命名空间
28
+ * const asyncMemo2 = new AsyncMemo({}, 'namespace');
29
+ *
24
30
  */
25
31
  declare class AsyncMemo<DataType = any> {
26
32
  private promiseCache;
@@ -28,7 +34,7 @@ declare class AsyncMemo<DataType = any> {
28
34
  * @summary cache2 实例,用于管理缓存
29
35
  */
30
36
  cache: Cache<DataType>;
31
- constructor(options?: Partial<CacheOptions>);
37
+ constructor(options?: Partial<CacheOptions>, ns?: string);
32
38
  /**
33
39
  * 执行异步方法
34
40
  *
package/types/ajax.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  type XMLHttpRequestListener = XMLHttpRequest['onloadstart'];
2
+ type DataType = Parameters<XMLHttpRequest['send']>[0];
2
3
  type AjaxOptions = {
3
4
  method?: string;
4
5
  async?: boolean;
5
6
  user?: string | null;
6
7
  password?: string | null;
7
- data?: Parameters<XMLHttpRequest['send']>[0];
8
+ data?: DataType;
8
9
  headers?: Record<string, string>;
9
10
  responseType?: XMLHttpRequestResponseType;
10
11
  timeout?: number;
@@ -75,6 +76,25 @@ type AjaxOptions = {
75
76
  * ajax('/api', { method: 'post' }).then(res=>{
76
77
  * // do something
77
78
  * });
79
+ *
80
+ * // 中断请求
81
+ * let xhr: XMLHttpRequest | null = null;
82
+ * ajax('./download/test.txt', {
83
+ * onLoadStart(e) {
84
+ * console.log('onLoadStart', e);
85
+ * xhr = e.target;
86
+ * }
87
+ * }).finally(() => {
88
+ * console.log('finally');
89
+ * xhr = null;
90
+ * });
91
+ *
92
+ * if(ABORT_CONDITION){
93
+ * if(xhr){
94
+ * xhr.abort();
95
+ * }
96
+ * }
97
+ *
78
98
  */
79
99
  declare function ajax(url: string, options?: AjaxOptions): Promise<ProgressEvent<EventTarget>>;
80
100
  export default ajax;
package/types/index.d.ts CHANGED
@@ -25,7 +25,7 @@ export { default as isHMCard } from './isHMCard';
25
25
  export { default as isTWCard } from './isTWCard';
26
26
  export { default as isSwiftCode } from './isSwiftCode';
27
27
  export { default as isValidNumber } from './isValidNumber';
28
- export { default as checkResult } from './checkResult';
28
+ export { default as isUnifiedIdentifier } from './isUnifiedIdentifier';
29
29
  /**
30
30
  * 数据处理
31
31
  *
@@ -117,6 +117,7 @@ export { default as calculateCursorPosition } from './calculateCursorPosition';
117
117
  export { default as getExtname } from './getExtname';
118
118
  export { default as randomString } from './randomString';
119
119
  export { default as strlen } from './strlen';
120
+ export { default as checkResult } from './checkResult';
120
121
  /**
121
122
  * 树结构数据查询、过滤、转换等处理方法
122
123
  *
@@ -4,9 +4,11 @@ type Options = {
4
4
  /**
5
5
  * 检测值是否为营业执照号,也叫工商注册号。由14位数字本体码和1位数字校验码组成,其中本体码从左至右依次为:6位首次登记机关码、8位顺序码。
6
6
  *
7
+ * @private
7
8
  * @alias module:Validator.isBusinessLicense
8
9
  * @since 3.5.0
9
10
  * @see {@link https://wenku.baidu.com/view/19873704cc1755270722087c.html GS15—2006 工商行政管理市场主体注册号编制规则}
11
+ * @deprecated 即将废弃。
10
12
  * @param {*} value 要检测的值
11
13
  * @param {Object} [options] 配置项
12
14
  * @param {boolean} [options.checkCode=true] 是否校验最后一位校验码,默认`true`。如果为false,不校验校验位。
@@ -1,12 +1,12 @@
1
- type Options = {
2
- checkCode?: boolean;
3
- };
1
+ import isUnifiedIdentifier from './isUnifiedIdentifier';
4
2
  /**
5
3
  * 检测值是否为统一社会信用代码,也叫三证合一组织代码。由18位数字和大写字母组成,不使用I、O、Z、S、V。
6
4
  *
5
+ * @private
7
6
  * @alias module:Validator.isSocialCreditCode
8
7
  * @since 1.1.0
9
8
  * @see {@link https://zh.wikisource.org/zh-hans/GB_32100-2015_法人和其他组织统一社会信用代码编码规则 GB 32100-2015 法人和其他组织统一社会信用代码编码规则}
9
+ * @deprecated 即将废弃,请使用 `isUnifiedIdentifier` 替代。
10
10
  * @param {*} value 要检测的值
11
11
  * @param {Object} [options] 配置项
12
12
  * @param {boolean} [options.checkCode=true] 是否校验最后一位校验码,如果为false,不校验校验位。
@@ -22,8 +22,5 @@ type Options = {
22
22
  * isSocialCreditCode('91350100M000100Y', { checkCode: false }); // false
23
23
  *
24
24
  */
25
- declare function isSocialCreditCode(value: any, options?: Options): boolean;
26
- declare namespace isSocialCreditCode {
27
- var sumCheckCode: (preCode: string) => string;
28
- }
25
+ declare const isSocialCreditCode: typeof isUnifiedIdentifier;
29
26
  export default isSocialCreditCode;
@@ -0,0 +1,32 @@
1
+ type Options = {
2
+ checkCode?: boolean;
3
+ };
4
+ /**
5
+ * 检测值是否为统一社会信用代码,也叫三证合一组织代码。由18位数字和大写字母组成,不使用I、O、Z、S、V。
6
+ *
7
+ * 之前版本的方法名为 `isSocialCreditCode` 。
8
+ *
9
+ * @alias module:Validator.isUnifiedIdentifier
10
+ * @since 5.6.0
11
+ * @see {@link https://zh.wikisource.org/zh-hans/GB_32100-2015_法人和其他组织统一社会信用代码编码规则 GB 32100-2015 法人和其他组织统一社会信用代码编码规则}
12
+ * @see {@link https://std.samr.gov.cn/gb/search/gbDetailed?id=71F772D808D6D3A7E05397BE0A0AB82A 法人和其他组织统一社会信用代码编码规则}
13
+ * @param {*} value 要检测的值
14
+ * @param {Object} [options] 配置项
15
+ * @param {boolean} [options.checkCode=true] 是否校验最后一位校验码,如果为false,不校验校验位。
16
+ * @returns {boolean} 值是否为统一社会信用代码
17
+ * @example
18
+ *
19
+ * isUnifiedIdentifier('91350100M000100Y43'); // true
20
+ * isUnifiedIdentifier('91350100M000100Y4A'); // false
21
+ *
22
+ * // 不校验校验位,长度和类型还是有校验的
23
+ * isUnifiedIdentifier('91350100M000100Y4A', { checkCode: false }); // true
24
+ * isUnifiedIdentifier('91350100M000100YIO', { checkCode: false }); // false
25
+ * isUnifiedIdentifier('91350100M000100Y', { checkCode: false }); // false
26
+ *
27
+ */
28
+ declare function isUnifiedIdentifier(value: any, options?: Options): boolean;
29
+ declare namespace isUnifiedIdentifier {
30
+ var sumCheckCode: (preCode: string) => string;
31
+ }
32
+ export default isUnifiedIdentifier;