ph-utils 0.18.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 +4 -3
- package/lib/dom.js +22 -7
- package/lib/validator.d.ts +3 -3
- package/lib/validator.js +15 -5
- package/package.json +1 -1
package/lib/dom.d.ts
CHANGED
|
@@ -10,6 +10,7 @@
|
|
|
10
10
|
type FormatStyleParam = (string | undefined | null)[] | Record<string, boolean | string | undefined | null> | string;
|
|
11
11
|
type FormatClassParam = (string | undefined | null | boolean)[] | Record<string, boolean | string | undefined | null | boolean> | string;
|
|
12
12
|
type DocumentContext = HTMLElement | ShadowRoot | Document;
|
|
13
|
+
type NodeChildren = HTMLElement | DocumentFragment | HTMLElement[] | NodeListOf<HTMLElement> | HTMLCollection;
|
|
13
14
|
export declare function elem(selector: string | HTMLElement, dom?: DocumentContext): HTMLElement[];
|
|
14
15
|
/**
|
|
15
16
|
* 根据选择器获取 DOM 元素。
|
|
@@ -22,7 +23,7 @@ export declare function $(selector: string | HTMLElement, dom?: DocumentContext)
|
|
|
22
23
|
* 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
|
|
23
24
|
* @param tag - 元素标签名或 HTML 字符串。
|
|
24
25
|
* @param option - 元素的属性、样式、文本内容等配置。所有不是指定的属性,都会通过 setAttribute 设置
|
|
25
|
-
* @param
|
|
26
|
+
* @param children - 子节点
|
|
26
27
|
* @returns 创建的 HTML 元素。
|
|
27
28
|
*/
|
|
28
29
|
export declare function create(tag: string, option?: {
|
|
@@ -32,7 +33,7 @@ export declare function create(tag: string, option?: {
|
|
|
32
33
|
innerHTML?: string;
|
|
33
34
|
outerHTML?: string;
|
|
34
35
|
[index: string]: any;
|
|
35
|
-
},
|
|
36
|
+
}, children?: NodeChildren): HTMLElement;
|
|
36
37
|
/** 创建节点 */
|
|
37
38
|
export declare function $$(tag: string, option?: {
|
|
38
39
|
class?: FormatClassParam;
|
|
@@ -41,7 +42,7 @@ export declare function $$(tag: string, option?: {
|
|
|
41
42
|
innerHTML?: string;
|
|
42
43
|
outerHTML?: string;
|
|
43
44
|
[index: string]: any;
|
|
44
|
-
},
|
|
45
|
+
}, children?: NodeChildren): HTMLElement;
|
|
45
46
|
/**
|
|
46
47
|
* 根据选择器获取匹配的第一个 DOM 元素。
|
|
47
48
|
* @param selector - 选择器字符串或直接的 HTMLElement。
|
package/lib/dom.js
CHANGED
|
@@ -19,10 +19,10 @@ export function $(selector, dom) {
|
|
|
19
19
|
* 创建一个 HTML 元素,支持通过标签名或 HTML 字符串创建。
|
|
20
20
|
* @param tag - 元素标签名或 HTML 字符串。
|
|
21
21
|
* @param option - 元素的属性、样式、文本内容等配置。所有不是指定的属性,都会通过 setAttribute 设置
|
|
22
|
-
* @param
|
|
22
|
+
* @param children - 子节点
|
|
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 元素。
|
package/lib/validator.d.ts
CHANGED
|
@@ -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.
|
|
65
|
-
this.addSchemas(schemas);
|
|
64
|
+
this.setSchemas(schemas);
|
|
66
65
|
}
|
|
67
66
|
addSchemas(schemas) {
|
|
68
67
|
for (let schema of schemas) {
|
|
69
|
-
this.
|
|
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,
|