smarturl/ts/smarturl.classes.smarturl.ts

179 lines
5.9 KiB
TypeScript
Raw Normal View History

// Import necessary plugins (if any are used in the module)
2022-07-26 22:40:18 +00:00
import * as plugins from './smarturl.plugins.js';
2021-04-12 19:18:36 +00:00
// Interface representing the structure of a URL object
2021-05-02 16:32:24 +00:00
export interface IUrlObject {
2021-04-12 20:09:19 +00:00
href: string;
origin: string;
protocol: string;
username: string;
password: string;
host: string;
hostname: string;
port: string;
2021-05-02 16:32:24 +00:00
path: string;
2021-04-12 20:09:19 +00:00
pathname: string;
search: string;
2021-05-02 16:32:24 +00:00
searchParams: ISearchParams;
2021-04-12 20:09:19 +00:00
hash: string;
}
// Interface representing the search parameters as a key-value pair object
2021-05-02 16:32:24 +00:00
export interface ISearchParams {
[key: string]: string;
}
// Main Smarturl class implementing the IUrlObject interface
2021-05-02 16:32:24 +00:00
export class Smarturl implements IUrlObject {
// Static method to create a Smarturl instance from a URL string
2021-05-02 16:32:24 +00:00
public static createFromUrl(
urlArg: string,
optionsArg?: {
searchParams?: ISearchParams;
}
): Smarturl {
// Parse the URL string using the built-in URL class
2022-08-06 20:17:41 +00:00
const parsedUrlInstance = new URL(urlArg);
2021-05-02 16:32:24 +00:00
const searchParams: ISearchParams = {};
// Array to hold key-value pairs of search parameters
2022-07-28 13:10:34 +00:00
const searchParamPairs: {
key: string;
value: string;
}[] = [];
2022-07-28 13:10:34 +00:00
// If the URL has search parameters, parse them into key-value pairs
2022-07-28 13:10:34 +00:00
if (parsedUrlInstance.search) {
parsedUrlInstance.search
.replace('?', '') // Remove the '?' at the beginning
.split('&') // Split the query string into individual parameters
.map((searchParamPair) => {
// Split each parameter into key and value and add to the array
searchParamPairs.push({
key: searchParamPair.split('=')[0],
value: searchParamPair.split('=')[1],
});
2022-07-28 13:10:34 +00:00
});
}
2022-07-26 23:06:16 +00:00
// Convert the array of key-value pairs into an object
2022-07-26 23:06:16 +00:00
for (const searchParamPair of searchParamPairs) {
searchParams[searchParamPair.key] = searchParamPair.value;
2021-05-02 16:32:24 +00:00
}
// Merge any additional search parameters provided in optionsArg
2021-05-02 16:32:24 +00:00
if (optionsArg?.searchParams) {
for (const key of Object.keys(optionsArg.searchParams)) {
searchParams[key] = optionsArg.searchParams[key];
}
}
// Reconstruct the path with updated search parameters
2022-07-28 13:10:34 +00:00
let path = parsedUrlInstance.pathname || '';
2021-05-02 16:32:24 +00:00
if (Object.keys(searchParams).length > 0) {
path += '?';
let first = true;
2022-07-26 22:40:18 +00:00
for (const key of Object.keys(searchParams)) {
2021-05-02 16:32:24 +00:00
if (first) {
first = false;
} else {
path += '&';
}
path += `${key}=${searchParams[key]}`;
}
}
// Create an IUrlObject containing all parts of the URL
2021-04-12 20:09:19 +00:00
const parsedUrl: IUrlObject = {
...parsedUrlInstance, // Spread operator to include all properties from parsedUrlInstance
2021-04-12 20:09:19 +00:00
href: parsedUrlInstance.href,
2022-08-06 20:17:41 +00:00
origin: parsedUrlInstance.origin,
2021-04-12 20:09:19 +00:00
protocol: parsedUrlInstance.protocol,
2022-08-06 20:17:41 +00:00
username: parsedUrlInstance.username,
password: parsedUrlInstance.password,
host: parsedUrlInstance.host,
hostname: parsedUrlInstance.hostname,
2021-04-12 20:09:19 +00:00
port: parsedUrlInstance.port,
path, // Updated path with new search parameters
2021-04-12 20:09:19 +00:00
pathname: parsedUrlInstance.pathname,
search: parsedUrlInstance.search,
searchParams, // The searchParams object we constructed
2021-04-12 20:09:19 +00:00
hash: parsedUrlInstance.hash,
};
2021-05-02 16:32:24 +00:00
// Infer default ports if none are specified based on the protocol
2021-04-12 20:09:19 +00:00
if (!parsedUrl.port && parsedUrl.protocol === 'https:') {
parsedUrl.port = '443';
}
2021-04-12 20:09:53 +00:00
if (!parsedUrl.port && parsedUrl.protocol === 'http:') {
parsedUrl.port = '80';
}
// Create a new Smarturl instance and assign the parsed URL properties
2021-05-02 16:32:24 +00:00
const returnSmarturl = new Smarturl();
Object.assign(returnSmarturl, parsedUrl); // Copy all properties from parsedUrl to returnSmarturl
2021-05-02 16:32:24 +00:00
return returnSmarturl;
}
// Static method to create a Smarturl instance from an existing IUrlObject
public static createFromParsedUrl(parsedUrlArg: IUrlObject): Smarturl {
2021-05-02 16:33:50 +00:00
const returnSmarturl = new Smarturl();
Object.assign(returnSmarturl, parsedUrlArg); // Copy all properties from parsedUrlArg to returnSmarturl
2021-05-02 16:33:50 +00:00
return returnSmarturl;
}
2021-05-02 16:32:24 +00:00
// INSTANCE PROPERTIES (matching IUrlObject interface)
2021-05-02 16:32:24 +00:00
href: string;
origin: string;
protocol: string;
username: string;
password: string;
host: string;
hostname: string;
port: string;
path: string;
pathname: string;
search: string;
searchParams: ISearchParams;
hash: string;
// Constructor initializes searchParams as an empty object
2023-04-17 12:41:23 +00:00
constructor() {
this.searchParams = {};
}
2021-05-02 16:32:24 +00:00
// Method to create an independent clone of the current Smarturl instance
clone(): Smarturl {
const clonedInstance = new Smarturl();
Object.assign(clonedInstance, this); // Copy all properties to the new instance
clonedInstance.searchParams = { ...this.searchParams }; // Shallow copy of searchParams to prevent shared references
return clonedInstance;
}
/**
* Typed method to set a property and return the instance for chaining.
* @param prop - The property name to set (must be a key of Smarturl)
* @param value - The value to assign to the property
* @returns The Smarturl instance for method chaining
*/
set<K extends keyof this>(prop: K, value: this[K]): this {
this[prop] = value; // Set the property to the new value
return this; // Return the instance for chaining
}
// Method to convert the Smarturl instance back into a URL string
toString(): string {
let userpart = ``; // Initialize the user part of the URL (username and password)
// Construct the user part based on the presence of username and password
2022-07-28 13:10:34 +00:00
if (this.username && !this.password) {
userpart = `${this.username}@`;
}
if (this.username && this.password) {
userpart = `${this.username}:${this.password}@`;
}
// Construct and return the full URL string
2022-08-21 11:56:34 +00:00
return `${this.protocol}//${userpart}${this.hostname}:${this.port}${this.path}`;
2021-04-12 19:18:36 +00:00
}
}