sussy-util
Version:
Util package made by me
234 lines (233 loc) • 8.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class UrlUtils {
constructor() { }
/**
* If the URL is valid, it will return true, otherwise it will return false.
* @param {string} url - The URL to validate.
* @returns {boolean} A boolean value.
*/
isUrl(url) {
try {
new URL(url);
return true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
}
catch (e) {
return false;
}
}
/**
* It takes a string and returns a URL object
* @param {string} url - The URL to parse.
* @returns A new URL object.
*/
parseUrl(url) {
return new URL(url);
}
/**
* It takes a URL as a string, and returns the domain name as a string
* @param {string} url - The URL to get the domain name from.
* @returns The hostname of the URL.
*/
getDomainName(url) {
return new URL(url).hostname;
}
/**
* It takes a URL and returns the pathname of the URL
* @param {string} url - The URL to parse.
* @returns The pathname of the url.
*/
getPath(url) {
return new URL(url).pathname;
}
/**
* It takes a URL and a set of key-value pairs and returns a new URL with the key-value pairs added
* as query parameters.
* @param {string} url - string - The URL to add the query parameters to.
* @param params - MutableObject<string>
* @returns A string
*/
addQueryParams(url, params) {
const parsedUrl = this.parseUrl(url);
Object.entries(params).forEach(([key, value]) => parsedUrl.searchParams.append(key, value));
return parsedUrl.toString();
}
/**
* It takes a URL and a query parameter name, and returns the URL with the query parameter removed.
* @param {string} url - The URL to be parsed.
* @param {string} param - The query parameter to remove
* @returns A string
*/
removeQueryParam(url, param) {
const parsedUrl = this.parseUrl(url);
parsedUrl.searchParams.delete(param);
return parsedUrl.toString();
}
/**
* It takes a URL and a set of key-value pairs, and returns a new URL with the query parameters
* updated to match the key-value pairs
* @param {string} url - string - The URL to edit
* @param params - MutableObject<string>
* @returns A string
*/
editQueryParams(url, params) {
const parsedUrl = this.parseUrl(url);
Object.entries(params).forEach(([key, value]) => {
if (parsedUrl.searchParams.has(key)) {
parsedUrl.searchParams.delete(key);
}
parsedUrl.searchParams.set(key, value);
});
return parsedUrl.toString();
}
/**
* It takes a URL and returns an object containing the query parameters as key-value pairs.
* @param {string} url - The URL to extract query parameters from.
* @returns {MutableObject<string>} An object containing the query parameters.
*/
getQueryParams(url) {
const parsedUrl = this.parseUrl(url);
const queryParams = {};
parsedUrl.searchParams.forEach((value, key) => (queryParams[key] = value));
return queryParams;
}
/**
* It takes a URL and a query parameter name, and returns the value of the specified query parameter.
* @param {string} url - The URL to extract the query parameter value from.
* @param {string} param - The query parameter name.
* @returns {string | null} The value of the specified query parameter, or null if it doesn't exist.
*/
getQueryParamValue(url, param) {
const parsedUrl = this.parseUrl(url);
return parsedUrl.searchParams.get(param);
}
/**
* It takes a URL and a new path, and returns a new URL with the updated path.
* @param {string} url - The URL to update the path for.
* @param {string} newPath - The new path to set.
* @returns {string} The updated URL with the new path.
*/
updatePath(url, newPath) {
const parsedUrl = this.parseUrl(url);
parsedUrl.pathname = newPath;
return parsedUrl.toString();
}
/**
* It takes a URL and returns a boolean indicating whether it has any query parameters.
* @param {string} url - The URL to check for query parameters.
* @returns {boolean} A boolean value indicating if the URL has query parameters.
*/
hasQueryParams(url) {
const parsedUrl = this.parseUrl(url);
return parsedUrl.searchParams && parsedUrl.searchParams.toString() !== '';
}
/**
* Checks if the URL has a specific query parameter.
* @param {string} url - The URL to check.
* @param {string} param - The query parameter name.
* @returns {boolean} True if the parameter exists, false otherwise.
*/
hasQueryParam(url, param) {
const parsedUrl = this.parseUrl(url);
return parsedUrl.searchParams.has(param);
}
/**
* Appends or updates query parameters from an object to a URL.
* @param {string} url - The URL to update.
* @param {MutableObject<string>} params - The query parameters to append or update.
* @returns {string} The updated URL.
*/
updateQueryParams(url, params) {
const parsedUrl = this.parseUrl(url);
for (const [key, value] of Object.entries(params)) {
parsedUrl.searchParams.set(key, value);
}
return parsedUrl.toString();
}
/**
* Replaces specified query parameters with new values.
* @param {string} url - The URL to replace query parameters in.
* @param {MutableObject<string>} replacements - The replacements for query parameters.
* @returns {string} The URL with replaced query parameters.
*/
replaceQueryParams(url, replacements) {
const queryParams = this.getQueryParams(url);
for (const [key, value] of Object.entries(replacements)) {
if (Object.prototype.hasOwnProperty.call(queryParams, key)) {
queryParams[key] = value;
}
}
return this.updateQueryParams(url, queryParams);
}
/**
* Removes specified query parameters from a URL.
* @param {string} url - The URL to remove query parameters from.
* @param {string[]} paramsToRemove - The names of query parameters to remove.
* @returns {string} The URL with specified query parameters removed.
*/
removeQueryParams(url, paramsToRemove) {
const parsedUrl = this.parseUrl(url);
for (const param of paramsToRemove) {
parsedUrl.searchParams.delete(param);
}
return parsedUrl.toString();
}
/**
* Merges query parameters from two URLs, prioritizing parameters from the second URL.
* @param {string} url1 - The first URL.
* @param {string} url2 - The second URL.
* @returns {string} The merged URL with query parameters.
*/
mergeQueryParams(url1, url2) {
const queryParams1 = this.getQueryParams(url1);
const queryParams2 = this.getQueryParams(url2);
const mergedParams = { ...queryParams1, ...queryParams2 };
return this.updateQueryParams(url1, mergedParams);
}
/**
* It takes a URL and returns the protocol (e.g., 'http:', 'https:').
* @param {string} url - The URL to extract the protocol from.
* @returns {string} The protocol of the URL.
*/
getProtocol(url) {
return this.parseUrl(url).protocol;
}
/**
* It takes a URL and returns the port number as a string.
* @param {string} url - The URL to extract the port from.
* @returns {string} The port number of the URL.
*/
getPort(url) {
return this.parseUrl(url).port;
}
/**
* It takes a URL and returns a boolean indicating whether it is an absolute URL.
* @param {string} url - The URL to check.
* @returns {boolean} A boolean value indicating if the URL is absolute.
*/
isAbsoluteUrl(url) {
return this.parseUrl(url).protocol !== null;
}
/**
* It takes a string and returns the URL-encoded version.
* @param {string} component - The string to encode.
* @returns {string} The URL-encoded string.
*/
encodeUrlComponent(component) {
return encodeURIComponent(component);
}
/**
* It takes a URL-encoded string and returns the decoded version.
* @param {string} encodedComponent - The URL-encoded string to decode.
* @returns {string} The decoded string.
*/
decodeUrlComponent(encodedComponent) {
return decodeURIComponent(encodedComponent);
}
static getInstance() {
return this.instance;
}
}
UrlUtils.instance = new UrlUtils();
exports.default = UrlUtils.getInstance();