ph-utils 0.4.5 → 0.4.7

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/lib/date.d.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  /**
2
2
  * 将日期格式化为指定形式的字符串
3
3
  * @param date 日期
4
- * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss -
4
+ * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒, S - 毫秒, 默认: yyyy-mm-dd HH:MM:ss
5
5
  */
6
- export declare function format(date?: Date | string | number, pattern?: string): string;
6
+ export declare function format(date?: Date | string | number | null, pattern?: string): string;
7
7
  /**
8
8
  * 将指定的参数解析为日期对象(Date)
9
9
  * 参考 dayjs 实现, 也可以参考 https://github.com/nomiddlename/date-format
10
10
  * @param date 待解析的日期参数
11
11
  */
12
- export declare function parse(date?: Date | string | number): Date;
12
+ export declare function parse(date?: Date | string | number | null): Date;
13
13
  /**
14
14
  * 设置日期的开始或者结束的点
15
15
  * @param {Object} date 日期,能够被 parse 解析的日期
package/lib/date.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * node 和 web 端日期处理工具类
3
3
  */
4
4
  // 配置日期格式的正则表达式
5
- const REGEX_FORMAT = /yy(?:yy)?|([HMmds])\1?/g;
5
+ const REGEX_FORMAT = /yy(?:yy)?|([HMmds])\1?|(S)?/g;
6
6
  // 由于 Date.parse() 不能正确解析 yyyy-dd-mm 格式的日期, 所以匹配手动解析
7
7
  const REGEX_PARSE = /^(\d{4})-?(\d{1,2})-?(\d{0,2})[^0-9]*(\d{1,2})?:?(\d{1,2})?:?(\d{1,2})?.?(\d{1,3})?$/;
8
8
  const ofArgs = {
@@ -72,9 +72,9 @@ function getLastDayOfYear(date, month) {
72
72
  /**
73
73
  * 将日期格式化为指定形式的字符串
74
74
  * @param date 日期
75
- * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss -
75
+ * @param pattern 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒, S - 毫秒, 默认: yyyy-mm-dd HH:MM:ss
76
76
  */
77
- export function format(date, pattern = "yyyy-mm-dd HH:MM") {
77
+ export function format(date, pattern = "yyyy-mm-dd HH:MM:ss") {
78
78
  // eslint-disable-next-line
79
79
  date = parse(date);
80
80
  const d = date.getDate();
@@ -96,6 +96,7 @@ export function format(date, pattern = "yyyy-mm-dd HH:MM") {
96
96
  MM: p(M),
97
97
  s: s,
98
98
  ss: p(s),
99
+ S: p(date.getMilliseconds(), 3),
99
100
  };
100
101
  if (pattern != null) {
101
102
  return pattern.replace(REGEX_FORMAT, (flag) => {
package/lib/file.js CHANGED
@@ -1,6 +1,6 @@
1
1
  /** nodejs 文件操作工具类 */
2
- import path from 'node:path';
3
- import fs from 'node:fs';
2
+ import path from "node:path";
3
+ import fs from "node:fs";
4
4
  /**
5
5
  * 读取文件内容为JSON格式
6
6
  * @param filepath 读取的文件路径
@@ -8,7 +8,7 @@ import fs from 'node:fs';
8
8
  */
9
9
  export function readJSON(filepath) {
10
10
  return new Promise((resolve, reject) => {
11
- fs.readFile(path.resolve(filepath), 'utf-8', (err, data) => {
11
+ fs.readFile(path.resolve(filepath), "utf-8", (err, data) => {
12
12
  if (err) {
13
13
  reject(err);
14
14
  }
@@ -29,8 +29,8 @@ export function readJSON(filepath) {
29
29
  export function write(file, data, opts) {
30
30
  return new Promise((resolve, reject) => {
31
31
  let writeData = data.toString();
32
- opts = { json: true, format: true, ...(opts || {}) };
33
- if (opts.json === true && typeof data === 'object') {
32
+ opts = { json: true, format: true, ...opts };
33
+ if (opts.json === true && typeof data === "object") {
34
34
  writeData = JSON.stringify(data, null, opts.format === true ? 2 : 0);
35
35
  }
36
36
  fs.writeFile(path.resolve(file), writeData, (err) => {
@@ -55,21 +55,21 @@ export function traverseDir(dir, callback, done) {
55
55
  fs.readdir(path.resolve(dr), { withFileTypes: true }, (err, files) => {
56
56
  if (err && err.errno === -4052) {
57
57
  // 本身就是文件
58
- if (typeof cb === 'function')
58
+ if (typeof cb === "function")
59
59
  cb(dr);
60
- if (typeof d === 'function')
60
+ if (typeof d === "function")
61
61
  d(); // 遍历完成
62
62
  }
63
63
  else {
64
64
  for (let i = 0, len = files.length; i < len; i++) {
65
65
  const file = files[i];
66
66
  if (file.isFile()) {
67
- if (typeof cb === 'function')
67
+ if (typeof cb === "function")
68
68
  cb(path.join(dr, file.name));
69
69
  clearTimeout(t);
70
70
  t = setTimeout(() => {
71
71
  setImmediate(() => {
72
- if (typeof done === 'function') {
72
+ if (typeof done === "function") {
73
73
  done();
74
74
  }
75
75
  });
package/lib/index.d.ts CHANGED
@@ -50,6 +50,13 @@ interface RandomConfig {
50
50
  * 1. 生成指定长度的随机数
51
51
  * 2. 生成介于 [min, max] 之间的随机数
52
52
  * @param opts 生成随机数的配置
53
+ *
54
+ * @example <caption>1. 生成指定长度的随机字符串</caption>
55
+ * random(1); // 长度为 1 的随机字符串
56
+ *
57
+ * @example <caption>2. 生成纯数字且不能为0长度为1的随机字符</caption>
58
+ * random({ length: 1, hasLetter: false, firstIsZero: false })
59
+ *
53
60
  * @returns
54
61
  */
55
62
  export declare function random(opts: number | RandomConfig): string | number;
package/lib/index.js CHANGED
@@ -40,7 +40,7 @@ export function shieldMobile(mobile) {
40
40
  * @returns true 是数字, false 不是数字
41
41
  */
42
42
  export function isNumeric(str, numericParam) {
43
- numericParam = { isPositive: false, isFloat: true, ...(numericParam || {}) };
43
+ numericParam = { isPositive: false, isFloat: true, ...numericParam };
44
44
  const symbol = numericParam.isPositive ? "[+]?" : "[+-]?";
45
45
  const main = numericParam.isFloat ? "([0-9]*[.])?[0-9]+" : "[0-9]+";
46
46
  return new RegExp("^" + symbol + main + "$").test(str);
@@ -58,6 +58,13 @@ export function isBoolean(str) {
58
58
  * 1. 生成指定长度的随机数
59
59
  * 2. 生成介于 [min, max] 之间的随机数
60
60
  * @param opts 生成随机数的配置
61
+ *
62
+ * @example <caption>1. 生成指定长度的随机字符串</caption>
63
+ * random(1); // 长度为 1 的随机字符串
64
+ *
65
+ * @example <caption>2. 生成纯数字且不能为0长度为1的随机字符</caption>
66
+ * random({ length: 1, hasLetter: false, firstIsZero: false })
67
+ *
61
68
  * @returns
62
69
  */
63
70
  export function random(opts) {
@@ -74,12 +81,11 @@ export function random(opts) {
74
81
  }
75
82
  const len = typeof opts === "object" ? opts.length : opts;
76
83
  /* 生成指定长度的随机数 */
77
- const charLens = RANDOM_CHARS.length;
78
84
  let chars = RANDOM_CHARS;
79
85
  if (typeof opts === "object" && opts.hasLetter === false) {
80
86
  chars = NUMBER_RANDOM_CHARTS;
81
87
  }
82
- const resRandom = Array.from({ length: len }, () => chars.charAt(Math.floor(Math.random() * charLens))).join("");
88
+ const resRandom = Array.from({ length: len }, () => chars.charAt(random({ min: 0, max: 9, hasEnd: true }))).join("");
83
89
  if (typeof opts === "object" &&
84
90
  opts.firstIsZero === false &&
85
91
  resRandom.indexOf("0") === 0) {
package/package.json CHANGED
@@ -52,7 +52,7 @@
52
52
  },
53
53
  "./*": "./lib/*"
54
54
  },
55
- "version": "0.4.5",
55
+ "version": "0.4.7",
56
56
  "type": "module",
57
57
  "repository": {
58
58
  "type": "git",