tuijs-util 2.0.4 → 2.0.5

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/src/lib/dom.ts CHANGED
@@ -1,54 +1,54 @@
1
- /**
2
- * Takes an HTML template literal, parses it, then extracts it.
3
- */
4
- export function parseTemplate(templateLiteral: string): DocumentFragment {
5
- const parser: DOMParser = new DOMParser();
6
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
7
- const element: HTMLTemplateElement | null = parsedDocument.querySelector('template');
8
- if (!element) {
9
- const temporaryDiv: HTMLDivElement = document.createElement('div');
10
- temporaryDiv.replaceChildren(...Array.from(parsedDocument.body.childNodes));
11
- const newFragment: DocumentFragment = document.createDocumentFragment();
12
- while (temporaryDiv.firstChild) {
13
- newFragment.appendChild(temporaryDiv.firstChild);
14
- }
15
- return newFragment;
16
- }
17
- const elementFragment: DocumentFragment = element.content;
18
- return elementFragment;
19
- }
20
-
21
- /**
22
- * Takes an HTML template literal, parses it, then extracts the element.
23
- * All elements in the template MUST be contained within a single parent element.
24
- */
25
- export function parseElement(templateLiteral: string): Element | undefined {
26
- const parser: DOMParser = new DOMParser();
27
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
28
- const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
29
- return element[0];
30
- }
31
-
32
- /**
33
- * Takes an HTML table row template literal, parses it, then extracts the element.
34
- */
35
- export function parseTableRow(templateLiteral: string): Element | null {
36
- const parser: DOMParser = new DOMParser();
37
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
38
- const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
39
- const temporaryTable = document.createElement('table');
40
- temporaryTable.replaceChildren(...Array.from(element));
41
- const tableRow: HTMLTableRowElement | null = temporaryTable.querySelector("tr");
42
- temporaryTable.remove();
43
- return tableRow;
44
- }
45
-
46
- /**
47
- * Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
48
- */
49
- export function listNodes(templateLiteral: string): Element[] {
50
- const parser: DOMParser = new DOMParser();
51
- const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
52
- const elements: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
53
- return Array.from(elements);
54
- }
1
+ /**
2
+ * Takes an HTML template literal, parses it, then extracts it.
3
+ */
4
+ export function parseTemplate(templateLiteral: string): DocumentFragment {
5
+ const parser: DOMParser = new DOMParser();
6
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
7
+ const element: HTMLTemplateElement | null = parsedDocument.querySelector('template');
8
+ if (!element) {
9
+ const temporaryDiv: HTMLDivElement = document.createElement('div');
10
+ temporaryDiv.replaceChildren(...Array.from(parsedDocument.body.childNodes));
11
+ const newFragment: DocumentFragment = document.createDocumentFragment();
12
+ while (temporaryDiv.firstChild) {
13
+ newFragment.appendChild(temporaryDiv.firstChild);
14
+ }
15
+ return newFragment;
16
+ }
17
+ const elementFragment: DocumentFragment = element.content;
18
+ return elementFragment;
19
+ }
20
+
21
+ /**
22
+ * Takes an HTML template literal, parses it, then extracts the element.
23
+ * All elements in the template MUST be contained within a single parent element.
24
+ */
25
+ export function parseElement(templateLiteral: string): Element | undefined {
26
+ const parser: DOMParser = new DOMParser();
27
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
28
+ const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
29
+ return element[0];
30
+ }
31
+
32
+ /**
33
+ * Takes an HTML table row template literal, parses it, then extracts the element.
34
+ */
35
+ export function parseTableRow(templateLiteral: string): Element | null {
36
+ const parser: DOMParser = new DOMParser();
37
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
38
+ const element: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
39
+ const temporaryTable = document.createElement('table');
40
+ temporaryTable.replaceChildren(...Array.from(element));
41
+ const tableRow: HTMLTableRowElement | null = temporaryTable.querySelector("tr");
42
+ temporaryTable.remove();
43
+ return tableRow;
44
+ }
45
+
46
+ /**
47
+ * Takes an HTML template literal, parses it with the DOM parser, then extracts the NodeList. The list is then returned as an array.
48
+ */
49
+ export function listNodes(templateLiteral: string): Element[] {
50
+ const parser: DOMParser = new DOMParser();
51
+ const parsedDocument: Document = parser.parseFromString(templateLiteral, 'text/html');
52
+ const elements: NodeListOf<Element> = parsedDocument.body.querySelectorAll("*");
53
+ return Array.from(elements);
54
+ }
package/src/lib/http.ts CHANGED
@@ -1,90 +1,90 @@
1
- import { checkUrl } from './check.js';
2
-
3
- /**
4
- * Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
5
- */
6
- export function urlAddHttp(url: string): string | undefined {
7
- if (url === null || !checkUrl(url)) {
8
- throw new Error(`Invalid input.`);
9
- };
10
- if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
11
- url = "http://" + url
12
- }
13
- return url;
14
- }
15
-
16
- /**
17
- * Adds 'https://' if valid URL and 'http://' or 'https://' is missing.
18
- */
19
- export function urlAddHttps(url: string): string | undefined {
20
- if (url === null || !checkUrl(url)) {
21
- throw new Error(`Invalid input.`);
22
- };
23
- if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
24
- url = "https://" + url
25
- }
26
- return url;
27
- }
28
-
29
- /**
30
- * Sends GET request to specified URL
31
- */
32
- export async function reqGet(url: string, signal: AbortSignal | null = null) {
33
- if (!checkUrl(url)) {
34
- throw new Error(`Invalid URL`);
35
- }
36
- const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
37
- return await fetch(url, options);
38
- }
39
-
40
- /**
41
- * Collects JSON data from a given url
42
- */
43
- export async function reqGetJson(url: string, signal: AbortSignal | null = null) {
44
- const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
45
- const res = await fetch(url, options);
46
- return await res.json();
47
- }
48
-
49
- /**
50
- * Collects text data from a given url
51
- */
52
- export async function reqGetText(url: string, signal: AbortSignal | null = null) {
53
- const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
54
- const res = await fetch(url, options);
55
- return await res.text();
56
- }
57
-
58
- /**
59
- * Sends POST request to specified URL, which contains JSON data in the body.
60
- */
61
- export async function reqPostJson(url: string, data: object, signal: AbortSignal | null = null) {
62
- const options = signal ? {
63
- method: 'POST',
64
- headers: { 'Content-Type': 'application/json' },
65
- body: JSON.stringify(data),
66
- signal
67
- } : {
68
- method: 'POST',
69
- headers: { 'Content-Type': 'application/json' },
70
- body: JSON.stringify(data)
71
- };
72
- const res = await fetch(url, options);
73
- return res;
74
- }
75
-
76
- /**
77
- * Sends POST request to specified URL, which contains FormData in the body.
78
- */
79
- export async function reqPostForm(url: string, data: FormData, signal: AbortSignal | null = null) {
80
- const options = signal ? {
81
- method: 'POST',
82
- body: data,
83
- signal
84
- } : {
85
- method: 'POST',
86
- body: data
87
- };
88
- const res = await fetch(url, options);
89
- return res;
90
- }
1
+ import { checkUrl } from './check.js';
2
+
3
+ /**
4
+ * Adds 'http://' if valid URL and 'http://' or 'https://' is missing.
5
+ */
6
+ export function urlAddHttp(url: string): string | undefined {
7
+ if (url === null || !checkUrl(url)) {
8
+ throw new Error(`Invalid input.`);
9
+ };
10
+ if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
11
+ url = "http://" + url
12
+ }
13
+ return url;
14
+ }
15
+
16
+ /**
17
+ * Adds 'https://' if valid URL and 'http://' or 'https://' is missing.
18
+ */
19
+ export function urlAddHttps(url: string): string | undefined {
20
+ if (url === null || !checkUrl(url)) {
21
+ throw new Error(`Invalid input.`);
22
+ };
23
+ if (url.startsWith("http://") === false && url.startsWith("https://") === false) {
24
+ url = "https://" + url
25
+ }
26
+ return url;
27
+ }
28
+
29
+ /**
30
+ * Sends GET request to specified URL
31
+ */
32
+ export async function reqGet(url: string, signal: AbortSignal | null = null) {
33
+ if (!checkUrl(url)) {
34
+ throw new Error(`Invalid URL`);
35
+ }
36
+ const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
37
+ return await fetch(url, options);
38
+ }
39
+
40
+ /**
41
+ * Collects JSON data from a given url
42
+ */
43
+ export async function reqGetJson(url: string, signal: AbortSignal | null = null) {
44
+ const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
45
+ const res = await fetch(url, options);
46
+ return await res.json();
47
+ }
48
+
49
+ /**
50
+ * Collects text data from a given url
51
+ */
52
+ export async function reqGetText(url: string, signal: AbortSignal | null = null) {
53
+ const options: RequestInit = signal ? { method: 'GET', signal } : { method: 'GET' };
54
+ const res = await fetch(url, options);
55
+ return await res.text();
56
+ }
57
+
58
+ /**
59
+ * Sends POST request to specified URL, which contains JSON data in the body.
60
+ */
61
+ export async function reqPostJson(url: string, data: object, signal: AbortSignal | null = null) {
62
+ const options = signal ? {
63
+ method: 'POST',
64
+ headers: { 'Content-Type': 'application/json' },
65
+ body: JSON.stringify(data),
66
+ signal
67
+ } : {
68
+ method: 'POST',
69
+ headers: { 'Content-Type': 'application/json' },
70
+ body: JSON.stringify(data)
71
+ };
72
+ const res = await fetch(url, options);
73
+ return res;
74
+ }
75
+
76
+ /**
77
+ * Sends POST request to specified URL, which contains FormData in the body.
78
+ */
79
+ export async function reqPostForm(url: string, data: FormData, signal: AbortSignal | null = null) {
80
+ const options = signal ? {
81
+ method: 'POST',
82
+ body: data,
83
+ signal
84
+ } : {
85
+ method: 'POST',
86
+ body: data
87
+ };
88
+ const res = await fetch(url, options);
89
+ return res;
90
+ }