ph-utils 0.8.1 → 0.8.3

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 ADDED
@@ -0,0 +1,79 @@
1
+ /**
2
+ * 数组排序
3
+ * @param arr 待排序数组
4
+ * @param order 排序信息, asc - 升序, desc - 倒序
5
+ * @param orderKey 如果是个对象数组,按哪个字段排序
6
+ * @returns
7
+ */
8
+ export declare function order<T>(arr: T[], order?: "asc" | "desc", orderKey?: string | null): T[];
9
+ /**
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 新列表中的元素在传入的所有列表中同时存在
25
+ */
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 ADDED
@@ -0,0 +1,211 @@
1
+ /**
2
+ * 数组排序
3
+ * @param arr 待排序数组
4
+ * @param order 排序信息, asc - 升序, desc - 倒序
5
+ * @param orderKey 如果是个对象数组,按哪个字段排序
6
+ * @returns
7
+ */
8
+ export function order(arr, order = "asc", orderKey = null) {
9
+ const collator = new Intl.Collator();
10
+ return arr.sort((a, b) => {
11
+ let aValue = a, bValue = b;
12
+ if (typeof a === "object" && orderKey != null) {
13
+ // @ts-ignore
14
+ aValue = a[orderKey];
15
+ // @ts-ignore
16
+ bValue = b[orderKey];
17
+ }
18
+ let aGtb = -1;
19
+ if (typeof aValue === "string") {
20
+ aGtb = collator.compare(aValue, bValue);
21
+ }
22
+ else {
23
+ aGtb = aValue >= bValue ? 1 : -1;
24
+ }
25
+ return order === "asc" ? aGtb : -aGtb;
26
+ });
27
+ }
28
+ /**
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 新集合
56
+ * @returns
57
+ */
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;
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.d.ts CHANGED
@@ -37,7 +37,7 @@ export declare function endOf(date?: Date | string | number, unit?: string): Dat
37
37
  * @param pre 精度, s - 精确到秒, ms - 精确到毫秒, 默认: s
38
38
  * @returns
39
39
  */
40
- export declare function timeStamp(ctime?: Date | string | number, pre?: "s" | "ms"): number;
40
+ export declare function timestamp(ctime?: Date | string | number, pre?: "s" | "ms"): number;
41
41
  /**
42
42
  * 日期加上指定时间后的日期
43
43
  * @param date 指定的日期
@@ -57,5 +57,10 @@ export declare function add(date: Date | string | number | null, num: number, un
57
57
  * @param num 需要添加的数字, 如果这个参数传递一个小于0的数字,则就是日期减去相应的数字
58
58
  * @param unit 需要添加的单位,date - 加减天数
59
59
  * @param fmt 如果传递了格式化的单位,则返回格式化后的日期, 格式化字符串 yyyy - 年, mm - 月, dd - 日, HH - 小时, MM - 分钟, ss - 秒
60
+ *
61
+ * #### 1. 前一天的日期字符串形式
62
+ * ```javascript
63
+ * add(new Date(), -1, 'Date', 'yyyy-mm-dd')
64
+ * ```
60
65
  */
61
66
  export declare function add(date: Date | string | number | null, num: number, unit: string, fmt: string): string;
package/lib/date.js CHANGED
@@ -61,7 +61,7 @@ function getUnit(unit) {
61
61
  * @param month 月份, 如果不传, 则当前月的最后一天
62
62
  * @returns
63
63
  */
64
- function getLastDayOfYear(date, month) {
64
+ function getLastDayOfMonth(date, month) {
65
65
  // 获取下个月的第一天
66
66
  const lastDate = new Date(date.getFullYear(), (month || date.getMonth()) + 1, 1);
67
67
  // 将下个月的第一天的日期减去一天,得到当前月的最后一天
@@ -154,7 +154,7 @@ export function dateOf(date, unit, isEnd = false) {
154
154
  let value = setValues[index];
155
155
  if (value === -2) {
156
156
  // 设置为某个月的最后一天的日期
157
- value = getLastDayOfYear(clone);
157
+ value = getLastDayOfMonth(clone);
158
158
  }
159
159
  Date.prototype["set" + periods[index]].apply(clone, [
160
160
  setValues[index],
@@ -186,7 +186,7 @@ export function endOf(date, unit) {
186
186
  * @param pre 精度, s - 精确到秒, ms - 精确到毫秒, 默认: s
187
187
  * @returns
188
188
  */
189
- export function timeStamp(ctime, pre = "s") {
189
+ export function timestamp(ctime, pre = "s") {
190
190
  let tm = parse(ctime).getTime();
191
191
  return pre === "s" ? Math.floor(tm / 1000) : tm;
192
192
  }
package/lib/dom.d.ts CHANGED
@@ -98,4 +98,13 @@ export declare function queryHideNodeSize(hideNode: string | HTMLElement, parent
98
98
  * @param direction 检查的方向,默认为"horizontal"
99
99
  * @returns 如果元素在父元素的可视区域内,则返回true;否则返回false。
100
100
  */
101
- export declare function isVisible(el: HTMLElement, parent?: HTMLElement, direction?: string): boolean;
101
+ export declare function isVisible(el: HTMLElement, parent?: HTMLElement | null | undefined, direction?: string): boolean;
102
+ /**
103
+ * 判断当前设备是否为移动设备。
104
+ *
105
+ * 本函数通过检查用户代理字符串和屏幕尺寸来判断设备是否为移动设备。
106
+ * 这对于需要根据设备类型进行不同布局或功能调整的应用非常有用。
107
+ *
108
+ * @returns {boolean} 如果设备是移动设备,则返回true;否则返回false。
109
+ */
110
+ export declare function isMobile(): boolean;
package/lib/dom.js CHANGED
@@ -196,7 +196,10 @@ export function queryHideNodeSize(hideNode, parent = document.body) {
196
196
  * @param direction 检查的方向,默认为"horizontal"
197
197
  * @returns 如果元素在父元素的可视区域内,则返回true;否则返回false。
198
198
  */
199
- export function isVisible(el, parent = document.body, direction = "horizontal") {
199
+ export function isVisible(el, parent = null, direction = "horizontal") {
200
+ if (parent == null) {
201
+ parent = el.offsetParent;
202
+ }
200
203
  // 获取父元素的边界信息
201
204
  const containerRect = parent.getBoundingClientRect();
202
205
  // 获取元素的边界信息
@@ -213,3 +216,24 @@ export function isVisible(el, parent = document.body, direction = "horizontal")
213
216
  // 判断元素是否完全在容器的可视区域内
214
217
  return elStart >= containerStart && elEnd <= containerEnd;
215
218
  }
219
+ /**
220
+ * 判断当前设备是否为移动设备。
221
+ *
222
+ * 本函数通过检查用户代理字符串和屏幕尺寸来判断设备是否为移动设备。
223
+ * 这对于需要根据设备类型进行不同布局或功能调整的应用非常有用。
224
+ *
225
+ * @returns {boolean} 如果设备是移动设备,则返回true;否则返回false。
226
+ */
227
+ export function isMobile() {
228
+ // 通过正则表达式匹配用户代理字符串,判断是否为移动设备
229
+ // 检查是否为移动设备
230
+ const isMobile = /Mobi|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
231
+ // 获取窗口的 innerWidth 和 innerHeight,用于判断屏幕尺寸
232
+ // 获取屏幕宽度和高度
233
+ const screenWidth = window.innerWidth;
234
+ const screenHeight = window.innerHeight;
235
+ // 判断屏幕宽度或高度是否小于等于800或600,用于进一步确认是否为移动设备
236
+ const isScreenMobile = screenWidth <= 800 || screenHeight <= 600;
237
+ // 如果是移动设备或屏幕尺寸符合移动设备特征,则返回true
238
+ return isMobile || isScreenMobile;
239
+ }
package/package.json CHANGED
@@ -50,9 +50,17 @@
50
50
  "types": "./lib/color.d.ts",
51
51
  "import": "./lib/color.js"
52
52
  },
53
+ "./logger": {
54
+ "types": "./lib/logger.d.ts",
55
+ "import": "./lib/logger.js"
56
+ },
57
+ "./array": {
58
+ "types": "./lib/array.d.ts",
59
+ "import": "./lib/array.js"
60
+ },
53
61
  "./*": "./lib/*"
54
62
  },
55
- "version": "0.8.1",
63
+ "version": "0.8.3",
56
64
  "type": "module",
57
65
  "repository": {
58
66
  "type": "git",