Skip to content

Commit 99439d6

Browse files
committed
refactor
1 parent bafa849 commit 99439d6

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

src/scriptlets/href-sanitizer.ts

+18-19
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export function hrefSanitizer(
188188
const REMOVE_HASH_TRANSFORM_MARKER = 'removeHash';
189189
const REMOVE_PARAM_TRANSFORM_MARKER = 'removeParam';
190190
const MARKER_SEPARATOR = ':';
191+
const COMMA = ',';
191192

192193
// Regular expression to find not valid characters at the beginning and at the end of the string,
193194
// \x21-\x7e is a range that includes the ASCII characters from ! (hex 21) to ~ (hex 7E).
@@ -420,30 +421,28 @@ export function hrefSanitizer(
420421
* @returns URL without the parameter(s) or empty string if no parameter is found
421422
*/
422423
const removeParam = (url: string, transformValue: string) => {
423-
// get the parameter values to remove
424-
const paramNamesToRemoveStr = transformValue.includes(MARKER_SEPARATOR)
425-
? transformValue.split(MARKER_SEPARATOR)[1]
426-
: undefined;
427-
428424
const urlObj = new URL(url, window.location.origin);
429425

430-
// if the parameter(s) value is specified, remove it
431-
if (paramNamesToRemoveStr) {
432-
const initSearchParamsLength = urlObj.searchParams.toString().length;
426+
// get the parameter values to remove
427+
const paramNamesToRemoveStr = transformValue.split(MARKER_SEPARATOR)[1];
433428

434-
const removeParams = paramNamesToRemoveStr.split(',');
435-
removeParams.forEach((param) => {
436-
if (urlObj.searchParams.has(param)) {
437-
urlObj.searchParams.delete(param);
438-
}
439-
});
429+
if (!paramNamesToRemoveStr) {
430+
urlObj.search = '';
431+
return urlObj.toString().replace(window.location.origin, '');
432+
}
440433

441-
// if the parameter(s) is not found, return empty string
442-
if (initSearchParamsLength === urlObj.searchParams.toString().length) {
443-
return '';
434+
const initSearchParamsLength = urlObj.searchParams.toString().length;
435+
436+
const removeParams = paramNamesToRemoveStr.split(COMMA);
437+
removeParams.forEach((param) => {
438+
if (urlObj.searchParams.has(param)) {
439+
urlObj.searchParams.delete(param);
444440
}
445-
} else {
446-
urlObj.search = '';
441+
});
442+
443+
// if the parameter(s) is not found, return empty string
444+
if (initSearchParamsLength === urlObj.searchParams.toString().length) {
445+
return '';
447446
}
448447

449448
return urlObj.toString().replace(window.location.origin, '');

0 commit comments

Comments
 (0)