Skip to content

Commit 083d56a

Browse files
committed
feat: special values and regex support
1 parent bbb819d commit 083d56a

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

src/getValue.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ HTMLHeadingElement.prototype.getValue = function () {
1717

1818
// TODO: check if using a a switch case will provide better performance
1919
const getValue = (element) => {
20-
let value = element.value || element.getAttribute('value')
20+
let value = element.value || element.getAttribute('value') || "";
2121
if (element.hasAttribute('component') || element.hasAttribute('plugin')) {
2222
value = storage.get(element)
2323
storage.delete(element)
@@ -64,6 +64,11 @@ const getValue = (element) => {
6464
value = [Number(element.min), Number(element.value)];
6565
} else if (element.type === "password") {
6666
value = btoa(value || '');
67+
} else if (element.type === "email") {
68+
value = value.toLowerCase();
69+
} else if (element.type === "url") {
70+
// TODO: define attributes to return url parts
71+
// return as a string or an object of url parts
6772
} else if (element.tagName == "SELECT" && element.hasAttribute('multiple')) {
6873
let options = element.selectedOptions;
6974
value = [];
@@ -109,13 +114,61 @@ const getValue = (element) => {
109114
}
110115
}
111116

112-
if (value === '$user_id')
113-
value = localStorage.getItem('user_id')
114-
else if (value === '$organization_id')
117+
if (value === '$organization_id')
115118
value = localStorage.getItem('organization_id')
119+
else if (value === '$user_id')
120+
value = localStorage.getItem('user_id')
116121
else if (value === '$clientId')
117122
value = localStorage.getItem('clientId')
123+
else if (value === '$session_id')
124+
value = localStorage.getItem('session_id')
125+
else if ([
126+
'$href',
127+
'$origin',
128+
'$protocol',
129+
'$host',
130+
'$hostname',
131+
'$port',
132+
'$pathname',
133+
'$search',
134+
'$hash'
135+
].includes(value)) {
136+
value = window.location[value.substring(1)]
137+
}
138+
139+
let replace = element.getAttribute('value-replace');
140+
let replaceAll = element.getAttribute('value-replaceall');
141+
142+
if (replace || replaceAll) {
143+
let replaceWith = element.getAttribute('value-replace-with') || "";
144+
let replaceRegex = element.getAttribute('value-replace-regex');
145+
146+
if (replaceRegex) {
147+
try {
148+
if (replace)
149+
value = value.replace(replaceRegex, replaceWith);
150+
else
151+
value = value.replaceAll(replaceRegex, replaceWith);
152+
} catch (error) {
153+
console.error('getValue() Regex error:', error, element);
154+
}
155+
} else {
156+
if (replace)
157+
value = value.replace(replace, replaceWith);
158+
else
159+
value = value.replaceAll(replaceAll, replaceWith);
160+
}
161+
}
162+
163+
let lowercase = element.getAttribute('value-lowercase')
164+
if (lowercase || lowercase === '')
165+
value = value.toLowerCase()
166+
let uppercase = element.getAttribute('value-uppercase');
167+
if (uppercase || uppercase === '')
168+
value = value.toUpperCase()
169+
118170
return value;
171+
119172
};
120173

121174
export { getValue, storage };

0 commit comments

Comments
 (0)