util-helpers 4.17.1 → 4.17.3
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/README.md +1 -0
- package/dist/util-helpers.js +27 -4
- package/dist/util-helpers.js.map +1 -1
- package/dist/util-helpers.min.js +1 -1
- package/dist/util-helpers.min.js.map +1 -1
- package/esm/index.js +1 -0
- package/esm/isValidNumber.js +23 -0
- package/esm/utils/config.js +1 -1
- package/lib/index.js +2 -0
- package/lib/isValidNumber.js +25 -0
- package/lib/utils/config.js +1 -1
- package/package.json +23 -24
- package/types/ajax.d.ts +1 -1
- package/types/blobToDataURL.d.ts +1 -1
- package/types/calculateCursorPosition.d.ts +1 -1
- package/types/download.d.ts +7 -3
- package/types/fileReader.d.ts +0 -26
- package/types/filterTree.d.ts +3 -1
- package/types/index.d.ts +1 -0
- package/types/isIdCard.d.ts +3 -1
- package/types/isValidNumber.d.ts +33 -0
- package/types/numberToChinese.d.ts +1 -1
- package/types/safeDate.d.ts +0 -22
- package/types/setDataURLPrefix.d.ts +2 -1
- package/types/treeToList.d.ts +3 -6
- package/types/utils/type/isBlob.d.ts +1 -1
- package/types/utils/type/isError.d.ts +2 -2
- package/types/utils/type/isNil.d.ts +1 -1
package/esm/index.js
CHANGED
|
@@ -20,6 +20,7 @@ export { default as isPromiseLike } from './isPromiseLike.js';
|
|
|
20
20
|
export { default as isHMCard } from './isHMCard.js';
|
|
21
21
|
export { default as isTWCard } from './isTWCard.js';
|
|
22
22
|
export { default as isSwiftCode } from './isSwiftCode.js';
|
|
23
|
+
export { default as isValidNumber } from './isValidNumber.js';
|
|
23
24
|
export { default as formatMoney } from './formatMoney.js';
|
|
24
25
|
export { default as formatBankCard } from './formatBankCard.js';
|
|
25
26
|
export { default as replaceChar } from './replaceChar.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import isSymbol from './utils/type/isSymbol.js';
|
|
2
|
+
|
|
3
|
+
function isValidNumber(value, strict) {
|
|
4
|
+
if (strict === void 0) { strict = false; }
|
|
5
|
+
var ret;
|
|
6
|
+
if (strict) {
|
|
7
|
+
ret = typeof value === 'string' && value !== '' ? Number(value) : value;
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
if (typeof value === 'number') {
|
|
11
|
+
ret = value;
|
|
12
|
+
}
|
|
13
|
+
else if (isSymbol(value)) {
|
|
14
|
+
ret = Number.NaN;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
ret = Number(value);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return typeof ret === 'number' && !Number.isNaN(ret);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export { isValidNumber as default };
|
package/esm/utils/config.js
CHANGED
package/lib/index.js
CHANGED
|
@@ -22,6 +22,7 @@ var isPromiseLike = require('./isPromiseLike.js');
|
|
|
22
22
|
var isHMCard = require('./isHMCard.js');
|
|
23
23
|
var isTWCard = require('./isTWCard.js');
|
|
24
24
|
var isSwiftCode = require('./isSwiftCode.js');
|
|
25
|
+
var isValidNumber = require('./isValidNumber.js');
|
|
25
26
|
var formatMoney = require('./formatMoney.js');
|
|
26
27
|
var formatBankCard = require('./formatBankCard.js');
|
|
27
28
|
var replaceChar = require('./replaceChar.js');
|
|
@@ -80,6 +81,7 @@ exports.isPromiseLike = isPromiseLike;
|
|
|
80
81
|
exports.isHMCard = isHMCard;
|
|
81
82
|
exports.isTWCard = isTWCard;
|
|
82
83
|
exports.isSwiftCode = isSwiftCode;
|
|
84
|
+
exports.isValidNumber = isValidNumber;
|
|
83
85
|
exports.formatMoney = formatMoney;
|
|
84
86
|
exports.formatBankCard = formatBankCard;
|
|
85
87
|
exports.replaceChar = replaceChar;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var isSymbol = require('./utils/type/isSymbol.js');
|
|
4
|
+
|
|
5
|
+
function isValidNumber(value, strict) {
|
|
6
|
+
if (strict === void 0) { strict = false; }
|
|
7
|
+
var ret;
|
|
8
|
+
if (strict) {
|
|
9
|
+
ret = typeof value === 'string' && value !== '' ? Number(value) : value;
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
if (typeof value === 'number') {
|
|
13
|
+
ret = value;
|
|
14
|
+
}
|
|
15
|
+
else if (isSymbol(value)) {
|
|
16
|
+
ret = Number.NaN;
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
ret = Number(value);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return typeof ret === 'number' && !Number.isNaN(ret);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = isValidNumber;
|
package/lib/utils/config.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "util-helpers",
|
|
3
|
-
"version": "4.17.
|
|
3
|
+
"version": "4.17.3",
|
|
4
4
|
"description": "一个基于业务场景的工具方法库",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "jest --verbose",
|
|
12
12
|
"test:coverage": "jest --coverage",
|
|
13
|
-
"test:coverage:local": "cross-env COVERAGE_LOCAL=1 jest --coverage",
|
|
13
|
+
"test:coverage:local": "cross-env COVERAGE_LOCAL=1 jest --coverage && open ./coverage/lcov-report/index.html",
|
|
14
14
|
"test:math": "jest --verbose test/math",
|
|
15
15
|
"test:type": "jest --verbose test/type",
|
|
16
16
|
"test:validator": "jest --verbose test/validator",
|
|
@@ -18,16 +18,16 @@
|
|
|
18
18
|
"build": "npm run build:module && npm run build:dist && npm run build:types",
|
|
19
19
|
"build:module": "rm -rf esm && rm -rf lib && rollup -c rollup.module.config.mjs",
|
|
20
20
|
"build:dist": "rm -rf dist && rollup -c rollup.umd.config.mjs",
|
|
21
|
-
"build:types": "rm -rf types && tsc -d --outDir types --emitDeclarationOnly",
|
|
21
|
+
"build:types": "rm -rf types && tsc -p tsconfig.build.json -d --outDir types --emitDeclarationOnly",
|
|
22
22
|
"build:doc": "npm run doc",
|
|
23
|
-
"doc": "rm -rf docs && rm -rf docs-src && tsc --outDir docs-src && jsdoc -c conf.json && rm -rf docs-src",
|
|
23
|
+
"doc": "rm -rf docs && rm -rf docs-src && tsc -p tsconfig.build.json --outDir docs-src && jsdoc -c conf.json && rm -rf docs-src",
|
|
24
24
|
"doc:open": "open ./docs/index.html",
|
|
25
25
|
"prettier": "prettier --write 'src/**/*.ts' && prettier --write 'test/**/*.ts'",
|
|
26
|
-
"precommit": "lint-staged",
|
|
27
26
|
"lint": "eslint src --ext .ts",
|
|
28
27
|
"lint:fix": "eslint src --ext .ts --fix",
|
|
29
28
|
"commit": "cz",
|
|
30
|
-
"prepublishOnly": "npm test && npm run build"
|
|
29
|
+
"prepublishOnly": "npm test && npm run build",
|
|
30
|
+
"tsc": "tsc --noEmit"
|
|
31
31
|
},
|
|
32
32
|
"repository": {
|
|
33
33
|
"type": "git",
|
|
@@ -54,42 +54,41 @@
|
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://doly-dev.github.io/util-helpers/index.html",
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@babel/core": "^7.21.
|
|
58
|
-
"@babel/preset-env": "^7.21.
|
|
59
|
-
"@babel/preset-typescript": "^7.21.
|
|
60
|
-
"@commitlint/cli": "^
|
|
61
|
-
"@commitlint/config-conventional": "^
|
|
57
|
+
"@babel/core": "^7.21.8",
|
|
58
|
+
"@babel/preset-env": "^7.21.5",
|
|
59
|
+
"@babel/preset-typescript": "^7.21.5",
|
|
60
|
+
"@commitlint/cli": "^17.6.3",
|
|
61
|
+
"@commitlint/config-conventional": "^17.6.3",
|
|
62
62
|
"@rollup/plugin-commonjs": "^24.1.0",
|
|
63
63
|
"@rollup/plugin-node-resolve": "^15.0.2",
|
|
64
64
|
"@rollup/plugin-replace": "^5.0.2",
|
|
65
65
|
"@rollup/plugin-terser": "^0.4.1",
|
|
66
66
|
"@rollup/plugin-typescript": "^11.1.0",
|
|
67
67
|
"@types/jest": "^29.5.1",
|
|
68
|
-
"@typescript-eslint/eslint-plugin": "^5.59.
|
|
69
|
-
"@typescript-eslint/parser": "^5.59.
|
|
68
|
+
"@typescript-eslint/eslint-plugin": "^5.59.5",
|
|
69
|
+
"@typescript-eslint/parser": "^5.59.5",
|
|
70
70
|
"babel-jest": "^29.5.0",
|
|
71
71
|
"babel-plugin-minify-replace": "^0.5.0",
|
|
72
72
|
"cross-env": "^7.0.3",
|
|
73
73
|
"cz-conventional-changelog": "^3.3.0",
|
|
74
|
-
"docdash": "^
|
|
75
|
-
"eslint": "^8.
|
|
76
|
-
"husky": "^4.3.6",
|
|
74
|
+
"docdash": "^2.0.1",
|
|
75
|
+
"eslint": "^8.40.0",
|
|
77
76
|
"jest": "^29.5.0",
|
|
78
77
|
"jest-environment-jsdom": "^29.5.0",
|
|
79
|
-
"jsdoc": "^
|
|
80
|
-
"lint-staged": "^
|
|
78
|
+
"jsdoc": "^4.0.2",
|
|
79
|
+
"lint-staged": "^13.2.2",
|
|
81
80
|
"prettier": "^2.8.8",
|
|
82
|
-
"rollup": "^3.21.
|
|
83
|
-
"typescript": "^5.0.4"
|
|
81
|
+
"rollup": "^3.21.6",
|
|
82
|
+
"typescript": "^5.0.4",
|
|
83
|
+
"yorkie": "^2.0.0"
|
|
84
84
|
},
|
|
85
85
|
"lint-staged": {
|
|
86
86
|
"**/*.ts": "eslint",
|
|
87
87
|
"**/*.{js,ts,md}": "prettier --write"
|
|
88
88
|
},
|
|
89
|
-
"
|
|
90
|
-
"
|
|
91
|
-
|
|
92
|
-
}
|
|
89
|
+
"gitHooks": {
|
|
90
|
+
"pre-commit": "lint-staged",
|
|
91
|
+
"commit-msg": "npx --no -- commitlint --edit \"$1\""
|
|
93
92
|
},
|
|
94
93
|
"config": {
|
|
95
94
|
"commitizen": {
|
package/types/ajax.d.ts
CHANGED
package/types/blobToDataURL.d.ts
CHANGED
package/types/download.d.ts
CHANGED
|
@@ -29,7 +29,7 @@ type DownloadOptions = {
|
|
|
29
29
|
* @property {TransformResponse} [options.transformResponse] 请求成功后触发,在传递给 then/catch 前,允许修改响应数据
|
|
30
30
|
*/
|
|
31
31
|
/**
|
|
32
|
-
*
|
|
32
|
+
* 下载
|
|
33
33
|
*
|
|
34
34
|
* <em style="font-weight: bold;">注意:该方法仅适用于浏览器端,兼容 IE10+ 和现代浏览器。</em>
|
|
35
35
|
*
|
|
@@ -45,8 +45,12 @@ type DownloadOptions = {
|
|
|
45
45
|
* // 文本
|
|
46
46
|
* download('hello world', 'text.txt');
|
|
47
47
|
*
|
|
48
|
-
* // 远程文件
|
|
49
|
-
*
|
|
48
|
+
* // 远程文件1
|
|
49
|
+
* // 不带协议的绝对地址,需要通过 dataType 指定为 url 类型
|
|
50
|
+
* download('/xxx.jpg', { dataType: 'url', fileName: 'test.jpg' });
|
|
51
|
+
*
|
|
52
|
+
* // 远程文件2
|
|
53
|
+
* download('https://example.com/xxx.jpg');
|
|
50
54
|
*
|
|
51
55
|
* // base64
|
|
52
56
|
* download('data:image/png;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=', 'test.png');
|
package/types/fileReader.d.ts
CHANGED
|
@@ -5,32 +5,6 @@ declare const FileReaderMethodMap: {
|
|
|
5
5
|
readonly text: "readAsText";
|
|
6
6
|
};
|
|
7
7
|
type ReadType = keyof typeof FileReaderMethodMap;
|
|
8
|
-
/**
|
|
9
|
-
* 读取 Blob 或 File 对象,转为 Base64/String/ArrayBuffer<br/><br/>
|
|
10
|
-
*
|
|
11
|
-
* <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
|
|
12
|
-
*
|
|
13
|
-
* @static
|
|
14
|
-
* @alias module:Processor.fileReader
|
|
15
|
-
* @see {@link https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader | FileReader}
|
|
16
|
-
* @since 4.16.0
|
|
17
|
-
* @param {Blob} blob Blob 或 File 对象
|
|
18
|
-
* @param {'arrayBuffer'|'binaryString'|'dataURL'|'text'} [type='dataURL'] 读取类型
|
|
19
|
-
* @returns {Promise<string|ArrayBuffer>} 文件内容
|
|
20
|
-
* @example
|
|
21
|
-
* const aFileParts = ['<a id="a"><b id="b">hey!</b></a>']; // 一个包含DOMString的数组
|
|
22
|
-
* const htmlBlob = new Blob(aFileParts, { type: 'text/html' }); // 得到 blob
|
|
23
|
-
*
|
|
24
|
-
* fileReader(htmlBlob).then(data=>{
|
|
25
|
-
* console.log(data); // data:text/html;base64,PGEgaWQ9ImEiPjxiIGlkPSJiIj5oZXkhPC9iPjwvYT4=
|
|
26
|
-
* });
|
|
27
|
-
*
|
|
28
|
-
* const textBlob = new Blob(aFileParts, { type: 'text/plain' });
|
|
29
|
-
*
|
|
30
|
-
* fileReader(textBlob, 'text').then(data=>{
|
|
31
|
-
* console.log(data); // <a id="a"><b id="b">hey!</b></a>
|
|
32
|
-
* });
|
|
33
|
-
*/
|
|
34
8
|
declare function fileReader(blob: Blob, type: 'arrayBuffer'): Promise<ArrayBuffer>;
|
|
35
9
|
declare function fileReader(blob: Blob, type?: Exclude<ReadType, 'arrayBuffer'>): Promise<string>;
|
|
36
10
|
export default fileReader;
|
package/types/filterTree.d.ts
CHANGED
package/types/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export { default as isPromiseLike } from './isPromiseLike';
|
|
|
26
26
|
export { default as isHMCard } from './isHMCard';
|
|
27
27
|
export { default as isTWCard } from './isTWCard';
|
|
28
28
|
export { default as isSwiftCode } from './isSwiftCode';
|
|
29
|
+
export { default as isValidNumber } from './isValidNumber';
|
|
29
30
|
/**
|
|
30
31
|
* 数据处理
|
|
31
32
|
*
|
package/types/isIdCard.d.ts
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检测值是否为有效数值,支持隐式转换。如果返回 true ,表示可以通过 Number() 转为数字。
|
|
3
|
+
*
|
|
4
|
+
* @static
|
|
5
|
+
* @alias module:Validator.isValidNumber
|
|
6
|
+
* @since 4.17.2
|
|
7
|
+
* @param {*} value 待检测的值
|
|
8
|
+
* @param {boolean} [strict=false] 严格模式,如果为 true ,仅支持字符串和数字类型,不处理其他类型隐式转换,且空字符串返回 false 。
|
|
9
|
+
* @returns {boolean} 值是否为有效数值
|
|
10
|
+
* @example
|
|
11
|
+
*
|
|
12
|
+
* isValidNumber(null); // true
|
|
13
|
+
* isValidNumber(true); // true
|
|
14
|
+
* isValidNumber(''); // true
|
|
15
|
+
* isValidNumber(1234); // true
|
|
16
|
+
* isValidNumber('1234'); // true
|
|
17
|
+
* isValidNumber(' 1234 '); // true
|
|
18
|
+
* isValidNumber(' 1234 '); // true
|
|
19
|
+
*
|
|
20
|
+
* isValidNumber(undefined); // false
|
|
21
|
+
* isValidNumber('0.10.1'); // false
|
|
22
|
+
*
|
|
23
|
+
* // 严格模式
|
|
24
|
+
* isValidNumber(null, true); // false
|
|
25
|
+
* isValidNumber(true, true); // false
|
|
26
|
+
* isValidNumber('', true); // false
|
|
27
|
+
* isValidNumber(1234, true); // true
|
|
28
|
+
* isValidNumber('1234', true); // true
|
|
29
|
+
* isValidNumber(' 1234 ', true); // true
|
|
30
|
+
* isValidNumber(' 1234 ', true); // true
|
|
31
|
+
*/
|
|
32
|
+
declare function isValidNumber(value: any, strict?: boolean): boolean;
|
|
33
|
+
export default isValidNumber;
|
package/types/safeDate.d.ts
CHANGED
|
@@ -1,25 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 创建一个 Date 实例日期对象,同 <a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date#%E5%8F%82%E6%95%B0">new Date()</a> <br/><br/>
|
|
3
|
-
*
|
|
4
|
-
* 规避了苹果设备浏览器不支持部分格式(例如,YYYY-MM-DD HH-mm 或 YYYY.MM.DD)。<br/>
|
|
5
|
-
* 如果参数为 undefined 正常返回 Date 。
|
|
6
|
-
*
|
|
7
|
-
* @static
|
|
8
|
-
* @alias module:Processor.safeDate
|
|
9
|
-
* @since 4.4.0
|
|
10
|
-
* @see {@link https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date | Date}
|
|
11
|
-
* @param {string|number|Date} [value] 日期时间字符串、毫秒数、日期对象
|
|
12
|
-
* @param {...number} [args] 月/日/时/分/秒/毫秒
|
|
13
|
-
* @returns {Date} Date 实例日期对象
|
|
14
|
-
* @example
|
|
15
|
-
*
|
|
16
|
-
* safeDate('2022-1-1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
17
|
-
* safeDate('2022/1/1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
18
|
-
* safeDate('2022.1.1'); // Sat Jan 01 2022 00:00:00 GMT+0800 (中国标准时间)
|
|
19
|
-
* safeDate('2022.1.1 11:11'); // Sat Jan 01 2022 11:11:00 GMT+0800 (中国标准时间)
|
|
20
|
-
* safeDate(99, 1); // Mon Feb 01 1999 00:00:00 GMT+0800 (中国标准时间)
|
|
21
|
-
* safeDate(1646711233171); // Tue Mar 08 2022 11:47:13 GMT+0800 (中国标准时间)
|
|
22
|
-
*/
|
|
23
1
|
declare function safeDate(): Date;
|
|
24
2
|
declare function safeDate(value: number | string | Date): Date;
|
|
25
3
|
declare function safeDate(year: number, monthIndex: number, date?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): Date;
|
package/types/treeToList.d.ts
CHANGED
|
@@ -4,12 +4,9 @@
|
|
|
4
4
|
* @static
|
|
5
5
|
* @alias module:Tree.treeToList
|
|
6
6
|
* @since 4.14.0
|
|
7
|
-
* @
|
|
8
|
-
* @
|
|
9
|
-
* @
|
|
10
|
-
* @param {T[]} tree 列表数据
|
|
11
|
-
* @param {K} childrenField 子级字段名称
|
|
12
|
-
* @returns {R[]} 列表数据
|
|
7
|
+
* @param {object[]} tree 树结构数据
|
|
8
|
+
* @param {string} childrenField 子级字段名称
|
|
9
|
+
* @returns {object[]} 列表数据
|
|
13
10
|
* @example
|
|
14
11
|
* const menus = [{ "id": "1", "name": "首页", "code": "trade", "pid": null }, { "id": "2", "name": "交易管理", "code": "trade", "pid": null, "children": [{ "id": "3", "name": "交易查询", "code": "trade-1", "pid": "2", "children": [{ "id": "4", "name": "交易查询-查询操作", "code": "trade-1-1", "pid": "3" }] }] }, { "id": "5", "name": "权限管理", "code": "authorization", "pid": null, "children": [{ "id": "6", "name": "角色管理", "code": "authorization-1", "pid": "5" }, { "id": "7", "name": "用户管理", "code": "authorization-2", "pid": "5" }] }];
|
|
15
12
|
*
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* 检查值是否为Error、EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError|DOMException
|
|
2
|
+
* 检查值是否为Error、EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError | DOMException
|
|
3
3
|
*
|
|
4
4
|
* @static
|
|
5
5
|
* @alias module:Type.isError
|
|
6
6
|
* @since 1.1.0
|
|
7
7
|
* @param {*} value 检查值
|
|
8
|
-
* @returns {boolean} 是否为Error、EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError|DOMException
|
|
8
|
+
* @returns {boolean} 是否为Error、EvalError、RangeError、ReferenceError、SyntaxError、TypeError、URIError | DOMException
|
|
9
9
|
* @example
|
|
10
10
|
*
|
|
11
11
|
* isError(new Error)
|