8 Commits

Author SHA1 Message Date
b1162191c6 3.0.3 2022-08-06 22:17:41 +02:00
60f1368071 fix(core): update 2022-08-06 22:17:41 +02:00
1c503ec655 3.0.2 2022-07-28 15:10:34 +02:00
2d52a14ec1 fix(core): update 2022-07-28 15:10:34 +02:00
55894a30f2 3.0.1 2022-07-27 01:06:17 +02:00
249c50a3c5 fix(core): update 2022-07-27 01:06:16 +02:00
6470b05152 3.0.0 2022-07-27 00:40:46 +02:00
cfe189b02f BREAKING CHANGE(core): switch to esm 2022-07-27 00:40:46 +02:00
7 changed files with 53 additions and 16 deletions

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "@pushrocks/smarturl",
"version": "2.0.2",
"version": "3.0.3",
"lockfileVersion": 2,
"requires": true,
"packages": {
"": {
"name": "@pushrocks/smarturl",
"version": "2.0.2",
"version": "3.0.3",
"license": "MIT",
"devDependencies": {
"@gitzone/tsbuild": "^2.1.63",

View File

@ -1,6 +1,6 @@
{
"name": "@pushrocks/smarturl",
"version": "2.0.2",
"version": "3.0.3",
"private": false,
"description": "a url parsing lib",
"main": "dist_ts/index.js",
@ -33,5 +33,6 @@
],
"browserslist": [
"last 1 chrome versions"
]
],
"dependencies": {}
}

View File

@ -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();

View File

@ -3,6 +3,6 @@
*/
export const commitinfo = {
name: '@pushrocks/smarturl',
version: '2.0.2',
version: '3.0.3',
description: 'a url parsing lib'
}

View File

@ -27,13 +27,29 @@ export class Smarturl implements IUrlObject {
searchParams?: ISearchParams;
}
): Smarturl {
const parsedUrlInstance = new plugins.url.URL(urlArg);
const parsedUrlInstance = new URL(urlArg);
const searchParams: ISearchParams = {};
// enrichment
const searchParamKeys = parsedUrlInstance.searchParams.keys();
for (const key of searchParamKeys) {
searchParams[key] = parsedUrlInstance.searchParams.get(key);
const searchParamPairs: {
key: string;
value: string;
}[] = []
if (parsedUrlInstance.search) {
parsedUrlInstance.search
.replace('?', '')
.split('&')
.map((searchParamPair) => {
searchParamPairs.push({
key: searchParamPair.split('=')[0],
value: searchParamPair.split('=')[1],
});
});
}
for (const searchParamPair of searchParamPairs) {
searchParams[searchParamPair.key] = searchParamPair.value;
}
if (optionsArg?.searchParams) {
for (const key of Object.keys(optionsArg.searchParams)) {
@ -41,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;
@ -107,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}`;
}
}

View File

@ -1,4 +1 @@
// node native
import * as url from 'url';
export { url };
export {};

View File

@ -5,6 +5,6 @@
"target": "ES2022",
"module": "ES2022",
"moduleResolution": "nodenext",
"esModuleInterop": true
"esModuleInterop": true,
}
}