From 2d52a14ec1b750c70ba7dfac45f05f7a77c988a4 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 28 Jul 2022 15:10:34 +0200 Subject: [PATCH] fix(core): update --- test/test.ts | 14 ++++++++++++++ ts/00_commitinfo_data.ts | 2 +- ts/smarturl.classes.smarturl.ts | 26 +++++++++++++++++++++----- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/test/test.ts b/test/test.ts index a9735a6..37d4bb6 100644 --- a/test/test.ts +++ b/test/test.ts @@ -22,4 +22,18 @@ tap.test('should parse an URL', async () => { console.log(parsedUrl.toString()); }); +tap.test('should parse an URL', async () => { + const testUrl = 'https://lossless.com:3000/'; + // 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.toString()); +}); + tap.start(); diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 253c5b1..308c64e 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@pushrocks/smarturl', - version: '3.0.1', + version: '3.0.2', description: 'a url parsing lib' } diff --git a/ts/smarturl.classes.smarturl.ts b/ts/smarturl.classes.smarturl.ts index ee3eba7..e1a879c 100644 --- a/ts/smarturl.classes.smarturl.ts +++ b/ts/smarturl.classes.smarturl.ts @@ -31,15 +31,22 @@ export class Smarturl implements IUrlObject { const searchParams: ISearchParams = {}; // enrichment - const searchParamPairs = parsedUrlInstance.search + const searchParamPairs: { + key: string; + value: string; + }[] = [] + + if (parsedUrlInstance.search) { + parsedUrlInstance.search .replace('?', '') .split('&') .map((searchParamPair) => { - return { + searchParamPairs.push({ key: searchParamPair.split('=')[0], value: searchParamPair.split('=')[1], - }; + }); }); + } for (const searchParamPair of searchParamPairs) { searchParams[searchParamPair.key] = searchParamPair.value; @@ -50,7 +57,7 @@ export class Smarturl implements IUrlObject { } } - let path = parsedUrlInstance.pathname; + let path = parsedUrlInstance.pathname || ''; if (Object.keys(searchParams).length > 0) { path += '?'; let first = true; @@ -116,6 +123,15 @@ export class Smarturl implements IUrlObject { constructor() {} toString() { - return `${this.protocol}//${this.hostname}:${this.port}${this.path}`; + let userpart = ``; + if (this.username && !this.password) { + userpart = `${this.username}@`; + } + if (this.username && this.password) { + userpart = `${this.username}:${this.password}@`; + } + + + return `${this.protocol}://${userpart}${this.hostname}:${this.port}/${this.path}`; } }