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());
|
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();
|
tap.start();
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@pushrocks/smarturl',
|
name: '@pushrocks/smarturl',
|
||||||
version: '3.0.1',
|
version: '3.0.2',
|
||||||
description: 'a url parsing lib'
|
description: 'a url parsing lib'
|
||||||
}
|
}
|
||||||
|
@ -31,15 +31,22 @@ export class Smarturl implements IUrlObject {
|
|||||||
const searchParams: ISearchParams = {};
|
const searchParams: ISearchParams = {};
|
||||||
|
|
||||||
// enrichment
|
// enrichment
|
||||||
const searchParamPairs = parsedUrlInstance.search
|
const searchParamPairs: {
|
||||||
|
key: string;
|
||||||
|
value: string;
|
||||||
|
}[] = []
|
||||||
|
|
||||||
|
if (parsedUrlInstance.search) {
|
||||||
|
parsedUrlInstance.search
|
||||||
.replace('?', '')
|
.replace('?', '')
|
||||||
.split('&')
|
.split('&')
|
||||||
.map((searchParamPair) => {
|
.map((searchParamPair) => {
|
||||||
return {
|
searchParamPairs.push({
|
||||||
key: searchParamPair.split('=')[0],
|
key: searchParamPair.split('=')[0],
|
||||||
value: searchParamPair.split('=')[1],
|
value: searchParamPair.split('=')[1],
|
||||||
};
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
for (const searchParamPair of searchParamPairs) {
|
for (const searchParamPair of searchParamPairs) {
|
||||||
searchParams[searchParamPair.key] = searchParamPair.value;
|
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) {
|
if (Object.keys(searchParams).length > 0) {
|
||||||
path += '?';
|
path += '?';
|
||||||
let first = true;
|
let first = true;
|
||||||
@ -116,6 +123,15 @@ export class Smarturl implements IUrlObject {
|
|||||||
constructor() {}
|
constructor() {}
|
||||||
|
|
||||||
toString() {
|
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