ph-utils 0.2.5 → 0.2.9
Sign up to get free protection for your applications and to get access to all the features.
- package/lib/date.js +4 -4
- package/lib/file.d.ts +6 -0
- package/lib/file.js +12 -1
- package/lib/index.js +17 -12
- package/lib/server.js +1 -1
- package/lib/validator.d.ts +13 -8
- package/lib/validator_m.d.ts +13 -8
- package/lib/validator_m.js +1 -0
- package/package.json +2 -2
package/lib/date.js
CHANGED
@@ -100,10 +100,10 @@ function startOf(date, unit, isEnd = false) {
|
|
100
100
|
exports.startOf = startOf;
|
101
101
|
/**
|
102
102
|
* 日期加上指定时间后的日期
|
103
|
-
* @param date
|
104
|
-
* @param num
|
105
|
-
* @param unit
|
106
|
-
* @param fmt
|
103
|
+
* @param date 指定的日期
|
104
|
+
* @param num 需要添加的数字, 如果这个参数传递一个小于0的数字,则就是日期减去相应的数字
|
105
|
+
* @param unit 需要添加的单位,date - 加减天数
|
106
|
+
* @param fmt 可选参数,如果传递了格式化的单位,则返回格式化后的日期, 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒
|
107
107
|
* @returns {Date | string} 如果传递了 fmt 参数,则返回 string,否则返回 Date
|
108
108
|
*/
|
109
109
|
function add(date, num, unit, fmt) {
|
package/lib/file.d.ts
CHANGED
@@ -24,5 +24,11 @@ declare const _default: {
|
|
24
24
|
* @param done 遍历完成后的回调
|
25
25
|
*/
|
26
26
|
traverseDir(dir: string, callback?: (filename: string) => void, done?: () => void): void;
|
27
|
+
/**
|
28
|
+
* 根据文件的 stat 获取文件的 etag
|
29
|
+
* @param filePath 文件地址
|
30
|
+
* @returns file stat etag
|
31
|
+
*/
|
32
|
+
statTag(filePath: string): Promise<string>;
|
27
33
|
};
|
28
34
|
export = _default;
|
package/lib/file.js
CHANGED
@@ -71,7 +71,9 @@ module.exports = {
|
|
71
71
|
clearTimeout(t);
|
72
72
|
t = setTimeout(() => {
|
73
73
|
setImmediate(() => {
|
74
|
-
done
|
74
|
+
if (typeof done === 'function') {
|
75
|
+
done();
|
76
|
+
}
|
75
77
|
});
|
76
78
|
}, 10);
|
77
79
|
}
|
@@ -85,4 +87,13 @@ module.exports = {
|
|
85
87
|
}
|
86
88
|
list(dir, callback, done);
|
87
89
|
},
|
90
|
+
/**
|
91
|
+
* 根据文件的 stat 获取文件的 etag
|
92
|
+
* @param filePath 文件地址
|
93
|
+
* @returns file stat etag
|
94
|
+
*/
|
95
|
+
async statTag(filePath) {
|
96
|
+
let stat = await fs.promises.stat(filePath);
|
97
|
+
return `${stat.size.toString(16)}-${stat.mtimeMs.toString(16)}`;
|
98
|
+
},
|
88
99
|
};
|
package/lib/index.js
CHANGED
@@ -1,27 +1,23 @@
|
|
1
|
-
"use strict";
|
2
1
|
/**
|
3
2
|
* node 和 web 通用的工具类
|
4
3
|
*/
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.BaseError = exports.isBoolean = exports.isNumeric = exports.shieldMobile = exports.isBlank = void 0;
|
7
4
|
/**
|
8
5
|
* 验证字符串是否为空
|
9
6
|
* @param str 待验证的字符串
|
10
7
|
* @param ignoreWhitespace 是否忽略空格(包括空白字符串以及[\r\t\n]之类的制表符),默认为true
|
11
8
|
*/
|
12
|
-
function isBlank(str, ignoreWhitespace = true) {
|
9
|
+
export function isBlank(str, ignoreWhitespace = true) {
|
13
10
|
if (str == null) {
|
14
11
|
return true;
|
15
12
|
}
|
16
13
|
return (ignoreWhitespace ? str.trim().length : str.length) === 0;
|
17
14
|
}
|
18
|
-
exports.isBlank = isBlank;
|
19
15
|
/**
|
20
16
|
* 屏蔽手机号,中间部分用 * 展示
|
21
17
|
* @param mobile 待屏蔽的手机号
|
22
18
|
* @returns 屏蔽后的手机号,例如:123 **** 1234
|
23
19
|
*/
|
24
|
-
function shieldMobile(mobile) {
|
20
|
+
export function shieldMobile(mobile) {
|
25
21
|
let x1 = Math.round(mobile.length / 2);
|
26
22
|
let x2 = Math.round(x1 / 2);
|
27
23
|
let shields = [' '];
|
@@ -31,7 +27,6 @@ function shieldMobile(mobile) {
|
|
31
27
|
shields.push(' ');
|
32
28
|
return mobile.substring(0, x2) + shields.join('') + mobile.substring(x2 + x1);
|
33
29
|
}
|
34
|
-
exports.shieldMobile = shieldMobile;
|
35
30
|
/**
|
36
31
|
* 验证参数是否是数字
|
37
32
|
* @param str 待验证的字符串
|
@@ -40,26 +35,36 @@ exports.shieldMobile = shieldMobile;
|
|
40
35
|
* @param numericParam.isFloat 是否是小数
|
41
36
|
* @returns true 是数字, false 不是数字
|
42
37
|
*/
|
43
|
-
function isNumeric(str, numericParam) {
|
38
|
+
export function isNumeric(str, numericParam) {
|
44
39
|
numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
|
45
40
|
let symbol = numericParam.isPositive ? '[+]?' : '[+-]?';
|
46
41
|
let main = numericParam.isFloat ? '([0-9]*[.])?[0-9]+' : '[0-9]+';
|
47
42
|
return new RegExp('^' + symbol + main + '$').test(str);
|
48
43
|
}
|
49
|
-
exports.isNumeric = isNumeric;
|
50
44
|
/**
|
51
45
|
* 验证参数是否是Boolean 类型
|
52
46
|
* @param str 待验证的字符串
|
53
47
|
* @returns
|
54
48
|
*/
|
55
|
-
function isBoolean(str) {
|
49
|
+
export function isBoolean(str) {
|
56
50
|
return ['true', 'false'].indexOf(str) >= 0;
|
57
51
|
}
|
58
|
-
exports.isBoolean = isBoolean;
|
59
52
|
/**
|
60
53
|
* 带有错误名称标记的错误类型
|
61
54
|
*/
|
62
|
-
class BaseError extends Error {
|
55
|
+
export class BaseError extends Error {
|
56
|
+
constructor() {
|
57
|
+
if (arguments.length === 1) {
|
58
|
+
super(arguments[0]);
|
59
|
+
this.name = 'BaseError';
|
60
|
+
}
|
61
|
+
else {
|
62
|
+
super(arguments[1]);
|
63
|
+
this.name = arguments[0];
|
64
|
+
}
|
65
|
+
}
|
66
|
+
}
|
67
|
+
ass BaseError extends Error {
|
63
68
|
/**
|
64
69
|
* 错误名称,类似于 Java 中的不同的 Exception[NullPointerException];
|
65
70
|
* 增加 name 字段,表明不同的错误,当需要根据不同的错误执行不同的处理的时候,会很有用
|
package/lib/server.js
CHANGED
@@ -59,7 +59,7 @@ module.exports = {
|
|
59
59
|
options = { ...(options || {}) };
|
60
60
|
const errorName = options.errorName || 'ExecError';
|
61
61
|
delete options.errorName;
|
62
|
-
child_process_1.exec(cmd, options, (error, stdout, stderr) => {
|
62
|
+
(0, child_process_1.exec)(cmd, options, (error, stdout, stderr) => {
|
63
63
|
let err = null;
|
64
64
|
let rs = null;
|
65
65
|
if (error) {
|
package/lib/validator.d.ts
CHANGED
@@ -1,8 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* 数据验证器
|
3
|
+
*/
|
1
4
|
interface RuleItem {
|
2
5
|
rule: RegExp | ((v: any) => boolean);
|
3
6
|
message: string;
|
4
7
|
sameKey?: string;
|
5
8
|
}
|
9
|
+
export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
10
|
+
rule: string | RegExp | ((v: any) => boolean);
|
11
|
+
message?: string;
|
12
|
+
});
|
13
|
+
export interface SchemaType {
|
14
|
+
key: string;
|
15
|
+
type?: string | ((v: any) => void);
|
16
|
+
rules: RuleType[];
|
17
|
+
}
|
6
18
|
/**
|
7
19
|
* 数据验证器,除了进行数据验证外,还可以同时进行数据转化
|
8
20
|
*/
|
@@ -17,14 +29,7 @@ declare class Validator {
|
|
17
29
|
* 构造数据验证转换器
|
18
30
|
* @param schemas 配置验证转换规则
|
19
31
|
*/
|
20
|
-
constructor(schemas:
|
21
|
-
key: string;
|
22
|
-
type?: string | ((v: any) => void);
|
23
|
-
rules: string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
24
|
-
rule: string | RegExp | ((v: any) => boolean);
|
25
|
-
message?: string;
|
26
|
-
})[];
|
27
|
-
}[]);
|
32
|
+
constructor(schemas: SchemaType[]);
|
28
33
|
/**
|
29
34
|
* 进行数据验证,同时根据 type 进行数据类型转换
|
30
35
|
* @param data 待验证的数据
|
package/lib/validator_m.d.ts
CHANGED
@@ -1,8 +1,20 @@
|
|
1
|
+
/**
|
2
|
+
* 数据验证器
|
3
|
+
*/
|
1
4
|
interface RuleItem {
|
2
5
|
rule: RegExp | ((v: any) => boolean);
|
3
6
|
message: string;
|
4
7
|
sameKey?: string;
|
5
8
|
}
|
9
|
+
export declare type RuleType = string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
10
|
+
rule: string | RegExp | ((v: any) => boolean);
|
11
|
+
message?: string;
|
12
|
+
});
|
13
|
+
export interface SchemaType {
|
14
|
+
key: string;
|
15
|
+
type?: string | ((v: any) => void);
|
16
|
+
rules: RuleType[];
|
17
|
+
}
|
6
18
|
/**
|
7
19
|
* 数据验证器,除了进行数据验证外,还可以同时进行数据转化
|
8
20
|
*/
|
@@ -17,14 +29,7 @@ declare class Validator {
|
|
17
29
|
* 构造数据验证转换器
|
18
30
|
* @param schemas 配置验证转换规则
|
19
31
|
*/
|
20
|
-
constructor(schemas:
|
21
|
-
key: string;
|
22
|
-
type?: string | ((v: any) => void);
|
23
|
-
rules: string | RegExp | ((v: any) => boolean) | (RegExp | string | ((v: any) => boolean) | {
|
24
|
-
rule: string | RegExp | ((v: any) => boolean);
|
25
|
-
message?: string;
|
26
|
-
})[];
|
27
|
-
}[]);
|
32
|
+
constructor(schemas: SchemaType[]);
|
28
33
|
/**
|
29
34
|
* 进行数据验证,同时根据 type 进行数据类型转换
|
30
35
|
* @param data 待验证的数据
|
package/lib/validator_m.js
CHANGED
package/package.json
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
"module": "lib/index_m.js",
|
6
6
|
"types": "lib/index.d.ts",
|
7
7
|
"browser": "lib/index_m.js",
|
8
|
-
"version": "0.2.
|
8
|
+
"version": "0.2.9",
|
9
9
|
"repository": {
|
10
10
|
"type": "git",
|
11
11
|
"url": "git+https//gitee.com/towardly/ph.git",
|
@@ -19,7 +19,7 @@
|
|
19
19
|
"homepage": "https://gitee.com/towardly/ph/tree/master/packages/utils",
|
20
20
|
"devDependencies": {
|
21
21
|
"@types/node": "^17.0.21",
|
22
|
-
"typescript": "
|
22
|
+
"typescript": "4.5.5"
|
23
23
|
},
|
24
24
|
"scripts": {
|
25
25
|
"build": "node scripts/build.js"
|