ph-utils 0.18.0 → 0.19.0
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/LICENSE +21 -0
- package/lib/dom.d.ts +114 -26
- package/lib/dom.js +21 -6
- package/lib/src/array.d.ts +79 -0
- package/lib/src/array.js +212 -0
- package/lib/src/color.d.ts +55 -0
- package/lib/src/color.js +294 -0
- package/lib/src/config.d.ts +33 -0
- package/lib/src/config.js +99 -0
- package/lib/src/copy.d.ts +11 -0
- package/lib/src/copy.js +101 -0
- package/lib/src/crypto.d.ts +74 -0
- package/lib/src/crypto.js +261 -0
- package/lib/src/crypto_node.d.ts +61 -0
- package/lib/src/crypto_node.js +133 -0
- package/lib/src/date.d.ts +66 -0
- package/lib/src/date.js +202 -0
- package/lib/src/dom.d.ts +265 -0
- package/lib/src/dom.js +635 -0
- package/lib/src/file.d.ts +29 -0
- package/lib/src/file.js +54 -0
- package/lib/src/id.d.ts +68 -0
- package/lib/src/id.js +170 -0
- package/lib/src/index.d.ts +154 -0
- package/lib/src/index.js +239 -0
- package/lib/src/logger.d.ts +62 -0
- package/lib/src/logger.js +122 -0
- package/lib/src/server.d.ts +33 -0
- package/lib/src/server.js +65 -0
- package/lib/src/storage.d.ts +51 -0
- package/lib/src/storage.js +73 -0
- package/lib/src/theme.d.ts +44 -0
- package/lib/src/theme.js +156 -0
- package/lib/src/validator.d.ts +71 -0
- package/lib/src/validator.js +238 -0
- package/lib/src/web.d.ts +30 -0
- package/lib/src/web.js +100 -0
- package/package.json +2 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Tenny
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/lib/dom.d.ts
CHANGED
|
@@ -7,9 +7,21 @@
|
|
|
7
7
|
* 根据选择器获取节点
|
|
8
8
|
* @param {string} selector 选择器
|
|
9
9
|
*/
|
|
10
|
-
type FormatStyleParam =
|
|
11
|
-
|
|
10
|
+
type FormatStyleParam =
|
|
11
|
+
| (string | undefined | null)[]
|
|
12
|
+
| Record<string, boolean | string | undefined | null>
|
|
13
|
+
| string;
|
|
14
|
+
type FormatClassParam =
|
|
15
|
+
| (string | undefined | null | boolean)[]
|
|
16
|
+
| Record<string, boolean | string | undefined | null | boolean>
|
|
17
|
+
| string;
|
|
12
18
|
type DocumentContext = HTMLElement | ShadowRoot | Document;
|
|
19
|
+
type NodeChildren =
|
|
20
|
+
| HTMLElement
|
|
21
|
+
| DocumentFragment
|
|
22
|
+
| HTMLElement[]
|
|
23
|
+
| NodeListOf<HTMLElement>
|
|
24
|
+
| HTMLCollection;
|
|
13
25
|
export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement[];
|
|
14
26
|
/**
|
|
15
27
|
* 根据选择器获取 DOM 元素。
|
|
@@ -22,33 +34,44 @@ export declare function $(selector: string | HTMLElement, dom?: DocumentContext)
|
|
|
22
34
|
* 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
|
|
23
35
|
* @param tag - 元素标签名或 HTML 字符串。
|
|
24
36
|
* @param option - 元素的属性、样式、文本内容等配置。所有不是指定的属性,都会通过 setAttribute 设置
|
|
25
|
-
* @param
|
|
37
|
+
* @param children - 子节点
|
|
26
38
|
* @returns 创建的 HTML 元素。
|
|
27
39
|
*/
|
|
28
|
-
export declare function create(
|
|
40
|
+
export declare function create(
|
|
41
|
+
tag: string,
|
|
42
|
+
option?: {
|
|
29
43
|
class?: FormatClassParam;
|
|
30
44
|
style?: FormatStyleParam;
|
|
31
45
|
textContent?: string;
|
|
32
46
|
innerHTML?: string;
|
|
33
47
|
outerHTML?: string;
|
|
34
48
|
[index: string]: any;
|
|
35
|
-
},
|
|
49
|
+
},
|
|
50
|
+
children?: NodeChildren,
|
|
51
|
+
): HTMLElement;
|
|
36
52
|
/** 创建节点 */
|
|
37
|
-
export declare function $$(
|
|
53
|
+
export declare function $$(
|
|
54
|
+
tag: string,
|
|
55
|
+
option?: {
|
|
38
56
|
class?: FormatClassParam;
|
|
39
57
|
style?: FormatStyleParam;
|
|
40
58
|
textContent?: string;
|
|
41
59
|
innerHTML?: string;
|
|
42
60
|
outerHTML?: string;
|
|
43
61
|
[index: string]: any;
|
|
44
|
-
},
|
|
62
|
+
},
|
|
63
|
+
children?: NodeChildren,
|
|
64
|
+
): HTMLElement;
|
|
45
65
|
/**
|
|
46
66
|
* 根据选择器获取匹配的第一个 DOM 元素。
|
|
47
67
|
* @param selector - 选择器字符串或直接的 HTMLElement。
|
|
48
68
|
* @param dom - 可选的父级 DOM 元素,默认为当前文档。
|
|
49
69
|
* @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 null。
|
|
50
70
|
*/
|
|
51
|
-
export declare function $one(
|
|
71
|
+
export declare function $one(
|
|
72
|
+
selector: string | HTMLElement,
|
|
73
|
+
dom?: DocumentContext,
|
|
74
|
+
): HTMLElement | null;
|
|
52
75
|
/**
|
|
53
76
|
* 为节点添加 class
|
|
54
77
|
* @param {HTMLElement} elem 待添加 class 的节点
|
|
@@ -86,7 +109,11 @@ export declare function toggleClass(el: HTMLElement, clazz: string): void;
|
|
|
86
109
|
* @param newClazz - 新的类名,用于替换旧的类
|
|
87
110
|
* @returns void
|
|
88
111
|
*/
|
|
89
|
-
export declare function replaceClass(
|
|
112
|
+
export declare function replaceClass(
|
|
113
|
+
el: HTMLElement,
|
|
114
|
+
oldClazz: string | number,
|
|
115
|
+
newClazz: string,
|
|
116
|
+
): void;
|
|
90
117
|
type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => void);
|
|
91
118
|
/**
|
|
92
119
|
* 为节点添加事件处理
|
|
@@ -95,16 +122,40 @@ type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => vo
|
|
|
95
122
|
* @param {function} fn 事件处理函数
|
|
96
123
|
* @param {boolean} option 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
|
|
97
124
|
*/
|
|
98
|
-
export declare function on(
|
|
99
|
-
|
|
100
|
-
|
|
125
|
+
export declare function on(
|
|
126
|
+
element:
|
|
127
|
+
| HTMLElement
|
|
128
|
+
| ShadowRoot
|
|
129
|
+
| Document
|
|
130
|
+
| HTMLCollection
|
|
131
|
+
| NodeListOf<HTMLElement>
|
|
132
|
+
| HTMLElement[],
|
|
133
|
+
listener: string,
|
|
134
|
+
fn: EventHandler,
|
|
135
|
+
option?:
|
|
136
|
+
| boolean
|
|
137
|
+
| (AddEventListenerOptions & {
|
|
138
|
+
eventFlag?: string;
|
|
139
|
+
}),
|
|
140
|
+
): void;
|
|
101
141
|
/**
|
|
102
142
|
* 移除指定元素的事件监听器。
|
|
103
143
|
* @param el - 要移除监听器的 HTML 元素。
|
|
104
144
|
* @param listener - 事件名称。
|
|
105
145
|
* @param fn - 要移除的事件监听器函数。
|
|
106
146
|
*/
|
|
107
|
-
export declare function off(
|
|
147
|
+
export declare function off(
|
|
148
|
+
el:
|
|
149
|
+
| HTMLElement
|
|
150
|
+
| ShadowRoot
|
|
151
|
+
| Document
|
|
152
|
+
| HTMLCollection
|
|
153
|
+
| NodeListOf<HTMLElement>
|
|
154
|
+
| HTMLElement[],
|
|
155
|
+
listener: string,
|
|
156
|
+
fn: EventHandler,
|
|
157
|
+
option?: boolean | EventListenerOptions,
|
|
158
|
+
): void;
|
|
108
159
|
/**
|
|
109
160
|
* 判断事件是否应该继续传递。
|
|
110
161
|
* 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
|
|
@@ -117,7 +168,11 @@ export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollec
|
|
|
117
168
|
* @param endRoot - 可选,如果传递该参数,则表示停止遍历的节点,如果未传递,则表示遍历到文档根节点为止
|
|
118
169
|
* @returns 包含三个元素的数组:[是否继续传递事件, 属性值, 当前检查的DOM节点]
|
|
119
170
|
*/
|
|
120
|
-
export declare function shouldEventNext(
|
|
171
|
+
export declare function shouldEventNext(
|
|
172
|
+
e: Event,
|
|
173
|
+
eventFlag: string,
|
|
174
|
+
endRoot?: HTMLElement | ShadowRoot,
|
|
175
|
+
): [boolean, string, HTMLElement];
|
|
121
176
|
/**
|
|
122
177
|
* 设置或获取节点的 innerHTML 属性
|
|
123
178
|
* @param element
|
|
@@ -133,7 +188,10 @@ export declare function html(element: HTMLElement, htmlstr?: string): string | u
|
|
|
133
188
|
*/
|
|
134
189
|
export declare function text(element: HTMLElement, textstr?: string): string | undefined | null;
|
|
135
190
|
export declare function iterate<T>(elems: T[], fn: (el: T, index: number) => any): void;
|
|
136
|
-
export declare function iterate(
|
|
191
|
+
export declare function iterate(
|
|
192
|
+
elems: NodeList | HTMLCollection | NodeListOf<HTMLElement>,
|
|
193
|
+
fn: (el: HTMLElement, index: number) => any,
|
|
194
|
+
): void;
|
|
137
195
|
/**
|
|
138
196
|
* 设置或获取节点 data-* 属性
|
|
139
197
|
* @param elem
|
|
@@ -141,7 +199,11 @@ export declare function iterate(elems: NodeList | HTMLCollection | NodeListOf<HT
|
|
|
141
199
|
* @param value 如果传递该值表示获取;否则表示设置
|
|
142
200
|
* @returns
|
|
143
201
|
*/
|
|
144
|
-
export declare function attr(
|
|
202
|
+
export declare function attr(
|
|
203
|
+
elem: HTMLElement,
|
|
204
|
+
key: string,
|
|
205
|
+
value?: string,
|
|
206
|
+
): string | null | undefined;
|
|
145
207
|
/**
|
|
146
208
|
* 获取属性值
|
|
147
209
|
* @param key 属性名称
|
|
@@ -155,7 +217,11 @@ export declare function getAttr(el: HTMLElement, key: string): string;
|
|
|
155
217
|
export declare function getAttr(el: HTMLElement, filepath: string, defaultValue: string): string;
|
|
156
218
|
export declare function getAttr(el: HTMLElement, filepath: string, defaultValue: number): number;
|
|
157
219
|
export declare function getAttr(el: HTMLElement, filepath: string, defaultValue: boolean): boolean;
|
|
158
|
-
export declare function getAttr<T extends Record<string, string | number | boolean>>(
|
|
220
|
+
export declare function getAttr<T extends Record<string, string | number | boolean>>(
|
|
221
|
+
el: HTMLElement,
|
|
222
|
+
filepath: string,
|
|
223
|
+
defaultValue: T,
|
|
224
|
+
): T;
|
|
159
225
|
/**
|
|
160
226
|
* 获取指定节点的父节点
|
|
161
227
|
* @param el
|
|
@@ -168,9 +234,12 @@ export declare function parent(el: HTMLElement): HTMLElement;
|
|
|
168
234
|
* @param parent - 添加临时节点的父节点, 如果传递 null, 则通过修改原始样式方式计算,默认为: body.
|
|
169
235
|
* @returns The DOMRect of the element.
|
|
170
236
|
*/
|
|
171
|
-
export declare function queryHideNodeSize(
|
|
172
|
-
|
|
173
|
-
|
|
237
|
+
export declare function queryHideNodeSize(
|
|
238
|
+
hideNode: string | HTMLElement,
|
|
239
|
+
parent?: null | HTMLElement,
|
|
240
|
+
): {
|
|
241
|
+
width: number;
|
|
242
|
+
height: number;
|
|
174
243
|
};
|
|
175
244
|
/**
|
|
176
245
|
* 判断元素是否在父元素的可视区域内。
|
|
@@ -180,7 +249,11 @@ export declare function queryHideNodeSize(hideNode: string | HTMLElement, parent
|
|
|
180
249
|
* @param direction 检查的方向,默认为"horizontal"
|
|
181
250
|
* @returns 如果元素在父元素的可视区域内,则返回true;否则返回false。
|
|
182
251
|
*/
|
|
183
|
-
export declare function isVisible(
|
|
252
|
+
export declare function isVisible(
|
|
253
|
+
el: HTMLElement,
|
|
254
|
+
parent?: HTMLElement | null | undefined,
|
|
255
|
+
direction?: string,
|
|
256
|
+
): boolean;
|
|
184
257
|
/**
|
|
185
258
|
* 判断当前设备是否为移动设备。
|
|
186
259
|
*
|
|
@@ -197,7 +270,12 @@ export declare function isMobile(): boolean;
|
|
|
197
270
|
* @param classObj - 类名对象或数组
|
|
198
271
|
* @returns 格式化后的类名字符串
|
|
199
272
|
*/
|
|
200
|
-
export declare function formatClass(
|
|
273
|
+
export declare function formatClass(
|
|
274
|
+
classObj:
|
|
275
|
+
| (boolean | string | undefined | null)[]
|
|
276
|
+
| Record<string, boolean | string | undefined | null>
|
|
277
|
+
| string,
|
|
278
|
+
): string;
|
|
201
279
|
/**
|
|
202
280
|
* 将样式对象格式化为 CSS 样式字符串。
|
|
203
281
|
* @param styleObj - 样式对象,可以是字符串数组或键值对对象。
|
|
@@ -233,7 +311,12 @@ export declare function formatStyle(styleObj: FormatStyleParam): string;
|
|
|
233
311
|
* @example <caption>动画结束后移除节点</caption>
|
|
234
312
|
* transition($el, [["opacity", "0", "0.3s"]], "leave", () => { $el.remove(); });
|
|
235
313
|
*/
|
|
236
|
-
export declare function transition(
|
|
314
|
+
export declare function transition(
|
|
315
|
+
el: HTMLElement,
|
|
316
|
+
nameOrProperties: string | [string, string, string][],
|
|
317
|
+
dir?: "leave" | "enter",
|
|
318
|
+
finish?: () => void,
|
|
319
|
+
): void;
|
|
237
320
|
/**
|
|
238
321
|
* 计算光标位置
|
|
239
322
|
*
|
|
@@ -257,8 +340,13 @@ export declare function transition(el: HTMLElement, nameOrProperties: string | [
|
|
|
257
340
|
* // 替换字符时,保持位置
|
|
258
341
|
* calcuteCursorPosition(2, 2, "abc", "axc"); // 返回 { start: 2, end: 2 }
|
|
259
342
|
*/
|
|
260
|
-
export declare function calcuteCursorPosition(
|
|
261
|
-
|
|
262
|
-
|
|
343
|
+
export declare function calcuteCursorPosition(
|
|
344
|
+
start: number,
|
|
345
|
+
end: number,
|
|
346
|
+
oldValue: string,
|
|
347
|
+
newValue: string,
|
|
348
|
+
): {
|
|
349
|
+
start: number;
|
|
350
|
+
end: number;
|
|
263
351
|
};
|
|
264
352
|
export {};
|
package/lib/dom.js
CHANGED
|
@@ -22,7 +22,7 @@ export function $(selector, dom) {
|
|
|
22
22
|
* @param ctx - 元素的父级文档上下文。
|
|
23
23
|
* @returns 创建的 HTML 元素。
|
|
24
24
|
*/
|
|
25
|
-
export function create(tag, option = {},
|
|
25
|
+
export function create(tag, option = {}, children) {
|
|
26
26
|
let $el;
|
|
27
27
|
if (tag.startsWith("<") && tag.endsWith(">")) {
|
|
28
28
|
const parser = new DOMParser();
|
|
@@ -53,7 +53,7 @@ export function create(tag, option = {}, ctx) {
|
|
|
53
53
|
$el.outerHTML = value;
|
|
54
54
|
}
|
|
55
55
|
else {
|
|
56
|
-
if (value === true) {
|
|
56
|
+
if (typeof value === "boolean" && value === true) {
|
|
57
57
|
$el.setAttribute(key, "");
|
|
58
58
|
}
|
|
59
59
|
else {
|
|
@@ -63,15 +63,30 @@ export function create(tag, option = {}, ctx) {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
-
if (
|
|
67
|
-
|
|
66
|
+
if (children) {
|
|
67
|
+
if (children instanceof HTMLElement || children instanceof DocumentFragment) {
|
|
68
|
+
$el.appendChild(children);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
let len = children.length;
|
|
72
|
+
const fragment = document.createDocumentFragment();
|
|
73
|
+
for (let i = 0; i < len; i++) {
|
|
74
|
+
const child = children[i];
|
|
75
|
+
if (child instanceof HTMLElement) {
|
|
76
|
+
fragment.appendChild(child);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
if (len > 0) {
|
|
80
|
+
$el.appendChild(fragment);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
68
83
|
}
|
|
69
84
|
}
|
|
70
85
|
return $el;
|
|
71
86
|
}
|
|
72
87
|
/** 创建节点 */
|
|
73
|
-
export function $$(tag, option = {},
|
|
74
|
-
return create(tag, option,
|
|
88
|
+
export function $$(tag, option = {}, children) {
|
|
89
|
+
return create(tag, option, children);
|
|
75
90
|
}
|
|
76
91
|
/**
|
|
77
92
|
* 根据选择器获取匹配的第一个 DOM 元素。
|
|
@@ -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>): boolean;
|
|
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>): boolean;
|
|
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>): boolean;
|
package/lib/src/array.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
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(undefined, { numeric: true });
|
|
10
|
+
return arr.sort((a, b) => {
|
|
11
|
+
let aValue = a;
|
|
12
|
+
let bValue = b;
|
|
13
|
+
if (typeof a === "object" && orderKey != null) {
|
|
14
|
+
// @ts-ignore
|
|
15
|
+
aValue = a[orderKey];
|
|
16
|
+
// @ts-ignore
|
|
17
|
+
bValue = b[orderKey];
|
|
18
|
+
}
|
|
19
|
+
let aGtb = -1;
|
|
20
|
+
if (typeof aValue === "string") {
|
|
21
|
+
aGtb = collator.compare(aValue, bValue);
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
aGtb = aValue >= bValue ? 1 : -1;
|
|
25
|
+
}
|
|
26
|
+
return order === "asc" ? aGtb : -aGtb;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* 返回所有集合交集的元素的新集合
|
|
31
|
+
*/ export function intersection(...arrs) {
|
|
32
|
+
return arrs.reduce((acc, cur) => {
|
|
33
|
+
if (cur instanceof Set) {
|
|
34
|
+
//@ts-ignore
|
|
35
|
+
if (cur.intersection != null) {
|
|
36
|
+
//@ts-ignore
|
|
37
|
+
return acc.intersection(cur);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const res = new Set();
|
|
41
|
+
for (const value of acc) {
|
|
42
|
+
if (cur.has(value)) {
|
|
43
|
+
res.add(value);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return res;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return acc.filter((x) => cur.includes(x));
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 返回一个包含第一个集合中的元素但不包含后续给定集合中元素的新集合
|
|
56
|
+
* @param arrs 新集合
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
export function difference(...arrs) {
|
|
60
|
+
return arrs.reduce((acc, cur) => {
|
|
61
|
+
if (acc instanceof Set) {
|
|
62
|
+
//@ts-ignore
|
|
63
|
+
if (acc.difference != null) {
|
|
64
|
+
//@ts-ignore
|
|
65
|
+
return acc.difference(cur);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const res = new Set();
|
|
69
|
+
for (const value of acc) {
|
|
70
|
+
if (!cur.has(value)) {
|
|
71
|
+
res.add(value);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return res;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
const res = [];
|
|
79
|
+
for (const value of acc) {
|
|
80
|
+
if (!cur.includes(value)) {
|
|
81
|
+
res.push(value);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return res;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
export function union(...arrs) {
|
|
89
|
+
return arrs.reduce((acc, cur) => {
|
|
90
|
+
if (cur instanceof Set) {
|
|
91
|
+
//@ts-ignore
|
|
92
|
+
if (cur.union != null) {
|
|
93
|
+
//@ts-ignore
|
|
94
|
+
return acc.union(cur);
|
|
95
|
+
}
|
|
96
|
+
return new Set([...acc, ...cur]);
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
return [...acc, ...cur];
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* 对称差: 返回一个包含此集合或给定集合中的元素的新集合,但不包含同时存在于这两个集合中的元素。
|
|
105
|
+
* @param arrs
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
108
|
+
export function symmetricDifference(...arrs) {
|
|
109
|
+
return arrs.reduce((acc, cur) => {
|
|
110
|
+
if (cur instanceof Set) {
|
|
111
|
+
//@ts-ignore
|
|
112
|
+
if (cur.symmetricDifference != null) {
|
|
113
|
+
//@ts-ignore
|
|
114
|
+
return acc.symmetricDifference(cur);
|
|
115
|
+
}
|
|
116
|
+
const res = new Set();
|
|
117
|
+
for (const item of acc) {
|
|
118
|
+
if (!cur.has(item)) {
|
|
119
|
+
res.add(item);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
for (const item of cur) {
|
|
123
|
+
if (!acc.has(item)) {
|
|
124
|
+
res.add(item);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return res;
|
|
128
|
+
}
|
|
129
|
+
const res = [];
|
|
130
|
+
for (const item of acc) {
|
|
131
|
+
if (!cur.includes(item)) {
|
|
132
|
+
res.push(item);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const item of cur) {
|
|
136
|
+
if (!acc.includes(item)) {
|
|
137
|
+
res.push(item);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return res;
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* 返回一个布尔值,指示此集合中的所有元素是否都在给定的集合中。
|
|
145
|
+
* @param a1
|
|
146
|
+
* @param a2
|
|
147
|
+
* @returns
|
|
148
|
+
*/
|
|
149
|
+
export function isSubsetOf(a1, a2) {
|
|
150
|
+
//@ts-ignore
|
|
151
|
+
if (a1 instanceof Set && a1.isSubsetOf != null) {
|
|
152
|
+
//@ts-ignore
|
|
153
|
+
return a1.isSubsetOf(a2);
|
|
154
|
+
}
|
|
155
|
+
let is = true;
|
|
156
|
+
for (const item of a1) {
|
|
157
|
+
if (a2 instanceof Set) {
|
|
158
|
+
if (!a2.has(item)) {
|
|
159
|
+
is = false;
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
if (!a2.includes(item)) {
|
|
165
|
+
is = false;
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return is;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 返回一个布尔值,指示给定集合中的所有元素是否都在此集合中。
|
|
174
|
+
* @param arr1
|
|
175
|
+
* @param arr2
|
|
176
|
+
* @returns
|
|
177
|
+
*/
|
|
178
|
+
export function isSupersetOf(arr1, arr2) {
|
|
179
|
+
return isSubsetOf(arr2, arr1);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 返回一个布尔值,指示此集合是否与给定集合没有公共元素。
|
|
183
|
+
*
|
|
184
|
+
* 判断两个集合是否有交集, 也可以通过 intersection(arr1, arr2).length 判断
|
|
185
|
+
*
|
|
186
|
+
* @param arr1
|
|
187
|
+
* @param arr2
|
|
188
|
+
* @returns
|
|
189
|
+
*/
|
|
190
|
+
export function isDisjointFrom(arr1, arr2) {
|
|
191
|
+
//@ts-ignore
|
|
192
|
+
if (arr1 instanceof Set && arr1.isDisjointFrom != null) {
|
|
193
|
+
//@ts-ignore
|
|
194
|
+
return arr1.isDisjointFrom(arr2);
|
|
195
|
+
}
|
|
196
|
+
let is = true;
|
|
197
|
+
for (const item of arr1) {
|
|
198
|
+
if (arr2 instanceof Set) {
|
|
199
|
+
if (arr2.has(item)) {
|
|
200
|
+
is = false;
|
|
201
|
+
break;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
if (arr2.includes(item)) {
|
|
206
|
+
is = false;
|
|
207
|
+
break;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return is;
|
|
212
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
type RGBColorObject = {
|
|
2
|
+
r: number;
|
|
3
|
+
g: number;
|
|
4
|
+
b: number;
|
|
5
|
+
a?: number;
|
|
6
|
+
};
|
|
7
|
+
type HSVColorObject = {
|
|
8
|
+
h: number;
|
|
9
|
+
s: number;
|
|
10
|
+
v: number;
|
|
11
|
+
};
|
|
12
|
+
type ColorType = string | RGBColorObject | HSVColorObject;
|
|
13
|
+
/**
|
|
14
|
+
* 将输入的颜色值转换为RGB对象格式。
|
|
15
|
+
*
|
|
16
|
+
* @param color 可以是字符串, 也可以是一个 HSV 对象[一个包含 h、s、v 属性的对象]
|
|
17
|
+
* @returns 返回一个包含r、g、b和a(透明度)属性的RGB对象。
|
|
18
|
+
* @throws 如果输入的字符串不是有效的颜色表示,则抛出错误。
|
|
19
|
+
*/
|
|
20
|
+
export declare function toRgb(color: ColorType): RGBColorObject;
|
|
21
|
+
/**
|
|
22
|
+
* 将颜色转换为HSV颜色模型。
|
|
23
|
+
*
|
|
24
|
+
* @param color - 字符串或者RGB对象
|
|
25
|
+
* @returns 返回一个包含h、s、v属性的对象,代表HSV颜色值,其中h是色相(取值范围0到360),s是饱和度(取值范围0到1),v是明度(取值范围0到1)。
|
|
26
|
+
*/
|
|
27
|
+
export declare function toHsv(color: ColorType): HSVColorObject;
|
|
28
|
+
/**
|
|
29
|
+
* 将RGB颜色对象转换为十六进制颜色字符串。
|
|
30
|
+
* @param rgb - 包含红色(r), 绿色(g), 蓝色(b)成分的对象。
|
|
31
|
+
* @returns 返回一个表示RGB颜色的十六进制字符串,例如"#FF0000"。
|
|
32
|
+
*/
|
|
33
|
+
export declare function rgbToHex(rgb: RGBColorObject): string;
|
|
34
|
+
/**
|
|
35
|
+
* 将颜色转换为 16 进制字符串
|
|
36
|
+
* @param color - 颜色, 可以 rgb(0,0,0),rgba(0,0,0,0)字符串, 也可以是 rgb、hsv对象
|
|
37
|
+
* @returns 返回颜色的十六进制字符串,例如"#FF0000"
|
|
38
|
+
*/
|
|
39
|
+
export declare function toHex(color: ColorType): string;
|
|
40
|
+
/**
|
|
41
|
+
* 调整给定颜色深[darken]浅[lighten]
|
|
42
|
+
* @param color - 输入的颜色,可以是任意颜色表示方式
|
|
43
|
+
* @param level - 调整深浅级别, 可以是小数。默认: 1
|
|
44
|
+
* @param light - 控制调整的方向。如果为true,[lighten] 变浅,如果为false,[darken] 变深。默认: true
|
|
45
|
+
*
|
|
46
|
+
* #### 1. 颜色变浅
|
|
47
|
+
*
|
|
48
|
+
* ```
|
|
49
|
+
* adjust('#4998f4', 3, true)
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @returns 返回调整后颜色的十六进制字符串表示。
|
|
53
|
+
*/
|
|
54
|
+
export declare function adjust(color: ColorType, level?: number, light?: boolean): string;
|
|
55
|
+
export {};
|