ziya-utils 1.0.2 → 1.0.31
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/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/date/index.d.ts +29 -0
- package/dist/date/index.js +76 -0
- package/dist/file/index.d.ts +13 -0
- package/dist/file/index.js +45 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +5 -0
- package/dist/node_modules/tslib/tslib.es6.js +75 -0
- package/dist/package.json +35 -0
- package/dist/regexp/index.d.ts +53 -0
- package/dist/regexp/index.js +59 -0
- package/dist/security/index.d.ts +9 -0
- package/dist/security/index.js +15 -0
- package/dist/time/index.d.ts +12 -0
- package/dist/time/index.js +19 -0
- package/package.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 glk
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
### 搭建步骤
|
|
2
|
+
```bash
|
|
3
|
+
# npm init
|
|
4
|
+
npm init -y
|
|
5
|
+
|
|
6
|
+
# typescript 环境
|
|
7
|
+
yarn add typescript -D
|
|
8
|
+
|
|
9
|
+
tsc --init
|
|
10
|
+
|
|
11
|
+
# package.json 修改
|
|
12
|
+
- "main": "index.js",
|
|
13
|
+
+ "main": "./dist/index.cjs.js",
|
|
14
|
+
+ "module": "./dist/index.esm.js",
|
|
15
|
+
+ "types": "./dist/index.d.ts",
|
|
16
|
+
+ "files": [
|
|
17
|
+
+ "dist"
|
|
18
|
+
+ ],
|
|
19
|
+
|
|
20
|
+
# rollup打包环境
|
|
21
|
+
yarn add rollup @rollup/plugin-typescript @rollup/plugin-node-resolve @rollup/plugin-commonjs tslib -D
|
|
22
|
+
|
|
23
|
+
# 配置 rollup.config.js
|
|
24
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
25
|
+
import typescript from '@rollup/plugin-typescript';
|
|
26
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
27
|
+
|
|
28
|
+
export default [
|
|
29
|
+
{
|
|
30
|
+
input: './src/index.ts',
|
|
31
|
+
output: {
|
|
32
|
+
dir: 'dist',
|
|
33
|
+
format: 'cjs',
|
|
34
|
+
entryFileNames: '[name].cjs.js',
|
|
35
|
+
},
|
|
36
|
+
plugins: [resolve(), commonjs(), typescript()],
|
|
37
|
+
}, {
|
|
38
|
+
input: './src/index.ts',
|
|
39
|
+
output: {
|
|
40
|
+
dir: 'dist',
|
|
41
|
+
format: 'esm',
|
|
42
|
+
entryFileNames: '[name].esm.js',
|
|
43
|
+
},
|
|
44
|
+
plugins: [resolve(), commonjs(), typescript()],
|
|
45
|
+
}
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
# 修改 scripts
|
|
49
|
+
"scripts": {
|
|
50
|
+
+ "dev": "rollup -w -c",
|
|
51
|
+
+ "build": "rollup -c"
|
|
52
|
+
- "test": "echo \"Error: run tests from root\" && exit 1"
|
|
53
|
+
},
|
|
54
|
+
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
### 核心结构
|
|
59
|
+
- 正则表达式
|
|
60
|
+
- 安全工具
|
|
61
|
+
- 日期时间
|
|
62
|
+
- websocket
|
|
63
|
+
- 串口通信
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 判断时间戳格式是否正确且有意义
|
|
3
|
+
* @param timestamp 要验证的时间戳,可以是数字或字符串
|
|
4
|
+
* @returns 如果时间戳格式正确且有意义则返回true,否则返回false
|
|
5
|
+
*/
|
|
6
|
+
declare function isValidTimestamp(timestamp: string | number): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* 是不是秒级时间戳
|
|
9
|
+
* @param timestamp
|
|
10
|
+
* @returns
|
|
11
|
+
*/
|
|
12
|
+
declare const isSecondTimestamp: (timestamp: string | number) => boolean;
|
|
13
|
+
/**
|
|
14
|
+
* 是不是毫秒级时间戳
|
|
15
|
+
* @param timestamp
|
|
16
|
+
* @returns
|
|
17
|
+
*/
|
|
18
|
+
declare const isMillisecondTimestamp: (timestamp: string | number) => boolean;
|
|
19
|
+
/**
|
|
20
|
+
* 计算传入时间戳和当前时间的时间差
|
|
21
|
+
* @param timestamp 时间戳
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
declare const getElapsedTimeSince: (timestamp: number) => {
|
|
25
|
+
hours: number;
|
|
26
|
+
minutes: number;
|
|
27
|
+
seconds: number;
|
|
28
|
+
};
|
|
29
|
+
export { isValidTimestamp, isSecondTimestamp, isMillisecondTimestamp, getElapsedTimeSince, };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 判断时间戳格式是否正确且有意义
|
|
3
|
+
* @param timestamp 要验证的时间戳,可以是数字或字符串
|
|
4
|
+
* @returns 如果时间戳格式正确且有意义则返回true,否则返回false
|
|
5
|
+
*/
|
|
6
|
+
function isValidTimestamp(timestamp) {
|
|
7
|
+
// 如果是字符串,尝试转换为数字
|
|
8
|
+
if (typeof timestamp === "string") {
|
|
9
|
+
// 检查是否只包含数字
|
|
10
|
+
if (!/^\d+$/.test(timestamp)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
timestamp = Number(timestamp);
|
|
14
|
+
}
|
|
15
|
+
// 检查是否为数字且不为NaN
|
|
16
|
+
if (typeof timestamp !== "number" || isNaN(timestamp)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
// 检查时间戳范围是否合理
|
|
20
|
+
// 时间戳下限:1970年1月1日
|
|
21
|
+
var minTimestamp = 0;
|
|
22
|
+
// 时间戳上限:设置为一个合理的未来日期,例如2100年1月1日
|
|
23
|
+
var maxTimestamp = 4102444800000; // 2100-01-01的毫秒时间戳
|
|
24
|
+
// 检查时间戳是否为13位(毫秒级)或10位(秒级)
|
|
25
|
+
if (timestamp.toString().length === 13) {
|
|
26
|
+
// 毫秒级时间戳
|
|
27
|
+
return timestamp >= minTimestamp && timestamp <= maxTimestamp;
|
|
28
|
+
}
|
|
29
|
+
else if (timestamp.toString().length === 10) {
|
|
30
|
+
// 秒级时间戳,转换为毫秒级进行比较
|
|
31
|
+
return timestamp >= minTimestamp / 1000 && timestamp <= maxTimestamp / 1000;
|
|
32
|
+
}
|
|
33
|
+
// 不符合常见的时间戳长度
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 是不是秒级时间戳
|
|
38
|
+
* @param timestamp
|
|
39
|
+
* @returns
|
|
40
|
+
*/
|
|
41
|
+
var isSecondTimestamp = function (timestamp) {
|
|
42
|
+
return isValidTimestamp(timestamp) && timestamp.toString().length === 10;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* 是不是毫秒级时间戳
|
|
46
|
+
* @param timestamp
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
var isMillisecondTimestamp = function (timestamp) {
|
|
50
|
+
return isValidTimestamp(timestamp) && timestamp.toString().length === 13;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* 计算传入时间戳和当前时间的时间差
|
|
54
|
+
* @param timestamp 时间戳
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
var getElapsedTimeSince = function (timestamp) {
|
|
58
|
+
var now = Date.now();
|
|
59
|
+
if (!isValidTimestamp(timestamp)) {
|
|
60
|
+
timestamp = now;
|
|
61
|
+
}
|
|
62
|
+
var diff = Math.max(0, now - (isSecondTimestamp(timestamp) ? timestamp * 1000 : timestamp));
|
|
63
|
+
// 计算小时
|
|
64
|
+
var hours = Math.floor(diff / (1000 * 60 * 60));
|
|
65
|
+
// 计算分钟
|
|
66
|
+
var minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
|
|
67
|
+
// 计算秒
|
|
68
|
+
var seconds = Math.floor((diff % (1000 * 60)) / 1000);
|
|
69
|
+
return {
|
|
70
|
+
hours: hours,
|
|
71
|
+
minutes: minutes,
|
|
72
|
+
seconds: seconds,
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export { getElapsedTimeSince, isMillisecondTimestamp, isSecondTimestamp, isValidTimestamp };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取文件名
|
|
3
|
+
* @param file_path 文件路径
|
|
4
|
+
* @returns
|
|
5
|
+
*/
|
|
6
|
+
declare const getFileName: (file_path: string) => string;
|
|
7
|
+
/**
|
|
8
|
+
* 文件下载
|
|
9
|
+
* @param url 文件路径
|
|
10
|
+
* @param name 文件名
|
|
11
|
+
*/
|
|
12
|
+
declare const downloadFile: (url: string, name?: string) => Promise<void>;
|
|
13
|
+
export { getFileName, downloadFile };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { __awaiter, __generator } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 获取文件名
|
|
5
|
+
* @param file_path 文件路径
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
var getFileName = function (file_path) {
|
|
9
|
+
if (!file_path) {
|
|
10
|
+
return "";
|
|
11
|
+
}
|
|
12
|
+
var index = file_path.lastIndexOf("/");
|
|
13
|
+
if (index === -1) {
|
|
14
|
+
return "";
|
|
15
|
+
}
|
|
16
|
+
return file_path.substring(index + 1);
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* 文件下载
|
|
20
|
+
* @param url 文件路径
|
|
21
|
+
* @param name 文件名
|
|
22
|
+
*/
|
|
23
|
+
var downloadFile = function (url, name) { return __awaiter(void 0, void 0, void 0, function () {
|
|
24
|
+
var fileName, blob, blobUrl, link;
|
|
25
|
+
return __generator(this, function (_a) {
|
|
26
|
+
switch (_a.label) {
|
|
27
|
+
case 0:
|
|
28
|
+
fileName = name || getFileName(url);
|
|
29
|
+
return [4 /*yield*/, fetch(url).then(function (response) { return response.blob(); })];
|
|
30
|
+
case 1:
|
|
31
|
+
blob = _a.sent();
|
|
32
|
+
blobUrl = URL.createObjectURL(new Blob([blob]));
|
|
33
|
+
link = document.createElement("a");
|
|
34
|
+
link.href = blobUrl;
|
|
35
|
+
link.download = fileName;
|
|
36
|
+
document.body.appendChild(link);
|
|
37
|
+
link.click();
|
|
38
|
+
URL.revokeObjectURL(blobUrl);
|
|
39
|
+
link.remove();
|
|
40
|
+
return [2 /*return*/];
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}); };
|
|
44
|
+
|
|
45
|
+
export { downloadFile, getFileName };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { downloadFile, getFileName } from './file/index.js';
|
|
2
|
+
export { sleep } from './time/index.js';
|
|
3
|
+
export { getElapsedTimeSince, isMillisecondTimestamp, isSecondTimestamp, isValidTimestamp } from './date/index.js';
|
|
4
|
+
export { getUuid } from './security/index.js';
|
|
5
|
+
export { RegexPatterns, RegexValidator } from './regexp/index.js';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/******************************************************************************
|
|
2
|
+
Copyright (c) Microsoft Corporation.
|
|
3
|
+
|
|
4
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
5
|
+
purpose with or without fee is hereby granted.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
8
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
9
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
10
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
11
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
12
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
13
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
***************************************************************************** */
|
|
15
|
+
/* global Reflect, Promise, SuppressedError, Symbol */
|
|
16
|
+
|
|
17
|
+
var extendStatics = function(d, b) {
|
|
18
|
+
extendStatics = Object.setPrototypeOf ||
|
|
19
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
20
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
21
|
+
return extendStatics(d, b);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
function __extends(d, b) {
|
|
25
|
+
if (typeof b !== "function" && b !== null)
|
|
26
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
27
|
+
extendStatics(d, b);
|
|
28
|
+
function __() { this.constructor = d; }
|
|
29
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
33
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
34
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
35
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
36
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
37
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
38
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function __generator(thisArg, body) {
|
|
43
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
44
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
45
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
46
|
+
function step(op) {
|
|
47
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
48
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
49
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
50
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
51
|
+
switch (op[0]) {
|
|
52
|
+
case 0: case 1: t = op; break;
|
|
53
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
54
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
55
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
56
|
+
default:
|
|
57
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
58
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
59
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
60
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
61
|
+
if (t[2]) _.ops.pop();
|
|
62
|
+
_.trys.pop(); continue;
|
|
63
|
+
}
|
|
64
|
+
op = body.call(thisArg, _);
|
|
65
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
66
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
71
|
+
var e = new Error(message);
|
|
72
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export { __awaiter, __extends, __generator };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "ziya-utils",
|
|
3
|
+
"version": "1.0.31",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.esm.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"package.json",
|
|
10
|
+
"README.md"
|
|
11
|
+
],
|
|
12
|
+
"scripts": {
|
|
13
|
+
"dev": "rollup -w -c --bundleConfigAsCjs",
|
|
14
|
+
"build": "rollup -c --bundleConfigAsCjs"
|
|
15
|
+
},
|
|
16
|
+
"keywords": ["utils", "ziya", "tools", "javascript", "typescript"],
|
|
17
|
+
"author": "glk",
|
|
18
|
+
"license": "ISC",
|
|
19
|
+
"description": "ziya-utils",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"@rollup/plugin-commonjs": "^25.0.3",
|
|
22
|
+
"@rollup/plugin-node-resolve": "^15.1.0",
|
|
23
|
+
"@rollup/plugin-strip": "^3.0.4",
|
|
24
|
+
"@rollup/plugin-typescript": "^11.1.2",
|
|
25
|
+
"rollup": "^3.26.3",
|
|
26
|
+
"rollup-plugin-copy": "^3.5.0",
|
|
27
|
+
"rollup-plugin-node-externals": "5",
|
|
28
|
+
"tslib": "^2.6.0",
|
|
29
|
+
"typescript": "^5.1.6"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=16"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 正则表达式工具模块
|
|
3
|
+
* @module RegexUtils
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* 正则表达式模式类型定义
|
|
7
|
+
*/
|
|
8
|
+
interface IRegexPatterns {
|
|
9
|
+
/** 手机号码正则 */
|
|
10
|
+
readonly PHONE: RegExp;
|
|
11
|
+
/** 邮箱地址正则 */
|
|
12
|
+
readonly EMAIL: RegExp;
|
|
13
|
+
/** 身份证号码正则 */
|
|
14
|
+
readonly ID_CARD: RegExp;
|
|
15
|
+
/** URL地址正则 */
|
|
16
|
+
readonly URL: RegExp;
|
|
17
|
+
/** IPv4地址正则 */
|
|
18
|
+
readonly IPV4: RegExp;
|
|
19
|
+
/** 强密码正则 */
|
|
20
|
+
readonly STRONG_PASSWORD: RegExp;
|
|
21
|
+
/** 中文字符正则 */
|
|
22
|
+
readonly CHINESE: RegExp;
|
|
23
|
+
}
|
|
24
|
+
declare const RegexPatterns: IRegexPatterns;
|
|
25
|
+
declare abstract class IRegexValidatorStatic {
|
|
26
|
+
static isPhone: (phone: string) => boolean;
|
|
27
|
+
static isEmail: (email: string) => boolean;
|
|
28
|
+
static isIdCard: (idCard: string) => boolean;
|
|
29
|
+
static isUrl: (url: string) => boolean;
|
|
30
|
+
static isIpv4: (ip: string) => boolean;
|
|
31
|
+
static isStrongPassword: (password: string) => boolean;
|
|
32
|
+
static isChinese: (text: string) => boolean;
|
|
33
|
+
}
|
|
34
|
+
declare class RegexValidator extends IRegexValidatorStatic {
|
|
35
|
+
static isPhone(phone: string): boolean;
|
|
36
|
+
static isEmail(email: string): boolean;
|
|
37
|
+
static isIdCard(idCard: string): boolean;
|
|
38
|
+
static isUrl(url: string): boolean;
|
|
39
|
+
static isIpv4(ip: string): boolean;
|
|
40
|
+
static isStrongPassword(password: string): boolean;
|
|
41
|
+
static isChinese(text: string): boolean;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 默认导出类型定义
|
|
45
|
+
*/
|
|
46
|
+
interface IRegexUtils {
|
|
47
|
+
patterns: IRegexPatterns;
|
|
48
|
+
validator: typeof RegexValidator;
|
|
49
|
+
}
|
|
50
|
+
export { RegexPatterns, RegexValidator, };
|
|
51
|
+
export type { IRegexPatterns, IRegexUtils };
|
|
52
|
+
declare const _default: IRegexUtils;
|
|
53
|
+
export default _default;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { __extends } from '../node_modules/tslib/tslib.es6.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 正则表达式工具模块
|
|
5
|
+
* @module RegexUtils
|
|
6
|
+
*/
|
|
7
|
+
// 正则表达式常量
|
|
8
|
+
var RegexPatterns = {
|
|
9
|
+
/** 手机号码正则 */
|
|
10
|
+
PHONE: /^1[3-9]\d{9}$/,
|
|
11
|
+
/** 邮箱地址正则 */
|
|
12
|
+
EMAIL: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
|
|
13
|
+
/** 身份证号码正则(支持15位和18位) */
|
|
14
|
+
ID_CARD: /(^\d{15}$)|(^\d{17}(\d|X|x)$)/,
|
|
15
|
+
/** URL地址正则 */
|
|
16
|
+
URL: /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})([/\w .-]*)*\/?$/,
|
|
17
|
+
/** IPv4地址正则 */
|
|
18
|
+
IPV4: /^(\d{1,3}\.){3}\d{1,3}$/,
|
|
19
|
+
/** 强密码正则(至少8位,包含大小写字母、数字和特殊字符) */
|
|
20
|
+
STRONG_PASSWORD: /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/,
|
|
21
|
+
/** 中文字符正则 */
|
|
22
|
+
CHINESE: /^[\u4e00-\u9fa5]+$/
|
|
23
|
+
};
|
|
24
|
+
// 使用 `abstract class` 代替 `interface`,用于静态方法约束
|
|
25
|
+
var IRegexValidatorStatic = /** @class */ (function () {
|
|
26
|
+
function IRegexValidatorStatic() {
|
|
27
|
+
}
|
|
28
|
+
return IRegexValidatorStatic;
|
|
29
|
+
}());
|
|
30
|
+
var RegexValidator = /** @class */ (function (_super) {
|
|
31
|
+
__extends(RegexValidator, _super);
|
|
32
|
+
function RegexValidator() {
|
|
33
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
34
|
+
}
|
|
35
|
+
RegexValidator.isPhone = function (phone) {
|
|
36
|
+
return RegexPatterns.PHONE.test(phone);
|
|
37
|
+
};
|
|
38
|
+
RegexValidator.isEmail = function (email) {
|
|
39
|
+
return RegexPatterns.EMAIL.test(email);
|
|
40
|
+
};
|
|
41
|
+
RegexValidator.isIdCard = function (idCard) {
|
|
42
|
+
return RegexPatterns.ID_CARD.test(idCard);
|
|
43
|
+
};
|
|
44
|
+
RegexValidator.isUrl = function (url) {
|
|
45
|
+
return RegexPatterns.URL.test(url);
|
|
46
|
+
};
|
|
47
|
+
RegexValidator.isIpv4 = function (ip) {
|
|
48
|
+
return RegexPatterns.IPV4.test(ip);
|
|
49
|
+
};
|
|
50
|
+
RegexValidator.isStrongPassword = function (password) {
|
|
51
|
+
return RegexPatterns.STRONG_PASSWORD.test(password);
|
|
52
|
+
};
|
|
53
|
+
RegexValidator.isChinese = function (text) {
|
|
54
|
+
return RegexPatterns.CHINESE.test(text);
|
|
55
|
+
};
|
|
56
|
+
return RegexValidator;
|
|
57
|
+
}(IRegexValidatorStatic));
|
|
58
|
+
|
|
59
|
+
export { RegexPatterns, RegexValidator };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 通用安全工具
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* 生成通用唯一识别码(UUID)
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
var getUuid = function () {
|
|
9
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
|
|
10
|
+
var r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8;
|
|
11
|
+
return v.toString(16);
|
|
12
|
+
});
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export { getUuid };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview 时间模块
|
|
3
|
+
* @date 2025-04-02
|
|
4
|
+
* @author glk
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* 休眠
|
|
8
|
+
* @param time - 休眠时间(s)
|
|
9
|
+
* @returns
|
|
10
|
+
*/
|
|
11
|
+
var sleep = function (time) {
|
|
12
|
+
return new Promise(function (resolve) {
|
|
13
|
+
setTimeout(function () {
|
|
14
|
+
resolve(true);
|
|
15
|
+
}, time * 1000);
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { sleep };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ziya-utils",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.31",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.esm.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
"dev": "rollup -w -c --bundleConfigAsCjs",
|
|
14
14
|
"build": "rollup -c --bundleConfigAsCjs"
|
|
15
15
|
},
|
|
16
|
-
"keywords": [],
|
|
16
|
+
"keywords": ["utils", "ziya", "tools", "javascript", "typescript"],
|
|
17
17
|
"author": "glk",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"description": "ziya-utils",
|