Add more URL parse helper functions, update Readme file
All checks were successful
JavaScriptUtilsVitest / ci-tests (push) Successful in 22s
All checks were successful
JavaScriptUtilsVitest / ci-tests (push) Successful in 22s
`npm build` is deprecated it is now `npm run` Add more url parser - set/remove entry from URL - get single entry from URL alt version - check if url has key entry
This commit is contained in:
@@ -100,19 +100,19 @@ Build with the following commands, the output will be stored in "build/js/output
|
|||||||
For full not minified version
|
For full not minified version
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm build utils-build
|
npm run utils-build
|
||||||
```
|
```
|
||||||
|
|
||||||
For minified build (but keeps the function names as is)
|
For minified build (but keeps the function names as is)
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm build utils-min-build
|
npm run utils-min-build
|
||||||
```
|
```
|
||||||
|
|
||||||
Or build both at the same time
|
Or build both at the same time
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
npm build utils-build-all
|
npm run utils-build-all
|
||||||
```
|
```
|
||||||
|
|
||||||
## Develop
|
## Develop
|
||||||
|
|||||||
@@ -61,7 +61,11 @@ import {
|
|||||||
} from './utils/FormatBytes.mjs';
|
} from './utils/FormatBytes.mjs';
|
||||||
import {
|
import {
|
||||||
parseQueryString as _parseQueryString,
|
parseQueryString as _parseQueryString,
|
||||||
getQueryStringParam as _getQueryStringParam
|
getQueryStringParam as _getQueryStringParam,
|
||||||
|
hasUrlParameter as _hasUrlParameter,
|
||||||
|
getUrlParameter as _getUrlParameter,
|
||||||
|
updateUrlParameter as _updateUrlParameter,
|
||||||
|
removeUrlParameter as _removeUrlParameter,
|
||||||
} from './utils/UrlParser.mjs';
|
} from './utils/UrlParser.mjs';
|
||||||
import {
|
import {
|
||||||
loginLogout as _loginLogout,
|
loginLogout as _loginLogout,
|
||||||
@@ -937,16 +941,14 @@ function html_options_refill(name, data, sort = '') // eslint-disable-line no-un
|
|||||||
* ALTERNATIVE CODE
|
* ALTERNATIVE CODE
|
||||||
* var url = new URL(window.location.href);
|
* var url = new URL(window.location.href);
|
||||||
* param_uid = url.searchParams.get('uid');
|
* param_uid = url.searchParams.get('uid');
|
||||||
* @param {String} [query=''] the query string to parse
|
* @param {String} [query=''] the query string to parse, if not set will auto fill
|
||||||
* if not set will auto fill
|
* @param {String} [return_key=''] if set only returns this key entry or empty for none
|
||||||
* @param {String} [return_key=''] if set only returns this key entry
|
* @param {Boolean} [single=false] if set to true then only the first found will be returned
|
||||||
* or empty for none
|
|
||||||
* @return {Object|String} parameter entry list
|
|
||||||
*/
|
*/
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
function parseQueryString(query = '', return_key = '') // eslint-disable-line no-unused-vars
|
function parseQueryString(query = '', return_key = '', single = false) // eslint-disable-line no-unused-vars
|
||||||
{
|
{
|
||||||
return _parseQueryString(query, return_key);
|
return _parseQueryString(query, return_key, single);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -955,15 +957,12 @@ function parseQueryString(query = '', return_key = '') // eslint-disable-line no
|
|||||||
* if a parameter is set several times it will be returned as an array
|
* if a parameter is set several times it will be returned as an array
|
||||||
* if search parameter set and nothing found and empty string is returned
|
* if search parameter set and nothing found and empty string is returned
|
||||||
* if no parametes exist and no serach is set and empty object is returned
|
* if no parametes exist and no serach is set and empty object is returned
|
||||||
* @param {String} [search=''] if set searches for this entry, if empty
|
* @param {String} [search=''] if set searches for this entry, if empty all parameters are returned
|
||||||
* all parameters are returned
|
* @param {String} [query=''] different query string to parse, if not set (default) the current window href is used
|
||||||
* @param {String} [query=''] different query string to parse, if not
|
* @param {Boolean} [single=false] if set to true then only the first found will be returned
|
||||||
* set (default) the current window href is used
|
* @return {Object|Array|String} if search is empty, object, if search is set
|
||||||
* @param {Boolean} [single=false] if set to true then only the first found
|
* and only one entry, then string, else array
|
||||||
* will be returned
|
* unless single is true
|
||||||
* @return {Object|Array|String} if search is empty, object, if search is set
|
|
||||||
* and only one entry, then string, else array
|
|
||||||
* unless single is true
|
|
||||||
*/
|
*/
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
function getQueryStringParam(search = '', query = '', single = false) // eslint-disable-line no-unused-vars
|
function getQueryStringParam(search = '', query = '', single = false) // eslint-disable-line no-unused-vars
|
||||||
@@ -971,6 +970,51 @@ function getQueryStringParam(search = '', query = '', single = false) // eslint-
|
|||||||
return _getQueryStringParam(search, query, single);
|
return _getQueryStringParam(search, query, single);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add or update a query parameter in the current URL and update the browser's address bar
|
||||||
|
* @param {string} key - The parameter name to add or update
|
||||||
|
* @param {string} value - The value to set for the parameter
|
||||||
|
* @param {boolean} [reload=false] - Whether to reload the page after updating the URL
|
||||||
|
*/
|
||||||
|
// @ts-ignore
|
||||||
|
function updateUrlParameter(key, value, reload = false) // eslint-disable-line no-unused-vars
|
||||||
|
{
|
||||||
|
return _updateUrlParameter(key, value, reload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a parameter from the current URL and update the browser's address bar
|
||||||
|
* @param {string} key - The parameter name to remove
|
||||||
|
* @param {boolean} [reload=false] - Whether to reload the page after updating the URL
|
||||||
|
*/
|
||||||
|
// @ts-ignore
|
||||||
|
function removeUrlParameter(key, reload = false) // eslint-disable-line no-unused-vars
|
||||||
|
{
|
||||||
|
return _removeUrlParameter(key, reload);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if key exists as URL parameter
|
||||||
|
* @param {String} key URL parameter to search
|
||||||
|
* @returns {Boolean} True if key exists
|
||||||
|
*/
|
||||||
|
// @ts-ignore
|
||||||
|
function hasUrlParameter(key) // eslint-disable-line no-unused-vars
|
||||||
|
{
|
||||||
|
return _hasUrlParameter(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return one value for a URL paramter or null if not found
|
||||||
|
* @param {String} key Which URL parameter to get
|
||||||
|
* @returns {String|Null} URL parameter content
|
||||||
|
*/
|
||||||
|
// @ts-ignore
|
||||||
|
function getUrlParameter(key) // eslint-disable-line no-unused-vars
|
||||||
|
{
|
||||||
|
return _getUrlParameter(key);
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: ACL LOGIN
|
// MARK: ACL LOGIN
|
||||||
// *** MASTER logout call
|
// *** MASTER logout call
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ Date: 2025//3/6
|
|||||||
Creator: Clemens Schwaighofer
|
Creator: Clemens Schwaighofer
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export { parseQueryString, getQueryStringParam };
|
export { parseQueryString, getQueryStringParam, hasUrlParameter, getUrlParameter, updateUrlParameter, removeUrlParameter };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NOTE: this original code was wrong, now using URL and parsing through
|
* NOTE: this original code was wrong, now using URL and parsing through
|
||||||
@@ -13,13 +13,10 @@ export { parseQueryString, getQueryStringParam };
|
|||||||
* ALTERNATIVE CODE
|
* ALTERNATIVE CODE
|
||||||
* var url = new URL(window.location.href);
|
* var url = new URL(window.location.href);
|
||||||
* param_uid = url.searchParams.get('uid');
|
* param_uid = url.searchParams.get('uid');
|
||||||
* @param {String} [query=''] the query string to parse
|
* @param {String} [query=''] the query string to parse, if not set will auto fill
|
||||||
* if not set will auto fill
|
* @param {String} [return_key=''] if set only returns this key entry or empty for none
|
||||||
* @param {String} [return_key=''] if set only returns this key entry
|
* @param {Boolean} [single=false] if set to true then only the first found will be returned
|
||||||
* or empty for none
|
* @return {Object|String} parameter entry list
|
||||||
* @param {Boolean} [single=false] if set to true then only the first found
|
|
||||||
* will be returned
|
|
||||||
* @return {Object|String} parameter entry list
|
|
||||||
*/
|
*/
|
||||||
function parseQueryString(query = '', return_key = '', single = false)
|
function parseQueryString(query = '', return_key = '', single = false)
|
||||||
{
|
{
|
||||||
@@ -32,15 +29,12 @@ function parseQueryString(query = '', return_key = '', single = false)
|
|||||||
* if a parameter is set several times it will be returned as an array
|
* if a parameter is set several times it will be returned as an array
|
||||||
* if search parameter set and nothing found and empty string is returned
|
* if search parameter set and nothing found and empty string is returned
|
||||||
* if no parametes exist and no serach is set and empty object is returned
|
* if no parametes exist and no serach is set and empty object is returned
|
||||||
* @param {String} [search=''] if set searches for this entry, if empty
|
* @param {String} [search=''] if set searches for this entry, if empty all parameters are returned
|
||||||
* all parameters are returned
|
* @param {String} [query=''] different query string to parse, if not set (default) the current window href is used
|
||||||
* @param {String} [query=''] different query string to parse, if not
|
* @param {Boolean} [single=false] if set to true then only the first found will be returned
|
||||||
* set (default) the current window href is used
|
* @return {Object|Array|String} if search is empty, object, if search is set
|
||||||
* @param {Boolean} [single=false] if set to true then only the first found
|
* and only one entry, then string, else array
|
||||||
* will be returned
|
* unless single is true
|
||||||
* @return {Object|Array|String} if search is empty, object, if search is set
|
|
||||||
* and only one entry, then string, else array
|
|
||||||
* unless single is true
|
|
||||||
*/
|
*/
|
||||||
function getQueryStringParam(search = '', query = '', single = false)
|
function getQueryStringParam(search = '', query = '', single = false)
|
||||||
{
|
{
|
||||||
@@ -75,4 +69,74 @@ function getQueryStringParam(search = '', query = '', single = false)
|
|||||||
return param;
|
return param;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if key exists as URL parameter
|
||||||
|
* @param {String} key URL parameter to search
|
||||||
|
* @returns {Boolean} True if key exists
|
||||||
|
*/
|
||||||
|
function hasUrlParameter(key)
|
||||||
|
{
|
||||||
|
var urlParams = new URLSearchParams(window.location.search);
|
||||||
|
return urlParams.has(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return one value for a URL paramter or null if not found
|
||||||
|
* @param {String} key Which URL parameter to get
|
||||||
|
* @returns {String|Null} URL parameter content
|
||||||
|
*/
|
||||||
|
function getUrlParameter(key)
|
||||||
|
{
|
||||||
|
var urlParams = new URLSearchParams(window.location.search);
|
||||||
|
return urlParams.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add or update a query parameter in the current URL and update the browser's address bar
|
||||||
|
* @param {string} key - The parameter name to add or update
|
||||||
|
* @param {string} value - The value to set for the parameter
|
||||||
|
* @param {boolean} [reload=false] - Whether to reload the page after updating the URL
|
||||||
|
*/
|
||||||
|
function updateUrlParameter(key, value, reload = false)
|
||||||
|
{
|
||||||
|
// Create a URL object from the current URL
|
||||||
|
const url = new URL(window.location.href);
|
||||||
|
|
||||||
|
// Set or update the parameter
|
||||||
|
url.searchParams.set(key, value);
|
||||||
|
|
||||||
|
const newUrl = url.toString();
|
||||||
|
|
||||||
|
// Update the browser's address bar without reloading the page
|
||||||
|
window.history.pushState({ path: newUrl }, '', newUrl);
|
||||||
|
|
||||||
|
// Optionally reload the page
|
||||||
|
if (reload) {
|
||||||
|
// window.location.href = newUrl;
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a parameter from the current URL and update the browser's address bar
|
||||||
|
* @param {string} key - The parameter name to remove
|
||||||
|
* @param {boolean} [reload=false] - Whether to reload the page after updating the URL
|
||||||
|
*/
|
||||||
|
function removeUrlParameter(key, reload = false)
|
||||||
|
{
|
||||||
|
// Create a URL object from the current URL
|
||||||
|
const url = new URL(window.location.href);
|
||||||
|
|
||||||
|
// Remove the parameter if it exists
|
||||||
|
url.searchParams.delete(key);
|
||||||
|
|
||||||
|
// Update the browser's address bar without reloading the page
|
||||||
|
window.history.pushState({}, '', url.toString());
|
||||||
|
|
||||||
|
// Optionally reload the page
|
||||||
|
if (reload) {
|
||||||
|
window.location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// __EMD__
|
// __EMD__
|
||||||
|
|||||||
Reference in New Issue
Block a user