util-helpers 5.0.3 → 5.1.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/README.md +2 -0
- package/dist/util-helpers.js +69 -13
- 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 +37 -0
- package/esm/getFileType.js +25 -0
- package/esm/index.js +2 -0
- package/esm/parseIdCard.js +2 -2
- package/lib/VERSION.js +1 -1
- package/lib/checkFileType.js +39 -0
- package/lib/getFileType.js +27 -0
- package/lib/index.js +4 -0
- package/lib/parseIdCard.js +2 -2
- package/package.json +10 -11
- package/types/checkFileType.d.ts +14 -0
- package/types/getFileType.d.ts +11 -0
- package/types/index.d.ts +2 -0
package/esm/VERSION.js
CHANGED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isBlob, isString, toString } from 'ut2';
|
|
2
|
+
|
|
3
|
+
function testExt(name, ext) {
|
|
4
|
+
return !!name && name.slice(-ext.length) === ext;
|
|
5
|
+
}
|
|
6
|
+
function checkFileType(file, accept) {
|
|
7
|
+
if (!isBlob(file)) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
if (!isString(accept)) {
|
|
11
|
+
accept = toString(accept);
|
|
12
|
+
}
|
|
13
|
+
accept = accept.trim();
|
|
14
|
+
if (!accept || accept === '*') {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
var ret = false;
|
|
18
|
+
var types = accept.toLowerCase().split(/,(?:\s+)?/);
|
|
19
|
+
var fileName = file.name.toLowerCase();
|
|
20
|
+
var fileType = file.type;
|
|
21
|
+
types.some(function (type) {
|
|
22
|
+
if (fileType === type || (type.indexOf('.') === 0 && testExt(fileName, type))) {
|
|
23
|
+
ret = true;
|
|
24
|
+
}
|
|
25
|
+
else if (type.includes('/*') && fileType.includes('/')) {
|
|
26
|
+
var match = type.match(/(.*)\/\*/);
|
|
27
|
+
var fileParentType = fileType.split('/')[0];
|
|
28
|
+
if (match && match[1] === fileParentType) {
|
|
29
|
+
ret = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return ret;
|
|
33
|
+
});
|
|
34
|
+
return ret;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export { checkFileType as default };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isBlob, forEach } from 'ut2';
|
|
2
|
+
import checkFileType from './checkFileType.js';
|
|
3
|
+
|
|
4
|
+
var config = {
|
|
5
|
+
image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
|
|
6
|
+
audio: 'audio/*,.mp3,.wav',
|
|
7
|
+
video: 'video/*,.mp4,.webm,.ogg,.ogv,.ogm',
|
|
8
|
+
pdf: 'application/pdf,.pdf',
|
|
9
|
+
word: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,.doc,.docx',
|
|
10
|
+
excel: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,.xls,.xlsx'
|
|
11
|
+
};
|
|
12
|
+
function getFileType(file) {
|
|
13
|
+
var type;
|
|
14
|
+
if (isBlob(file)) {
|
|
15
|
+
forEach(config, function (accept, fileType) {
|
|
16
|
+
if (checkFileType(file, accept)) {
|
|
17
|
+
type = fileType;
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
return type;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { getFileType as default };
|
package/esm/index.js
CHANGED
|
@@ -42,9 +42,11 @@ export { default as gcd } from './gcd.js';
|
|
|
42
42
|
export { default as lcm } from './lcm.js';
|
|
43
43
|
export { default as ajax } from './ajax.js';
|
|
44
44
|
export { default as calculateCursorPosition } from './calculateCursorPosition.js';
|
|
45
|
+
export { default as checkFileType } from './checkFileType.js';
|
|
45
46
|
export { default as compressImage } from './compressImage.js';
|
|
46
47
|
export { default as download } from './download.js';
|
|
47
48
|
export { default as getFileBlob } from './getFileBlob.js';
|
|
49
|
+
export { default as getFileType } from './getFileType.js';
|
|
48
50
|
export { default as getImageInfo } from './getImageInfo.js';
|
|
49
51
|
export { default as loadImage } from './loadImage.js';
|
|
50
52
|
export { default as loadImageWithBlob } from './loadImageWithBlob.js';
|
package/esm/parseIdCard.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
var regIdCard = /^(
|
|
1
|
+
var regIdCard = /^(\d{2})(\d{2})(\d{2})((?:\d{2})?\d{2})(\d{2})(\d{2})\d{2}(\d)(?:\d|X)?$/i;
|
|
2
2
|
var Provinces = [
|
|
3
3
|
['11', '北京市'],
|
|
4
4
|
['12', '天津市'],
|
|
@@ -41,7 +41,7 @@ function parseIdCard(id) {
|
|
|
41
41
|
if (!match) {
|
|
42
42
|
return null;
|
|
43
43
|
}
|
|
44
|
-
var origin =
|
|
44
|
+
var origin = {
|
|
45
45
|
province: match[1],
|
|
46
46
|
city: match[2],
|
|
47
47
|
area: match[3],
|
package/lib/VERSION.js
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
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 checkFileType(file, accept) {
|
|
9
|
+
if (!ut2.isBlob(file)) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
if (!ut2.isString(accept)) {
|
|
13
|
+
accept = ut2.toString(accept);
|
|
14
|
+
}
|
|
15
|
+
accept = accept.trim();
|
|
16
|
+
if (!accept || accept === '*') {
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
var ret = false;
|
|
20
|
+
var types = accept.toLowerCase().split(/,(?:\s+)?/);
|
|
21
|
+
var fileName = file.name.toLowerCase();
|
|
22
|
+
var fileType = file.type;
|
|
23
|
+
types.some(function (type) {
|
|
24
|
+
if (fileType === type || (type.indexOf('.') === 0 && testExt(fileName, type))) {
|
|
25
|
+
ret = true;
|
|
26
|
+
}
|
|
27
|
+
else if (type.includes('/*') && fileType.includes('/')) {
|
|
28
|
+
var match = type.match(/(.*)\/\*/);
|
|
29
|
+
var fileParentType = fileType.split('/')[0];
|
|
30
|
+
if (match && match[1] === fileParentType) {
|
|
31
|
+
ret = true;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return ret;
|
|
35
|
+
});
|
|
36
|
+
return ret;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = checkFileType;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var ut2 = require('ut2');
|
|
4
|
+
var checkFileType = require('./checkFileType.js');
|
|
5
|
+
|
|
6
|
+
var config = {
|
|
7
|
+
image: 'image/*,.jpeg,.jpg,.gif,.bmp,.png,.webp',
|
|
8
|
+
audio: 'audio/*,.mp3,.wav',
|
|
9
|
+
video: 'video/*,.mp4,.webm,.ogg,.ogv,.ogm',
|
|
10
|
+
pdf: 'application/pdf,.pdf',
|
|
11
|
+
word: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document,.doc,.docx',
|
|
12
|
+
excel: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,.xls,.xlsx'
|
|
13
|
+
};
|
|
14
|
+
function getFileType(file) {
|
|
15
|
+
var type;
|
|
16
|
+
if (ut2.isBlob(file)) {
|
|
17
|
+
ut2.forEach(config, function (accept, fileType) {
|
|
18
|
+
if (checkFileType(file, accept)) {
|
|
19
|
+
type = fileType;
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
return type;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = getFileType;
|
package/lib/index.js
CHANGED
|
@@ -44,9 +44,11 @@ var gcd = require('./gcd.js');
|
|
|
44
44
|
var lcm = require('./lcm.js');
|
|
45
45
|
var ajax = require('./ajax.js');
|
|
46
46
|
var calculateCursorPosition = require('./calculateCursorPosition.js');
|
|
47
|
+
var checkFileType = require('./checkFileType.js');
|
|
47
48
|
var compressImage = require('./compressImage.js');
|
|
48
49
|
var download = require('./download.js');
|
|
49
50
|
var getFileBlob = require('./getFileBlob.js');
|
|
51
|
+
var getFileType = require('./getFileType.js');
|
|
50
52
|
var getImageInfo = require('./getImageInfo.js');
|
|
51
53
|
var loadImage = require('./loadImage.js');
|
|
52
54
|
var loadImageWithBlob = require('./loadImageWithBlob.js');
|
|
@@ -110,9 +112,11 @@ exports.gcd = gcd;
|
|
|
110
112
|
exports.lcm = lcm;
|
|
111
113
|
exports.ajax = ajax;
|
|
112
114
|
exports.calculateCursorPosition = calculateCursorPosition;
|
|
115
|
+
exports.checkFileType = checkFileType;
|
|
113
116
|
exports.compressImage = compressImage;
|
|
114
117
|
exports.download = download;
|
|
115
118
|
exports.getFileBlob = getFileBlob;
|
|
119
|
+
exports.getFileType = getFileType;
|
|
116
120
|
exports.getImageInfo = getImageInfo;
|
|
117
121
|
exports.loadImage = loadImage;
|
|
118
122
|
exports.loadImageWithBlob = loadImageWithBlob;
|
package/lib/parseIdCard.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var regIdCard = /^(
|
|
3
|
+
var regIdCard = /^(\d{2})(\d{2})(\d{2})((?:\d{2})?\d{2})(\d{2})(\d{2})\d{2}(\d)(?:\d|X)?$/i;
|
|
4
4
|
var Provinces = [
|
|
5
5
|
['11', '北京市'],
|
|
6
6
|
['12', '天津市'],
|
|
@@ -43,7 +43,7 @@ function parseIdCard(id) {
|
|
|
43
43
|
if (!match) {
|
|
44
44
|
return null;
|
|
45
45
|
}
|
|
46
|
-
var origin =
|
|
46
|
+
var origin = {
|
|
47
47
|
province: match[1],
|
|
48
48
|
city: match[2],
|
|
49
49
|
area: match[3],
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "util-helpers",
|
|
3
|
-
"version": "5.0
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "一个基于业务场景的工具方法库",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "esm/index.js",
|
|
@@ -18,9 +18,9 @@
|
|
|
18
18
|
"build:doc": "npm run doc",
|
|
19
19
|
"doc": "rm -rf docs && rm -rf docs-src && tsc -p tsconfig.build.json -t esnext --outDir docs-src && jsdoc -c conf.json && rm -rf docs-src",
|
|
20
20
|
"doc:open": "open ./docs/index.html",
|
|
21
|
-
"prettier": "prettier
|
|
22
|
-
"lint": "eslint
|
|
23
|
-
"lint:fix": "eslint
|
|
21
|
+
"prettier": "prettier -w **/*",
|
|
22
|
+
"lint": "eslint . --ext .js,.jsx,.ts,.tsx",
|
|
23
|
+
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix",
|
|
24
24
|
"commit": "cz",
|
|
25
25
|
"prepublishOnly": "npm test && npm run build",
|
|
26
26
|
"tsc": "tsc --noEmit",
|
|
@@ -63,8 +63,8 @@
|
|
|
63
63
|
"@rollup/plugin-terser": "^0.4.4",
|
|
64
64
|
"@rollup/plugin-typescript": "^11.1.6",
|
|
65
65
|
"@types/jest": "^29.5.12",
|
|
66
|
-
"@typescript-eslint/eslint-plugin": "^7.
|
|
67
|
-
"@typescript-eslint/parser": "^7.
|
|
66
|
+
"@typescript-eslint/eslint-plugin": "^7.16.0",
|
|
67
|
+
"@typescript-eslint/parser": "^7.16.0",
|
|
68
68
|
"babel-jest": "^29.7.0",
|
|
69
69
|
"babel-plugin-minify-replace": "^0.5.0",
|
|
70
70
|
"commitizen": "^4.3.0",
|
|
@@ -79,12 +79,12 @@
|
|
|
79
79
|
"jsdoc": "^4.0.3",
|
|
80
80
|
"lint-staged": "^13.3.0",
|
|
81
81
|
"prettier": "^3.3.2",
|
|
82
|
-
"rollup": "^4.18.
|
|
83
|
-
"typescript": "^5.5.
|
|
82
|
+
"rollup": "^4.18.1",
|
|
83
|
+
"typescript": "^5.5.3"
|
|
84
84
|
},
|
|
85
85
|
"lint-staged": {
|
|
86
86
|
"**/*.ts": "eslint",
|
|
87
|
-
"**/*.{js,ts,md}": "prettier
|
|
87
|
+
"**/*.{js,ts,md}": "prettier -w"
|
|
88
88
|
},
|
|
89
89
|
"config": {
|
|
90
90
|
"commitizen": {
|
|
@@ -93,9 +93,8 @@
|
|
|
93
93
|
},
|
|
94
94
|
"dependencies": {
|
|
95
95
|
"cache2": "^3.0.0",
|
|
96
|
-
"emitter-pro": "^1.2.1",
|
|
97
96
|
"tslib": "^2.6.3",
|
|
98
|
-
"ut2": "^1.
|
|
97
|
+
"ut2": "^1.10.1"
|
|
99
98
|
},
|
|
100
99
|
"publishConfig": {
|
|
101
100
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 检查文件是否符合 `accept` 类型说明符。
|
|
3
|
+
*
|
|
4
|
+
* @static
|
|
5
|
+
* @alias module:Other.checkFileType
|
|
6
|
+
* @since 5.1.0
|
|
7
|
+
* @see {@link https://developer.mozilla.org/zh-CN/docs/Web/HTML/Element/input/file#唯一文件类型说明符 唯一文件类型说明符}
|
|
8
|
+
* @see {@link https://www.iana.org/assignments/media-types/media-types.xhtml Media Types}
|
|
9
|
+
* @param {File} file 文件对象。
|
|
10
|
+
* @param {string} [accept] 文件类型说明符。
|
|
11
|
+
* @returns {boolean} 如果 `file` 符合 `accept` 返回 `true`, 否则返回 `false`。
|
|
12
|
+
*/
|
|
13
|
+
declare function checkFileType(file: File, accept?: string): boolean;
|
|
14
|
+
export default checkFileType;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取文件类型
|
|
3
|
+
*
|
|
4
|
+
* @static
|
|
5
|
+
* @alias module:Other.getFileType
|
|
6
|
+
* @since 5.1.0
|
|
7
|
+
* @param {File} file 文件对象。
|
|
8
|
+
* @returns {"image" | "audio" | "video" | "pdf" | "word" | "excel" | undefined} 如果是 `image` `audio` `video` `pdf` `word` `excel` 这些类型的文件,返回对应的类型值,否则返回 `undefined`。
|
|
9
|
+
*/
|
|
10
|
+
declare function getFileType(file: File): "image" | "audio" | "video" | "pdf" | "word" | "excel" | undefined;
|
|
11
|
+
export default getFileType;
|
package/types/index.d.ts
CHANGED
|
@@ -99,9 +99,11 @@ export { default as lcm } from './lcm';
|
|
|
99
99
|
*/
|
|
100
100
|
export { default as ajax } from './ajax';
|
|
101
101
|
export { default as calculateCursorPosition } from './calculateCursorPosition';
|
|
102
|
+
export { default as checkFileType } from './checkFileType';
|
|
102
103
|
export { default as compressImage } from './compressImage';
|
|
103
104
|
export { default as download } from './download';
|
|
104
105
|
export { default as getFileBlob } from './getFileBlob';
|
|
106
|
+
export { default as getFileType } from './getFileType';
|
|
105
107
|
export { default as getImageInfo } from './getImageInfo';
|
|
106
108
|
export { default as loadImage } from './loadImage';
|
|
107
109
|
export { default as loadImageWithBlob } from './loadImageWithBlob';
|