util-helpers 5.1.0 → 5.1.2
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/dist/util-helpers.js +25 -9
- 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/VERSION.js +1 -1
- package/esm/checkFileType.js +6 -7
- package/esm/formatMoney.js +4 -5
- package/esm/getFileType.js +2 -1
- package/esm/utils/file.util.js +13 -0
- package/lib/VERSION.js +1 -1
- package/lib/checkFileType.js +5 -6
- package/lib/formatMoney.js +3 -4
- package/lib/getFileType.js +2 -1
- package/lib/utils/file.util.js +16 -0
- package/package.json +2 -2
- package/types/checkFileType.d.ts +23 -2
- package/types/formatMoney.d.ts +2 -0
- package/types/getFileType.d.ts +12 -2
- package/types/utils/file.util.d.ts +8 -0
package/esm/VERSION.js
CHANGED
package/esm/checkFileType.js
CHANGED
|
@@ -1,10 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isFile, isString, toString } from 'ut2';
|
|
2
|
+
import { isUploadFile, testExt } from './utils/file.util.js';
|
|
2
3
|
|
|
3
|
-
function testExt(name, ext) {
|
|
4
|
-
return !!name && name.slice(-ext.length) === ext;
|
|
5
|
-
}
|
|
6
4
|
function checkFileType(file, accept) {
|
|
7
|
-
if (!
|
|
5
|
+
if (!isFile(file) && !isUploadFile(file)) {
|
|
8
6
|
return false;
|
|
9
7
|
}
|
|
10
8
|
if (!isString(accept)) {
|
|
@@ -17,9 +15,10 @@ function checkFileType(file, accept) {
|
|
|
17
15
|
var ret = false;
|
|
18
16
|
var types = accept.toLowerCase().split(/,(?:\s+)?/);
|
|
19
17
|
var fileName = file.name.toLowerCase();
|
|
20
|
-
var fileType = file.type;
|
|
18
|
+
var fileType = file.type || '';
|
|
19
|
+
var fileUrl = file.url || '';
|
|
21
20
|
types.some(function (type) {
|
|
22
|
-
if (fileType === type || (type.indexOf('.') === 0 && testExt(fileName, type))) {
|
|
21
|
+
if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (testExt(fileName, type) || testExt(fileUrl, type)))) {
|
|
23
22
|
ret = true;
|
|
24
23
|
}
|
|
25
24
|
else if (type.includes('/*') && fileType.includes('/')) {
|
package/esm/formatMoney.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __read } from 'tslib';
|
|
2
|
-
import { isNaN } from 'ut2';
|
|
2
|
+
import { isString, isNumber, isNaN } from 'ut2';
|
|
3
3
|
import { transformEffectiveNumber, checkBoundary, trimLeftZero } from './utils/math.util.js';
|
|
4
4
|
import devWarn from './utils/devWarn.js';
|
|
5
5
|
import isValidNumber from './isValidNumber.js';
|
|
@@ -41,10 +41,9 @@ function formatDec(decStr, precision, decimal) {
|
|
|
41
41
|
return decimal + ret;
|
|
42
42
|
}
|
|
43
43
|
var formatMoney = function (num, options) {
|
|
44
|
-
if (num === void 0) { num = ''; }
|
|
45
44
|
if (options === void 0) { options = {}; }
|
|
46
|
-
var _a = options.precision, precision = _a === void 0 ? 2 : _a, symbol = options.symbol, _b = options.thousand, thousand = _b === void 0 ? ',' : _b, _c = options.decimal, decimal = _c === void 0 ? '.' : _c;
|
|
47
|
-
if (!checkNumber(num)) {
|
|
45
|
+
var _a = options.precision, precision = _a === void 0 ? 2 : _a, symbol = options.symbol, _b = options.thousand, thousand = _b === void 0 ? ',' : _b, _c = options.decimal, decimal = _c === void 0 ? '.' : _c, _d = options.strict, strict = _d === void 0 ? true : _d;
|
|
46
|
+
if (!checkNumber(num) || (strict && (!isString(num) || num === '') && !isNumber(num))) {
|
|
48
47
|
return '';
|
|
49
48
|
}
|
|
50
49
|
if (typeof num === 'number' && !isFinite(num)) {
|
|
@@ -60,7 +59,7 @@ var formatMoney = function (num, options) {
|
|
|
60
59
|
thousand = typeof thousand === 'string' ? thousand : ',';
|
|
61
60
|
decimal = typeof decimal === 'string' ? decimal : '.';
|
|
62
61
|
var strNum = transformEffectiveNumber(num) + '';
|
|
63
|
-
var
|
|
62
|
+
var _e = __read(strNum.split('.'), 2), intStr = _e[0], decStr = _e[1];
|
|
64
63
|
return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
|
|
65
64
|
};
|
|
66
65
|
var formatMoney$1 = formatMoney;
|
package/esm/getFileType.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isBlob, forEach } from 'ut2';
|
|
2
2
|
import checkFileType from './checkFileType.js';
|
|
3
|
+
import { isUploadFile } from './utils/file.util.js';
|
|
3
4
|
|
|
4
5
|
var config = {
|
|
5
6
|
image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
|
|
@@ -11,7 +12,7 @@ var config = {
|
|
|
11
12
|
};
|
|
12
13
|
function getFileType(file) {
|
|
13
14
|
var type;
|
|
14
|
-
if (isBlob(file)) {
|
|
15
|
+
if (isBlob(file) || isUploadFile(file)) {
|
|
15
16
|
forEach(config, function (accept, fileType) {
|
|
16
17
|
if (checkFileType(file, accept)) {
|
|
17
18
|
type = fileType;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { isObjectLike, isString } from 'ut2';
|
|
2
|
+
|
|
3
|
+
function testExt(name, ext) {
|
|
4
|
+
return !!name && name.slice(-ext.length) === ext;
|
|
5
|
+
}
|
|
6
|
+
function isUploadFile(fileObj) {
|
|
7
|
+
if (isObjectLike(fileObj) && isString(fileObj.uid) && isString(fileObj.name)) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { isUploadFile, testExt };
|
package/lib/VERSION.js
CHANGED
package/lib/checkFileType.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var ut2 = require('ut2');
|
|
4
|
+
var file_util = require('./utils/file.util.js');
|
|
4
5
|
|
|
5
|
-
function testExt(name, ext) {
|
|
6
|
-
return !!name && name.slice(-ext.length) === ext;
|
|
7
|
-
}
|
|
8
6
|
function checkFileType(file, accept) {
|
|
9
|
-
if (!ut2.
|
|
7
|
+
if (!ut2.isFile(file) && !file_util.isUploadFile(file)) {
|
|
10
8
|
return false;
|
|
11
9
|
}
|
|
12
10
|
if (!ut2.isString(accept)) {
|
|
@@ -19,9 +17,10 @@ function checkFileType(file, accept) {
|
|
|
19
17
|
var ret = false;
|
|
20
18
|
var types = accept.toLowerCase().split(/,(?:\s+)?/);
|
|
21
19
|
var fileName = file.name.toLowerCase();
|
|
22
|
-
var fileType = file.type;
|
|
20
|
+
var fileType = file.type || '';
|
|
21
|
+
var fileUrl = file.url || '';
|
|
23
22
|
types.some(function (type) {
|
|
24
|
-
if (fileType === type || (type.indexOf('.') === 0 && testExt(fileName, type))) {
|
|
23
|
+
if (type === '*' || fileType === type || (type.indexOf('.') === 0 && (file_util.testExt(fileName, type) || file_util.testExt(fileUrl, type)))) {
|
|
25
24
|
ret = true;
|
|
26
25
|
}
|
|
27
26
|
else if (type.includes('/*') && fileType.includes('/')) {
|
package/lib/formatMoney.js
CHANGED
|
@@ -43,10 +43,9 @@ function formatDec(decStr, precision, decimal) {
|
|
|
43
43
|
return decimal + ret;
|
|
44
44
|
}
|
|
45
45
|
var formatMoney = function (num, options) {
|
|
46
|
-
if (num === void 0) { num = ''; }
|
|
47
46
|
if (options === void 0) { options = {}; }
|
|
48
|
-
var _a = options.precision, precision = _a === void 0 ? 2 : _a, symbol = options.symbol, _b = options.thousand, thousand = _b === void 0 ? ',' : _b, _c = options.decimal, decimal = _c === void 0 ? '.' : _c;
|
|
49
|
-
if (!checkNumber(num)) {
|
|
47
|
+
var _a = options.precision, precision = _a === void 0 ? 2 : _a, symbol = options.symbol, _b = options.thousand, thousand = _b === void 0 ? ',' : _b, _c = options.decimal, decimal = _c === void 0 ? '.' : _c, _d = options.strict, strict = _d === void 0 ? true : _d;
|
|
48
|
+
if (!checkNumber(num) || (strict && (!ut2.isString(num) || num === '') && !ut2.isNumber(num))) {
|
|
50
49
|
return '';
|
|
51
50
|
}
|
|
52
51
|
if (typeof num === 'number' && !isFinite(num)) {
|
|
@@ -62,7 +61,7 @@ var formatMoney = function (num, options) {
|
|
|
62
61
|
thousand = typeof thousand === 'string' ? thousand : ',';
|
|
63
62
|
decimal = typeof decimal === 'string' ? decimal : '.';
|
|
64
63
|
var strNum = math_util.transformEffectiveNumber(num) + '';
|
|
65
|
-
var
|
|
64
|
+
var _e = tslib.__read(strNum.split('.'), 2), intStr = _e[0], decStr = _e[1];
|
|
66
65
|
return symbol + formatInt(intStr, thousand) + formatDec(decStr, precision, decimal);
|
|
67
66
|
};
|
|
68
67
|
var formatMoney$1 = formatMoney;
|
package/lib/getFileType.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var ut2 = require('ut2');
|
|
4
4
|
var checkFileType = require('./checkFileType.js');
|
|
5
|
+
var file_util = require('./utils/file.util.js');
|
|
5
6
|
|
|
6
7
|
var config = {
|
|
7
8
|
image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
|
|
@@ -13,7 +14,7 @@ var config = {
|
|
|
13
14
|
};
|
|
14
15
|
function getFileType(file) {
|
|
15
16
|
var type;
|
|
16
|
-
if (ut2.isBlob(file)) {
|
|
17
|
+
if (ut2.isBlob(file) || file_util.isUploadFile(file)) {
|
|
17
18
|
ut2.forEach(config, function (accept, fileType) {
|
|
18
19
|
if (checkFileType(file, accept)) {
|
|
19
20
|
type = fileType;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ut2 = require('ut2');
|
|
4
|
+
|
|
5
|
+
function testExt(name, ext) {
|
|
6
|
+
return !!name && name.slice(-ext.length) === ext;
|
|
7
|
+
}
|
|
8
|
+
function isUploadFile(fileObj) {
|
|
9
|
+
if (ut2.isObjectLike(fileObj) && ut2.isString(fileObj.uid) && ut2.isString(fileObj.name)) {
|
|
10
|
+
return true;
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
exports.isUploadFile = isUploadFile;
|
|
16
|
+
exports.testExt = testExt;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "util-helpers",
|
|
3
|
-
"version": "5.1.
|
|
3
|
+
"version": "5.1.2",
|
|
4
4
|
"description": "一个基于业务场景的工具方法库",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -94,7 +94,7 @@
|
|
|
94
94
|
"dependencies": {
|
|
95
95
|
"cache2": "^3.0.0",
|
|
96
96
|
"tslib": "^2.6.3",
|
|
97
|
-
"ut2": "^1.
|
|
97
|
+
"ut2": "^1.11.0"
|
|
98
98
|
},
|
|
99
99
|
"publishConfig": {
|
|
100
100
|
"registry": "https://registry.npmjs.org/"
|
package/types/checkFileType.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { UploadFile } from './utils/file.util';
|
|
1
2
|
/**
|
|
2
3
|
* 检查文件是否符合 `accept` 类型说明符。
|
|
3
4
|
*
|
|
@@ -6,9 +7,29 @@
|
|
|
6
7
|
* @since 5.1.0
|
|
7
8
|
* @see {@link https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/input/file#唯一文件类型说明符 唯一文件类型说明符}
|
|
8
9
|
* @see {@link https://www.iana.org/assignments/media-types/media-types.xhtml Media Types}
|
|
9
|
-
* @param {File} file
|
|
10
|
+
* @param {File} file 文件对象。支持 antd `UploadFile` 对象。
|
|
10
11
|
* @param {string} [accept] 文件类型说明符。
|
|
11
12
|
* @returns {boolean} 如果 `file` 符合 `accept` 返回 `true`, 否则返回 `false`。
|
|
13
|
+
* @example
|
|
14
|
+
*
|
|
15
|
+
* const pdf = new File([], '1.pdf', { type: 'application/pdf' });
|
|
16
|
+
* const jpeg = new File([], 'xx.jpeg', { type: 'image/jpeg' });
|
|
17
|
+
*
|
|
18
|
+
* // 文件类型
|
|
19
|
+
* checkFileType(pdf, 'application/pdf'); // true
|
|
20
|
+
* checkFileType(jpeg, 'image/jpeg'); // true
|
|
21
|
+
*
|
|
22
|
+
* // 通配符
|
|
23
|
+
* checkFileType(jpeg, 'image/*'); // true
|
|
24
|
+
* checkFileType(pdf, 'image/*'); // false
|
|
25
|
+
* checkFileType(pdf, 'application/*'); // true
|
|
26
|
+
* checkFileType(pdf, '*'); // true
|
|
27
|
+
* checkFileType(jpeg, '*'); // true
|
|
28
|
+
*
|
|
29
|
+
* // 文件名扩展名
|
|
30
|
+
* checkFileType(jpeg, '.png,.jpeg,.jpg'); // true
|
|
31
|
+
* checkFileType(pdf, '.pdf'); // true
|
|
32
|
+
*
|
|
12
33
|
*/
|
|
13
|
-
declare function checkFileType(file: File, accept?: string): boolean;
|
|
34
|
+
declare function checkFileType(file: File | UploadFile, accept?: string): boolean;
|
|
14
35
|
export default checkFileType;
|
package/types/formatMoney.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ type Options = {
|
|
|
3
3
|
symbol?: string;
|
|
4
4
|
thousand?: string;
|
|
5
5
|
decimal?: string;
|
|
6
|
+
strict?: boolean;
|
|
6
7
|
};
|
|
7
8
|
/**
|
|
8
9
|
* 格式化金额
|
|
@@ -16,6 +17,7 @@ type Options = {
|
|
|
16
17
|
* @param {string} [options.symbol] 货币符号
|
|
17
18
|
* @param {string} [options.thousand=","] 千分位符号
|
|
18
19
|
* @param {string} [options.decimal="."] 小数位符号
|
|
20
|
+
* @param {boolean} [options.strict=ture] 严格模式。开启后,只支持非空字符串和数字格式化,其他类型值如`null` `undefined` `true` `false`等将返回空字符串。
|
|
19
21
|
* @returns {string} 格式化的金额
|
|
20
22
|
* @example
|
|
21
23
|
*
|
package/types/getFileType.d.ts
CHANGED
|
@@ -1,11 +1,21 @@
|
|
|
1
|
+
import { UploadFile } from './utils/file.util';
|
|
1
2
|
/**
|
|
2
3
|
* 获取文件类型
|
|
3
4
|
*
|
|
4
5
|
* @static
|
|
5
6
|
* @alias module:Other.getFileType
|
|
6
7
|
* @since 5.1.0
|
|
7
|
-
* @
|
|
8
|
+
* @requires Other.checkFileType
|
|
9
|
+
* @param {File} file 文件对象。支持 antd `UploadFile` 对象。
|
|
8
10
|
* @returns {"image" | "audio" | "video" | "pdf" | "word" | "excel" | undefined} 如果是 `image` `audio` `video` `pdf` `word` `excel` 这些类型的文件,返回对应的类型值,否则返回 `undefined`。
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* const pdf = new File([], '1.pdf', { type: 'application/pdf' });
|
|
14
|
+
* const jpeg = new File([], 'xx.jpeg', { type: 'image/jpeg' });
|
|
15
|
+
*
|
|
16
|
+
* getFileType(pdf); // 'pdf'
|
|
17
|
+
* getFileType(jpeg); // 'image'
|
|
18
|
+
*
|
|
9
19
|
*/
|
|
10
|
-
declare function getFileType(file: File): "image" | "audio" | "video" | "pdf" | "word" | "excel" | undefined;
|
|
20
|
+
declare function getFileType(file: File | UploadFile): "image" | "audio" | "video" | "pdf" | "word" | "excel" | undefined;
|
|
11
21
|
export default getFileType;
|