ph-utils 0.8.2 → 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/array.d.ts CHANGED
@@ -7,8 +7,73 @@
7
7
  */
8
8
  export declare function order<T>(arr: T[], order?: "asc" | "desc", orderKey?: string | null): T[];
9
9
  /**
10
- * 多个数组取交集, 通常用于字符串或者数字数组
11
- * @param arrs 待取交集的多个数组
12
- * @returns
10
+ * 返回一个所有集合交集的新集合
11
+ *
12
+ * 如果集合本身支持 intersection, 则调用原生 intersection 函数
13
+ *
14
+ * @param arrs
15
+ *
16
+ * @returns 新集合中的元素在传入的所有集合中同时存在
17
+ */
18
+ export declare function intersection<T>(...arrs: Set<T>[]): Set<T>;
19
+ /**
20
+ * 返回一个所有列表交集的新列表
21
+ *
22
+ * @param arrs
23
+ *
24
+ * @returns 新列表中的元素在传入的所有列表中同时存在
13
25
  */
14
26
  export declare function intersection<T>(...arrs: T[][]): T[];
27
+ /**
28
+ * 返回一个包含第一个集合中的元素但不包含后续给定集合中元素的新集合
29
+ *
30
+ * 如果集合本身支持 difference 方法,则调用原生 difference 方法
31
+ *
32
+ * @param arrs 集合列表
33
+ *
34
+ * @returns
35
+ */
36
+ export declare function difference<T>(...arrs: Set<T>[]): Set<T>;
37
+ /**
38
+ * 返回一个包含第一个列表中的元素但不包含后续给定列表中元素的新列表
39
+ *
40
+ * @param arrs 二维列表
41
+ *
42
+ * @returns
43
+ */
44
+ export declare function difference<T>(...arrs: T[][]): T[];
45
+ /**
46
+ * 返回多个集合的并集, 如果支持 union,则调用原生 union
47
+ *
48
+ * @param arrs
49
+ *
50
+ * @returns 一个包含所有给定集合的所有元素的新集合
51
+ */
52
+ export declare function union<T>(...arrs: Set<T>[]): Set<T>;
53
+ export declare function union<T>(...arrs: T[][]): T[];
54
+ export declare function symmetricDifference<T>(...arrs: Set<T>[]): Set<T>;
55
+ export declare function symmetricDifference<T>(...arrs: T[][]): T[];
56
+ /**
57
+ * 返回一个布尔值,指示此集合中的所有元素是否都在给定的集合中。
58
+ * @param a1
59
+ * @param a2
60
+ * @returns
61
+ */
62
+ export declare function isSubsetOf<T>(a1: T[] | Set<T>, a2: T[] | Set<T>): any;
63
+ /**
64
+ * 返回一个布尔值,指示给定集合中的所有元素是否都在此集合中。
65
+ * @param arr1
66
+ * @param arr2
67
+ * @returns
68
+ */
69
+ export declare function isSupersetOf<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): any;
70
+ /**
71
+ * 返回一个布尔值,指示此集合是否与给定集合没有公共元素。
72
+ *
73
+ * 判断两个集合是否有交集, 也可以通过 intersection(arr1, arr2).length 判断
74
+ *
75
+ * @param arr1
76
+ * @param arr2
77
+ * @returns
78
+ */
79
+ export declare function isDisjointFrom<T>(arr1: T[] | Set<T>, arr2: T[] | Set<T>): any;
package/lib/array.js CHANGED
@@ -26,10 +26,186 @@ export function order(arr, order = "asc", orderKey = null) {
26
26
  });
27
27
  }
28
28
  /**
29
- * 多个数组取交集, 通常用于字符串或者数字数组
30
- * @param arrs 待取交集的多个数组
29
+ * 返回所有集合交集的元素的新集合
30
+ */ export function intersection(...arrs) {
31
+ return arrs.reduce((acc, cur) => {
32
+ if (cur instanceof Set) {
33
+ //@ts-ignore
34
+ if (cur.intersection != null) {
35
+ //@ts-ignore
36
+ return acc.intersection(cur);
37
+ }
38
+ else {
39
+ const res = new Set();
40
+ for (const value of acc) {
41
+ if (cur.has(value)) {
42
+ res.add(value);
43
+ }
44
+ }
45
+ return res;
46
+ }
47
+ }
48
+ else {
49
+ return acc.filter((x) => cur.includes(x));
50
+ }
51
+ });
52
+ }
53
+ /**
54
+ * 返回一个包含第一个集合中的元素但不包含后续给定集合中元素的新集合
55
+ * @param arrs 新集合
31
56
  * @returns
32
57
  */
33
- export function intersection(...arrs) {
34
- return arrs.reduce((acc, cur) => acc.filter((x) => cur.includes(x)));
58
+ export function difference(...arrs) {
59
+ return arrs.reduce((acc, cur) => {
60
+ if (acc instanceof Set) {
61
+ //@ts-ignore
62
+ if (acc.difference != null) {
63
+ //@ts-ignore
64
+ return acc.difference(cur);
65
+ }
66
+ else {
67
+ const res = new Set();
68
+ for (const value of acc) {
69
+ if (!cur.has(value)) {
70
+ res.add(value);
71
+ }
72
+ }
73
+ return res;
74
+ }
75
+ }
76
+ else {
77
+ const res = [];
78
+ for (const value of acc) {
79
+ if (!cur.includes(value)) {
80
+ res.push(value);
81
+ }
82
+ }
83
+ return res;
84
+ }
85
+ });
86
+ }
87
+ export function union(...arrs) {
88
+ return arrs.reduce((acc, cur) => {
89
+ if (cur instanceof Set) {
90
+ //@ts-ignore
91
+ if (cur.union != null) {
92
+ //@ts-ignore
93
+ return acc.union(cur);
94
+ }
95
+ return new Set([...acc, ...cur]);
96
+ }
97
+ else {
98
+ return [...acc, ...cur];
99
+ }
100
+ });
101
+ }
102
+ /**
103
+ * 对称差: 返回一个包含此集合或给定集合中的元素的新集合,但不包含同时存在于这两个集合中的元素。
104
+ * @param arrs
105
+ * @returns
106
+ */
107
+ export function symmetricDifference(...arrs) {
108
+ return arrs.reduce((acc, cur) => {
109
+ if (cur instanceof Set) {
110
+ //@ts-ignore
111
+ if (cur.symmetricDifference != null) {
112
+ //@ts-ignore
113
+ return acc.symmetricDifference(cur);
114
+ }
115
+ const res = new Set();
116
+ for (const item of acc) {
117
+ if (!cur.has(item)) {
118
+ res.add(item);
119
+ }
120
+ }
121
+ for (const item of cur) {
122
+ if (!acc.has(item)) {
123
+ res.add(item);
124
+ }
125
+ }
126
+ return res;
127
+ }
128
+ const res = [];
129
+ for (const item of acc) {
130
+ if (!cur.includes(item)) {
131
+ res.push(item);
132
+ }
133
+ }
134
+ for (const item of cur) {
135
+ if (!acc.includes(item)) {
136
+ res.push(item);
137
+ }
138
+ }
139
+ return res;
140
+ });
141
+ }
142
+ /**
143
+ * 返回一个布尔值,指示此集合中的所有元素是否都在给定的集合中。
144
+ * @param a1
145
+ * @param a2
146
+ * @returns
147
+ */
148
+ export function isSubsetOf(a1, a2) {
149
+ //@ts-ignore
150
+ if (a1 instanceof Set && a1.isSubsetOf != null) {
151
+ //@ts-ignore
152
+ return a1.isSubsetOf(a2);
153
+ }
154
+ let is = true;
155
+ for (const item of a1) {
156
+ if (a2 instanceof Set) {
157
+ if (!a2.has(item)) {
158
+ is = false;
159
+ break;
160
+ }
161
+ }
162
+ else {
163
+ if (!a2.includes(item)) {
164
+ is = false;
165
+ break;
166
+ }
167
+ }
168
+ }
169
+ return is;
170
+ }
171
+ /**
172
+ * 返回一个布尔值,指示给定集合中的所有元素是否都在此集合中。
173
+ * @param arr1
174
+ * @param arr2
175
+ * @returns
176
+ */
177
+ export function isSupersetOf(arr1, arr2) {
178
+ return isSubsetOf(arr2, arr1);
179
+ }
180
+ /**
181
+ * 返回一个布尔值,指示此集合是否与给定集合没有公共元素。
182
+ *
183
+ * 判断两个集合是否有交集, 也可以通过 intersection(arr1, arr2).length 判断
184
+ *
185
+ * @param arr1
186
+ * @param arr2
187
+ * @returns
188
+ */
189
+ export function isDisjointFrom(arr1, arr2) {
190
+ //@ts-ignore
191
+ if (arr1 instanceof Set && arr1.isDisjointFrom != null) {
192
+ //@ts-ignore
193
+ return arr1.isDisjointFrom(arr2);
194
+ }
195
+ let is = true;
196
+ for (const item of arr1) {
197
+ if (arr2 instanceof Set) {
198
+ if (arr2.has(item)) {
199
+ is = false;
200
+ break;
201
+ }
202
+ }
203
+ else {
204
+ if (arr2.includes(item)) {
205
+ is = false;
206
+ break;
207
+ }
208
+ }
209
+ }
210
+ return is;
35
211
  }
package/lib/color.d.ts CHANGED
@@ -39,8 +39,15 @@ export declare function toHex(color: any): string;
39
39
  /**
40
40
  * 调整给定颜色深[darken]浅[lighten]
41
41
  * @param color - 输入的颜色,可以是任意颜色表示方式
42
- * @param level - 调整深浅级别, 可以是小数
43
- * @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。
42
+ * @param level - 调整深浅级别, 可以是小数。默认: 1
43
+ * @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。默认: true
44
+ *
45
+ * #### 1. 颜色变浅
46
+ *
47
+ * ```
48
+ * adjust('#4998f4', 3, true)
49
+ * ```
50
+ *
44
51
  * @returns 返回调整后颜色的十六进制字符串表示。
45
52
  */
46
53
  export declare function adjust(color: any, level?: number, light?: boolean): string;
package/lib/color.js CHANGED
@@ -272,8 +272,15 @@ export function toHex(color) {
272
272
  /**
273
273
  * 调整给定颜色深[darken]浅[lighten]
274
274
  * @param color - 输入的颜色,可以是任意颜色表示方式
275
- * @param level - 调整深浅级别, 可以是小数
276
- * @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。
275
+ * @param level - 调整深浅级别, 可以是小数。默认: 1
276
+ * @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。默认: true
277
+ *
278
+ * #### 1. 颜色变浅
279
+ *
280
+ * ```
281
+ * adjust('#4998f4', 3, true)
282
+ * ```
283
+ *
277
284
  * @returns 返回调整后颜色的十六进制字符串表示。
278
285
  */
279
286
  export function adjust(color, level = 1, light = true) {
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.2",
63
+ "version": "0.8.4",
64
64
  "type": "module",
65
65
  "repository": {
66
66
  "type": "git",