fix(core): update
This commit is contained in:
parent
55894a30f2
commit
2d52a14ec1
14
test/test.ts
14
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();
|
||||
|
@ -3,6 +3,6 @@
|
||||
*/
|
||||
export const commitinfo = {
|
||||
name: '@pushrocks/smarturl',
|
||||
version: '3.0.1',
|
||||
version: '3.0.2',
|
||||
description: 'a url parsing lib'
|
||||
}
|
||||
|
@ -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}`;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user