BREAKING CHANGE(core): update
This commit is contained in:
parent
105b1ca637
commit
ce6622f858
11
test/test.ts
11
test/test.ts
@ -9,8 +9,17 @@ tap.test('first test', async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should parse an URL', async () => {
|
tap.test('should parse an URL', async () => {
|
||||||
const parsedUrl = testSmarturl.parseUrl('https://lossless.com');
|
const testUrl = 'https://lossless.com:3000/?some=cool&more=yes';
|
||||||
|
// const urlMod = await import('url');
|
||||||
|
// const altParsed = urlMod.parse(testUrl);
|
||||||
|
// console.log(altParsed);
|
||||||
|
const parsedUrl = smarturl.Smarturl.createFromUrl(testUrl, {
|
||||||
|
searchParams: {
|
||||||
|
more: 'overwritten'
|
||||||
|
}
|
||||||
|
});
|
||||||
console.log(parsedUrl);
|
console.log(parsedUrl);
|
||||||
|
console.log(parsedUrl.toString());
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -9,15 +9,52 @@ export interface IUrlObject{
|
|||||||
host: string;
|
host: string;
|
||||||
hostname: string;
|
hostname: string;
|
||||||
port: string;
|
port: string;
|
||||||
|
path: string;
|
||||||
pathname: string;
|
pathname: string;
|
||||||
search: string;
|
search: string;
|
||||||
searchParams: any;
|
searchParams: ISearchParams;
|
||||||
hash: string;
|
hash: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Smarturl {
|
export interface ISearchParams {
|
||||||
public parseUrl(urlArg: string) {
|
[key: string]: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Smarturl implements IUrlObject {
|
||||||
|
public static createFromUrl(
|
||||||
|
urlArg: string,
|
||||||
|
optionsArg?: {
|
||||||
|
searchParams?: ISearchParams;
|
||||||
|
}
|
||||||
|
): Smarturl {
|
||||||
const parsedUrlInstance = new plugins.url.URL(urlArg);
|
const parsedUrlInstance = new plugins.url.URL(urlArg);
|
||||||
|
const searchParams: ISearchParams = {};
|
||||||
|
|
||||||
|
// enrichment
|
||||||
|
const searchParamKeys = parsedUrlInstance.searchParams.keys();
|
||||||
|
for (const key of searchParamKeys) {
|
||||||
|
searchParams[key] = parsedUrlInstance.searchParams.get(key);
|
||||||
|
}
|
||||||
|
if (optionsArg?.searchParams) {
|
||||||
|
for (const key of Object.keys(optionsArg.searchParams)) {
|
||||||
|
searchParams[key] = optionsArg.searchParams[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let path = parsedUrlInstance.pathname;
|
||||||
|
if (Object.keys(searchParams).length > 0) {
|
||||||
|
path += '?';
|
||||||
|
let first = true;
|
||||||
|
for(const key of Object.keys(searchParams)) {
|
||||||
|
if (first) {
|
||||||
|
first = false;
|
||||||
|
} else {
|
||||||
|
path += '&';
|
||||||
|
}
|
||||||
|
path += `${key}=${searchParams[key]}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const parsedUrl: IUrlObject = {
|
const parsedUrl: IUrlObject = {
|
||||||
href: parsedUrlInstance.href,
|
href: parsedUrlInstance.href,
|
||||||
origin: parsedUrlInstance.origin,
|
origin: parsedUrlInstance.origin,
|
||||||
@ -27,11 +64,13 @@ export class Smarturl {
|
|||||||
host: parsedUrlInstance.host,
|
host: parsedUrlInstance.host,
|
||||||
hostname: parsedUrlInstance.hostname,
|
hostname: parsedUrlInstance.hostname,
|
||||||
port: parsedUrlInstance.port,
|
port: parsedUrlInstance.port,
|
||||||
|
path,
|
||||||
pathname: parsedUrlInstance.pathname,
|
pathname: parsedUrlInstance.pathname,
|
||||||
search: parsedUrlInstance.search,
|
search: parsedUrlInstance.search,
|
||||||
searchParams: parsedUrlInstance.searchParams,
|
searchParams,
|
||||||
hash: parsedUrlInstance.hash,
|
hash: parsedUrlInstance.hash,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!parsedUrl.port && parsedUrl.protocol === 'https:') {
|
if (!parsedUrl.port && parsedUrl.protocol === 'https:') {
|
||||||
// console.log(`inferring port 443 for "https:"`);
|
// console.log(`inferring port 443 for "https:"`);
|
||||||
parsedUrl.port = '443';
|
parsedUrl.port = '443';
|
||||||
@ -40,6 +79,30 @@ export class Smarturl {
|
|||||||
// console.log(`inferring port 80 for "http:"`);
|
// console.log(`inferring port 80 for "http:"`);
|
||||||
parsedUrl.port = '80';
|
parsedUrl.port = '80';
|
||||||
}
|
}
|
||||||
return parsedUrl;
|
const returnSmarturl = new Smarturl();
|
||||||
|
Object.assign(returnSmarturl, parsedUrl);
|
||||||
|
return returnSmarturl;
|
||||||
|
}
|
||||||
|
public static createFromParsedUrl() {}
|
||||||
|
|
||||||
|
// INSTANCE
|
||||||
|
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() {}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
return `${this.protocol}//${this.hostname}:${this.port}${this.path}`
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user