-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.ts
114 lines (98 loc) · 2.9 KB
/
dom.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/**
* Returns whether element is an input element
*/
const isInputElement = (element: HTMLElement): element is HTMLInputElement => {
return element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement;
}
/**
* Indicates whether element is a radio element
*/
const isRadioElement = (element: HTMLElement): element is HTMLInputElement => {
return isInputElement(element) && element.type === 'radio';
}
/**
* Indicates whether element is a checkbox element
*/
const isCheckboxElement = (element: HTMLElement): element is HTMLInputElement => {
return isInputElement(element) && element.type === 'checkbox';
}
/**
* Returns whether element is an canvas element
*/
const isCanvasElement = (element: HTMLElement): element is HTMLCanvasElement => {
return element instanceof HTMLCanvasElement;
}
/**
* Returns whether element is a meter element
*/
const isMeterElement = (element: HTMLElement): element is HTMLMeterElement => {
return element instanceof HTMLMeterElement;
}
/**
* Returns whether element is an output element
*/
const isOutputElement = (element: HTMLElement): element is HTMLOutputElement => {
return element instanceof HTMLOutputElement;
}
/**
* Returns whether element is an select element
*/
const isSelectElement = (element: HTMLElement): element is HTMLSelectElement => {
return element instanceof HTMLSelectElement
}
/**
* Returns whether element is a progress element
*/
const isProgressElement = (element: HTMLElement): element is HTMLProgressElement => {
return element instanceof HTMLProgressElement;
}
/**
* Returns an flexible array representation of an html element collection
*/
const htmlCollectionToArray = <T extends Element>(collection: HTMLCollectionOf<T>): T[] => {
const arr: T[] = [];
for(let i = 0; i < collection.length; i++) {
arr.push(collection.item(i) as T);
}
return arr;
}
/**
* Returns the value of the element, when it exists
*/
const getValue = (element: HTMLElement): string[] => {
if (
isInputElement(element)
|| isMeterElement(element)
|| isOutputElement(element)
|| isOutputElement(element)
|| isProgressElement(element)
) {
return [(element.value || '').toString()].filter(Boolean);
}
if (isSelectElement(element)) {
return htmlCollectionToArray<HTMLOptionElement>(element.selectedOptions)
.map((option: HTMLOptionElement) => option.value)
.filter(Boolean);
}
return [];
}
const nodeListToArray = <T extends Node>(nodeList: NodeListOf<T>): T[] => {
const array: T[] = [];
nodeList.forEach((node: T) => {
array.push(node);
})
return array;
}
export {
isInputElement,
isRadioElement,
isSelectElement,
isCanvasElement,
isMeterElement,
isOutputElement,
isProgressElement,
isCheckboxElement,
htmlCollectionToArray,
getValue,
nodeListToArray
};