ph-utils 0.19.0 → 0.20.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/lib/dom.d.ts CHANGED
@@ -7,21 +7,10 @@
7
7
  * 根据选择器获取节点
8
8
  * @param {string} selector 选择器
9
9
  */
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;
10
+ type FormatStyleParam = (string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string;
11
+ type FormatClassParam = (string | undefined | null | boolean)[] | Record<string, boolean | string | undefined | null | boolean> | string;
18
12
  type DocumentContext = HTMLElement | ShadowRoot | Document;
19
- type NodeChildren =
20
- | HTMLElement
21
- | DocumentFragment
22
- | HTMLElement[]
23
- | NodeListOf<HTMLElement>
24
- | HTMLCollection;
13
+ type NodeChildren = HTMLElement | DocumentFragment | HTMLElement[] | NodeListOf<HTMLElement> | HTMLCollection;
25
14
  export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement[];
26
15
  /**
27
16
  * 根据选择器获取 DOM 元素。
@@ -37,41 +26,30 @@ export declare function $(selector: string | HTMLElement, dom?: DocumentContext)
37
26
  * @param children - 子节点
38
27
  * @returns 创建的 HTML 元素。
39
28
  */
40
- export declare function create(
41
- tag: string,
42
- option?: {
29
+ export declare function create(tag: string, option?: {
43
30
  class?: FormatClassParam;
44
31
  style?: FormatStyleParam;
45
32
  textContent?: string;
46
33
  innerHTML?: string;
47
34
  outerHTML?: string;
48
35
  [index: string]: any;
49
- },
50
- children?: NodeChildren,
51
- ): HTMLElement;
36
+ }, children?: NodeChildren): HTMLElement;
52
37
  /** 创建节点 */
53
- export declare function $$(
54
- tag: string,
55
- option?: {
38
+ export declare function $$(tag: string, option?: {
56
39
  class?: FormatClassParam;
57
40
  style?: FormatStyleParam;
58
41
  textContent?: string;
59
42
  innerHTML?: string;
60
43
  outerHTML?: string;
61
44
  [index: string]: any;
62
- },
63
- children?: NodeChildren,
64
- ): HTMLElement;
45
+ }, children?: NodeChildren): HTMLElement;
65
46
  /**
66
47
  * 根据选择器获取匹配的第一个 DOM 元素。
67
48
  * @param selector - 选择器字符串或直接的 HTMLElement。
68
49
  * @param dom - 可选的父级 DOM 元素,默认为当前文档。
69
50
  * @returns 返回匹配的第一个 HTMLElement,如果没有找到则返回 null。
70
51
  */
71
- export declare function $one(
72
- selector: string | HTMLElement,
73
- dom?: DocumentContext,
74
- ): HTMLElement | null;
52
+ export declare function $one(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement | null;
75
53
  /**
76
54
  * 为节点添加 class
77
55
  * @param {HTMLElement} elem 待添加 class 的节点
@@ -109,11 +87,7 @@ export declare function toggleClass(el: HTMLElement, clazz: string): void;
109
87
  * @param newClazz - 新的类名,用于替换旧的类
110
88
  * @returns void
111
89
  */
112
- export declare function replaceClass(
113
- el: HTMLElement,
114
- oldClazz: string | number,
115
- newClazz: string,
116
- ): void;
90
+ export declare function replaceClass(el: HTMLElement, oldClazz: string | number, newClazz: string): void;
117
91
  type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => void);
118
92
  /**
119
93
  * 为节点添加事件处理
@@ -122,40 +96,16 @@ type EventHandler = EventListenerOrEventListenerObject | ((e: CustomEvent) => vo
122
96
  * @param {function} fn 事件处理函数
123
97
  * @param {boolean} option 是否是只运行一次的处理函数或者配置,其中 eventFlag 为 string,如果配置该项,则表明为委托事件
124
98
  */
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;
99
+ export declare function on(element: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | (AddEventListenerOptions & {
100
+ eventFlag?: string;
101
+ })): void;
141
102
  /**
142
103
  * 移除指定元素的事件监听器。
143
104
  * @param el - 要移除监听器的 HTML 元素。
144
105
  * @param listener - 事件名称。
145
106
  * @param fn - 要移除的事件监听器函数。
146
107
  */
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
+ export declare function off(el: HTMLElement | ShadowRoot | Document | HTMLCollection | NodeListOf<HTMLElement> | HTMLElement[], listener: string, fn: EventHandler, option?: boolean | EventListenerOptions): void;
159
109
  /**
160
110
  * 判断事件是否应该继续传递。
161
111
  * 从事件目标开始向上遍历DOM树,检查每个节点上是否存在指定的属性。
@@ -168,11 +118,7 @@ export declare function off(
168
118
  * @param endRoot - 可选,如果传递该参数,则表示停止遍历的节点,如果未传递,则表示遍历到文档根节点为止
169
119
  * @returns 包含三个元素的数组:[是否继续传递事件, 属性值, 当前检查的DOM节点]
170
120
  */
171
- export declare function shouldEventNext(
172
- e: Event,
173
- eventFlag: string,
174
- endRoot?: HTMLElement | ShadowRoot,
175
- ): [boolean, string, HTMLElement];
121
+ export declare function shouldEventNext(e: Event, eventFlag: string, endRoot?: HTMLElement | ShadowRoot): [boolean, string, HTMLElement];
176
122
  /**
177
123
  * 设置或获取节点的 innerHTML 属性
178
124
  * @param element
@@ -188,10 +134,7 @@ export declare function html(element: HTMLElement, htmlstr?: string): string | u
188
134
  */
189
135
  export declare function text(element: HTMLElement, textstr?: string): string | undefined | null;
190
136
  export declare function iterate<T>(elems: T[], fn: (el: T, index: number) => any): void;
191
- export declare function iterate(
192
- elems: NodeList | HTMLCollection | NodeListOf<HTMLElement>,
193
- fn: (el: HTMLElement, index: number) => any,
194
- ): void;
137
+ export declare function iterate(elems: NodeList | HTMLCollection | NodeListOf<HTMLElement>, fn: (el: HTMLElement, index: number) => any): void;
195
138
  /**
196
139
  * 设置或获取节点 data-* 属性
197
140
  * @param elem
@@ -199,11 +142,7 @@ export declare function iterate(
199
142
  * @param value 如果传递该值表示获取;否则表示设置
200
143
  * @returns
201
144
  */
202
- export declare function attr(
203
- elem: HTMLElement,
204
- key: string,
205
- value?: string,
206
- ): string | null | undefined;
145
+ export declare function attr(elem: HTMLElement, key: string, value?: string): string | null | undefined;
207
146
  /**
208
147
  * 获取属性值
209
148
  * @param key 属性名称
@@ -217,11 +156,7 @@ export declare function getAttr(el: HTMLElement, key: string): string;
217
156
  export declare function getAttr(el: HTMLElement, filepath: string, defaultValue: string): string;
218
157
  export declare function getAttr(el: HTMLElement, filepath: string, defaultValue: number): number;
219
158
  export declare function getAttr(el: HTMLElement, filepath: string, defaultValue: boolean): 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
+ export declare function getAttr<T extends Record<string, string | number | boolean>>(el: HTMLElement, filepath: string, defaultValue: T): T;
225
160
  /**
226
161
  * 获取指定节点的父节点
227
162
  * @param el
@@ -234,12 +169,9 @@ export declare function parent(el: HTMLElement): HTMLElement;
234
169
  * @param parent - 添加临时节点的父节点, 如果传递 null, 则通过修改原始样式方式计算,默认为: body.
235
170
  * @returns The DOMRect of the element.
236
171
  */
237
- export declare function queryHideNodeSize(
238
- hideNode: string | HTMLElement,
239
- parent?: null | HTMLElement,
240
- ): {
241
- width: number;
242
- height: number;
172
+ export declare function queryHideNodeSize(hideNode: string | HTMLElement, parent?: null | HTMLElement): {
173
+ width: number;
174
+ height: number;
243
175
  };
244
176
  /**
245
177
  * 判断元素是否在父元素的可视区域内。
@@ -249,11 +181,7 @@ export declare function queryHideNodeSize(
249
181
  * @param direction 检查的方向,默认为"horizontal"
250
182
  * @returns 如果元素在父元素的可视区域内,则返回true;否则返回false。
251
183
  */
252
- export declare function isVisible(
253
- el: HTMLElement,
254
- parent?: HTMLElement | null | undefined,
255
- direction?: string,
256
- ): boolean;
184
+ export declare function isVisible(el: HTMLElement, parent?: HTMLElement | null | undefined, direction?: string): boolean;
257
185
  /**
258
186
  * 判断当前设备是否为移动设备。
259
187
  *
@@ -270,12 +198,7 @@ export declare function isMobile(): boolean;
270
198
  * @param classObj - 类名对象或数组
271
199
  * @returns 格式化后的类名字符串
272
200
  */
273
- export declare function formatClass(
274
- classObj:
275
- | (boolean | string | undefined | null)[]
276
- | Record<string, boolean | string | undefined | null>
277
- | string,
278
- ): string;
201
+ export declare function formatClass(classObj: (boolean | string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string): string;
279
202
  /**
280
203
  * 将样式对象格式化为 CSS 样式字符串。
281
204
  * @param styleObj - 样式对象,可以是字符串数组或键值对对象。
@@ -311,12 +234,7 @@ export declare function formatStyle(styleObj: FormatStyleParam): string;
311
234
  * @example <caption>动画结束后移除节点</caption>
312
235
  * transition($el, [["opacity", "0", "0.3s"]], "leave", () => { $el.remove(); });
313
236
  */
314
- export declare function transition(
315
- el: HTMLElement,
316
- nameOrProperties: string | [string, string, string][],
317
- dir?: "leave" | "enter",
318
- finish?: () => void,
319
- ): void;
237
+ export declare function transition(el: HTMLElement, nameOrProperties: string | [string, string, string][], dir?: "leave" | "enter", finish?: () => void): void;
320
238
  /**
321
239
  * 计算光标位置
322
240
  *
@@ -340,13 +258,8 @@ export declare function transition(
340
258
  * // 替换字符时,保持位置
341
259
  * calcuteCursorPosition(2, 2, "abc", "axc"); // 返回 { start: 2, end: 2 }
342
260
  */
343
- export declare function calcuteCursorPosition(
344
- start: number,
345
- end: number,
346
- oldValue: string,
347
- newValue: string,
348
- ): {
349
- start: number;
350
- end: number;
261
+ export declare function calcuteCursorPosition(start: number, end: number, oldValue: string, newValue: string): {
262
+ start: number;
263
+ end: number;
351
264
  };
352
265
  export {};
package/lib/dom.js CHANGED
@@ -19,7 +19,7 @@ export function $(selector, dom) {
19
19
  * 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
20
20
  * @param tag - 元素标签名或 HTML 字符串。
21
21
  * @param option - 元素的属性、样式、文本内容等配置。所有不是指定的属性,都会通过 setAttribute 设置
22
- * @param ctx - 元素的父级文档上下文。
22
+ * @param children - 子节点
23
23
  * @returns 创建的 HTML 元素。
24
24
  */
25
25
  export function create(tag, option = {}, children) {
@@ -24,9 +24,7 @@ export interface SchemaType {
24
24
  * 数据验证器
25
25
  */
26
26
  declare class Validator {
27
- rules: {
28
- [index: string]: RuleItem[];
29
- };
27
+ rules: Record<string, RuleItem[]>;
30
28
  /**
31
29
  * 构造数据验证转换器
32
30
  *
@@ -47,6 +45,8 @@ declare class Validator {
47
45
  constructor(schemas: SchemaType[]);
48
46
  addSchemas(schemas: SchemaType[]): void;
49
47
  addSchema(schema: SchemaType): void;
48
+ setSchema(schema: SchemaType): void;
49
+ setSchemas(schemas: SchemaType[]): void;
50
50
  /**
51
51
  * 进行数据验证
52
52
  * @param data 待验证的数据
package/lib/validator.js CHANGED
@@ -61,17 +61,28 @@ class Validator {
61
61
  * validator.validateKey().then(res => {})
62
62
  */
63
63
  constructor(schemas) {
64
- this.rules = {};
65
- this.addSchemas(schemas);
64
+ this.setSchemas(schemas);
66
65
  }
67
66
  addSchemas(schemas) {
68
67
  for (let schema of schemas) {
69
- this.rules[schema.key] = this._parseSchemaRules(schema);
68
+ this.addSchema(schema);
70
69
  }
71
70
  }
72
71
  addSchema(schema) {
72
+ if (schema.key in this.rules) {
73
+ this.rules[schema.key] = [...this.rules[schema.key], ...this._parseSchemaRules(schema)];
74
+ }
75
+ else {
76
+ this.rules[schema.key] = this._parseSchemaRules(schema);
77
+ }
78
+ }
79
+ setSchema(schema) {
73
80
  this.rules[schema.key] = this._parseSchemaRules(schema);
74
81
  }
82
+ setSchemas(schemas) {
83
+ this.rules = {};
84
+ this.addSchemas(schemas);
85
+ }
75
86
  /**
76
87
  * 进行数据验证
77
88
  * @param data 待验证的数据
@@ -175,8 +186,7 @@ class Validator {
175
186
  if (typeof ruleItem === "string") {
176
187
  rules.push(...this._parseStringRule(ruleItem, schema.message));
177
188
  }
178
- else if (ruleItem instanceof RegExp ||
179
- typeof ruleItem === "function") {
189
+ else if (ruleItem instanceof RegExp || typeof ruleItem === "function") {
180
190
  rules.push({
181
191
  rule: ruleItem,
182
192
  message: schema.message || defaultMsg,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ph-utils",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "js 开发工具集,前后端都可以使用(commonjs和es module)",
5
5
  "keywords": [
6
6
  "date",
@@ -96,4 +96,4 @@
96
96
  "@types/node": "^22.8.2",
97
97
  "typescript": "^5.6.3"
98
98
  }
99
- }
99
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
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.
@@ -1,79 +0,0 @@
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 DELETED
@@ -1,212 +0,0 @@
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
- }