-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.js
118 lines (105 loc) · 2.11 KB
/
util.js
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
115
116
117
118
/**
* @param {string} str
* @returns {string}
*/
export function convertDashToCamel(str) {
return str.replace(/-([a-z0-9])/g, (g) => g[1].toUpperCase());
}
/**
* @param {string} str
* @returns {string}
*/
export function convertCamelToDash(str) {
return str.replace(/([a-zA-Z0-9])(?=[A-Z])/g, "$1-").toLowerCase();
}
/**
* @param {string} html
* @returns {HTMLTemplateElement}
*/
export function createTemplate(html) {
const template = document.createElement("template");
template.innerHTML = html;
return template;
}
/**
* @param {unknown} input
* @returns {string}
*/
export function stringify(input) {
return typeof input === "string"
? input
: typeof input === "number"
? input.toString()
: "";
}
/**
* @param {unknown} input
* @returns {input is string}
*/
export function isString(input) {
return typeof input === "string";
}
/**
* @param {unknown} input
* @returns {input is null}
*/
export function isNull(input) {
return input === null;
}
/**
* @param {unknown} input
* @returns {boolean}
*/
export function isNotNull(input) {
return input !== null;
}
/**
* @param {unknown} input
* @returns {input is true}
*/
export function isTrue(input) {
return input === true;
}
/**
* @param {unknown} input
* @returns {input is false}
*/
export function isFalse(input) {
return input === false;
}
/**
* @param {unknown} obj
* @returns {obj is Record<string, unknown>}
*/
export function isObject(obj) {
return (obj !== null && typeof obj === "object" &&
Array.isArray(obj) === false);
}
/**
* @param {unknown} obj
* @returns {obj is unknown[]}
*/
export function isArray(obj) {
return Array.isArray(obj);
}
/**
* @param {unknown} input
* @returns {input is HTMLElement}
*/
export function isHtmlElement(input) {
return input instanceof HTMLElement;
}
/**
* @param {unknown} input
* @returns {input is HTMLTemplateElement}
*/
export function isTemplate(input) {
return input instanceof HTMLTemplateElement;
}
/**
* @param {string} keyName
* @returns {string | null }
*/
export function getJwt(keyName) {
return window.localStorage.getItem(keyName);
}