ph-utils 0.8.3 → 0.8.4

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.js CHANGED
@@ -116,8 +116,6 @@ export function format(date, pattern = "yyyy-mm-dd HH:MM:ss") {
116
116
  export function parse(date) {
117
117
  if (date == null)
118
118
  return new Date();
119
- if (date instanceof Date)
120
- return date;
121
119
  if (typeof date === "string" && !/Z$/i.test(date)) {
122
120
  const d = date.match(REGEX_PARSE);
123
121
  if (d) {
@@ -127,7 +125,7 @@ export function parse(date) {
127
125
  if (typeof date === "number") {
128
126
  return new Date(date <= 9999999999 ? date * 1000 : date);
129
127
  }
130
- return new Date();
128
+ return new Date(date);
131
129
  }
132
130
  /**
133
131
  * 设置日期的开始或者结束的点
package/lib/index.d.ts CHANGED
@@ -29,37 +29,55 @@ export declare function isNumeric(str: string, numericParam?: {
29
29
  */
30
30
  export declare function isBoolean(str: string): boolean;
31
31
  /** 生成随机数的选项 */
32
- interface RandomConfig {
32
+ interface RandomStringOption {
33
33
  /** 生成指定长度的随机字符串 */
34
- length?: number;
34
+ length: number;
35
35
  /** 是否包含英文字母, 默认为: true */
36
36
  hasLetter?: boolean;
37
37
  /** 生成纯数字的随机数时, 首位是否允许为 0, 默认为: true */
38
38
  firstIsZero?: boolean;
39
+ }
40
+ interface RangeRandomOption {
39
41
  /** 配合 max 生成 [min~max] 之间的随机数 */
40
- min?: number;
42
+ min: number;
41
43
  /** 配合 min 生成 [min~max] 之间的随机数 */
42
- max?: number;
44
+ max: number;
43
45
  /** 生成的随机数,是否包含 max, 默认: false */
44
46
  hasEnd?: boolean;
45
47
  /** 生成的随机数是否是整数, 默认: true */
46
48
  isInteger?: boolean;
47
49
  }
48
50
  /**
49
- * 生成随机数
50
- * 1. 生成指定长度的随机数
51
- * 2. 生成介于 [min, max] 之间的随机数
52
- * @param opts 生成随机数的配置
51
+ * 生成指定长度的随机数
52
+ *
53
+ * @param len - 生成的随机数长度
53
54
  *
54
55
  * @example <caption>1. 生成指定长度的随机字符串</caption>
55
56
  * random(1); // 长度为 1 的随机字符串
57
+ */
58
+ export declare function random(len: number): string;
59
+ /**
60
+ * 生成介于 [min, max] 之间的随机数
56
61
  *
57
- * @example <caption>2. 生成纯数字且不能为0长度为1的随机字符</caption>
58
- * random({ length: 1, hasLetter: false, firstIsZero: false })
62
+ * @param option 配置项
63
+ * @param option.min 生成介于 [min, max] 之间的随机数
64
+ * @param option.max 生成介于 [min, max] 之间的随机数
65
+ * @param option.hasEnd 生成的随机数,是否包含 max, 默认: false
66
+ * @param option.isInteger 生成的随机数是否是整数, 默认: true
67
+ */
68
+ export declare function random(option: RangeRandomOption): number;
69
+ /**
70
+ * 生成指定长度随机数
59
71
  *
60
- * @returns
72
+ * @param option 配置项
73
+ * @param option.length 生成的随机数长度
74
+ * @param option.hasLetter 是否包含英文字母, 默认为: true
75
+ * @param option.firstIsZero 生成纯数字的随机数时, 首位是否允许为 0, 默认为: true
76
+ *
77
+ * @example <caption>2. 生成纯数字且首位不能为0长度为1的随机字符</caption>
78
+ * random({ length: 1, hasLetter: false, firstIsZero: false })
61
79
  */
62
- export declare function random(opts: number | RandomConfig): string | number;
80
+ export declare function random(option: RandomStringOption): string;
63
81
  /**
64
82
  * 带有错误名称标记的错误类型
65
83
  */
package/lib/index.js CHANGED
@@ -53,20 +53,6 @@ export function isNumeric(str, numericParam) {
53
53
  export function isBoolean(str) {
54
54
  return ["true", "false"].indexOf(str) >= 0;
55
55
  }
56
- /**
57
- * 生成随机数
58
- * 1. 生成指定长度的随机数
59
- * 2. 生成介于 [min, max] 之间的随机数
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
- *
68
- * @returns
69
- */
70
56
  export function random(opts) {
71
57
  if (typeof opts === "object" && opts.min != null && opts.max != null) {
72
58
  const randomNum = Math.random();
@@ -79,13 +65,13 @@ export function random(opts) {
79
65
  if (typeof opts === "object" && opts.length == null) {
80
66
  throw new Error("random_length_cannot_null");
81
67
  }
82
- const len = typeof opts === "object" ? opts.length : opts;
68
+ let len = typeof opts === "object" ? opts.length : opts;
83
69
  /* 生成指定长度的随机数 */
84
70
  let chars = RANDOM_CHARS;
85
71
  if (typeof opts === "object" && opts.hasLetter === false) {
86
72
  chars = NUMBER_RANDOM_CHARTS;
87
73
  }
88
- const resRandom = Array.from({ length: len }, () => chars.charAt(random({ min: 0, max: 9, hasEnd: true }))).join("");
74
+ const resRandom = Array.from({ length: len }, () => chars.charAt(random({ min: 0, max: chars.length - 1, hasEnd: true }))).join("");
89
75
  if (typeof opts === "object" &&
90
76
  opts.firstIsZero === false &&
91
77
  resRandom.indexOf("0") === 0) {
package/lib/storage.d.ts CHANGED
@@ -11,8 +11,8 @@ interface StorageSetOption {
11
11
  * 存储值到 Storage 中
12
12
  * @param key 设置的 key
13
13
  * @param value 设置的值
14
- * @param storage session 或 local
15
- * @param expire 数据有效期, 单位秒, 默认: -1 - 永久存储
14
+ * @param [option.storage] session 或 local, 默认: session
15
+ * @param [option.expire] 数据有效期, 单位秒, 默认: -1 - 永久存储
16
16
  */
17
17
  export declare function set(key: string, value: any, option?: StorageSetOption): void;
18
18
  /**
@@ -37,8 +37,8 @@ interface StorageQueryOption {
37
37
  * 从 Storage 中取出数据
38
38
  * @param key 保存时的 key
39
39
  * @param defaultValue 没有数据时的默认值
40
- * @param delete 是否在取出后,删除数据,默认:false - 取出后删除数据
41
- * @param storage 使用的 Storage ,可以是 localStorage、sessionStorage
40
+ * @param [option.delete] 是否在取出后,删除数据,默认:false - 取出后删除数据
41
+ * @param [option.storage] 使用的 Storage ,可以是 localStorage、sessionStorage, 默认: localStorage、sessionStorage
42
42
  * @returns Storage 中 key 对应的数据
43
43
  */
44
44
  export declare function get<T>(key: string, defaultValue?: T, option?: StorageQueryOption): T;
package/lib/storage.js CHANGED
@@ -5,8 +5,8 @@ function getStorage(storage = "session") {
5
5
  * 存储值到 Storage 中
6
6
  * @param key 设置的 key
7
7
  * @param value 设置的值
8
- * @param storage session 或 local
9
- * @param expire 数据有效期, 单位秒, 默认: -1 - 永久存储
8
+ * @param [option.storage] session 或 local, 默认: session
9
+ * @param [option.expire] 数据有效期, 单位秒, 默认: -1 - 永久存储
10
10
  */
11
11
  export function set(key, value, option) {
12
12
  const opts = {
@@ -40,8 +40,8 @@ export function remove(key, storage) {
40
40
  * 从 Storage 中取出数据
41
41
  * @param key 保存时的 key
42
42
  * @param defaultValue 没有数据时的默认值
43
- * @param delete 是否在取出后,删除数据,默认:false - 取出后删除数据
44
- * @param storage 使用的 Storage ,可以是 localStorage、sessionStorage
43
+ * @param [option.delete] 是否在取出后,删除数据,默认:false - 取出后删除数据
44
+ * @param [option.storage] 使用的 Storage ,可以是 localStorage、sessionStorage, 默认: localStorage、sessionStorage
45
45
  * @returns Storage 中 key 对应的数据
46
46
  */
47
47
  export function get(key, defaultValue, option) {
package/package.json CHANGED
@@ -60,7 +60,7 @@
60
60
  },
61
61
  "./*": "./lib/*"
62
62
  },
63
- "version": "0.8.3",
63
+ "version": "0.8.4",
64
64
  "type": "module",
65
65
  "repository": {
66
66
  "type": "git",