Compare commits

...

44 Commits

Author SHA1 Message Date
731e70bc0b 3.1.0 2024-10-03 12:55:37 +02:00
fac8e52a11 feat(Smarturl): Enhanced documentation and added method for property setting and chaining in Smarturl 2024-10-03 12:55:37 +02:00
ae9e12ba86 3.0.8 2024-10-03 12:49:54 +02:00
c9b40c8725 fix(core): Fix code formatting and indentation issues in smarturl.classes.smarturl.ts 2024-10-03 12:49:53 +02:00
0ab0308eb1 update description 2024-05-29 14:17:00 +02:00
ff63115b4f update tsconfig 2024-04-14 18:31:19 +02:00
0c49438942 update tsconfig 2024-04-01 21:42:05 +02:00
dfc3944258 update npmextra.json: githost 2024-04-01 20:00:01 +02:00
72fb807c59 update npmextra.json: githost 2024-03-30 21:49:01 +01:00
a8495baf4c 3.0.7 2023-07-11 17:17:19 +02:00
3a29679e66 fix(core): update 2023-07-11 17:17:19 +02:00
08d5627e7e 3.0.6 2023-04-17 14:41:24 +02:00
25a8b750d8 fix(core): update 2023-04-17 14:41:23 +02:00
5eaa7ea0c3 3.0.5 2022-08-21 13:56:35 +02:00
d255c598b4 fix(core): update 2022-08-21 13:56:34 +02:00
5bb01a2db9 3.0.4 2022-08-06 22:28:16 +02:00
44b1e67302 fix(core): update 2022-08-06 22:28:16 +02:00
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
f3e9f49af2 2.0.2 2022-07-27 00:40:18 +02:00
4b6dc9f5c3 fix(core): update 2022-07-27 00:40:18 +02:00
b84c8053ff 2.0.1 2021-05-02 16:33:50 +00:00
544f309dc9 fix(core): update 2021-05-02 16:33:50 +00:00
f6e6697244 2.0.0 2021-05-02 16:32:25 +00:00
ce6622f858 BREAKING CHANGE(core): update 2021-05-02 16:32:24 +00:00
105b1ca637 1.0.8 2021-04-13 08:10:11 +00:00
546c7806bc fix(core): update 2021-04-13 08:10:09 +00:00
d446575e60 1.0.7 2021-04-12 20:09:54 +00:00
31922529ae fix(core): update 2021-04-12 20:09:53 +00:00
fced2e845e 1.0.6 2021-04-12 20:09:20 +00:00
c7169e37a6 fix(core): update 2021-04-12 20:09:19 +00:00
04a62f6176 1.0.5 2021-04-12 19:41:03 +00:00
ea9b67a328 fix(core): update 2021-04-12 19:41:03 +00:00
dbb62a819a 1.0.4 2021-04-12 19:34:01 +00:00
9381533066 1.0.3 2021-04-12 19:18:36 +00:00
4d966147c4 fix(core): update 2021-04-12 19:18:36 +00:00
3c609edf0a 1.0.2 2020-03-27 13:40:03 +00:00
0a1bf06048 fix(core): update 2020-03-27 13:40:03 +00:00
34 changed files with 5168 additions and 2229 deletions

21
.gitignore vendored
View File

@ -1,3 +1,20 @@
.nogit/
# artifacts
coverage/
public/
pages/
# installs
node_modules/ node_modules/
.settings/
.idea/ # caches
.yarn/
.cache/
.rpt2_cache
# builds
dist/
dist_*/
# custom

128
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,128 @@
# gitzone ci_default
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
cache:
paths:
- .npmci_cache/
key: '$CI_BUILD_STAGE'
stages:
- security
- test
- release
- metadata
before_script:
- npm install -g @shipzone/npmci
# ====================
# security stage
# ====================
auditProductionDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --production --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=prod --production
tags:
- docker
allow_failure: true
auditDevDependencies:
image: registry.gitlab.com/hosttoday/ht-docker-node:npmci
stage: security
script:
- npmci npm prepare
- npmci command npm install --ignore-scripts
- npmci command npm config set registry https://registry.npmjs.org
- npmci command npm audit --audit-level=high --only=dev
tags:
- docker
allow_failure: true
# ====================
# test stage
# ====================
testStable:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci npm test
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
testBuild:
stage: test
script:
- npmci npm prepare
- npmci node install stable
- npmci npm install
- npmci command npm run build
coverage: /\d+.?\d+?\%\s*coverage/
tags:
- docker
release:
stage: release
script:
- npmci node install stable
- npmci npm publish
only:
- tags
tags:
- lossless
- docker
- notpriv
# ====================
# metadata stage
# ====================
codequality:
stage: metadata
allow_failure: true
only:
- tags
script:
- npmci command npm install -g typescript
- npmci npm prepare
- npmci npm install
tags:
- lossless
- docker
- priv
trigger:
stage: metadata
script:
- npmci trigger
only:
- tags
tags:
- lossless
- docker
- notpriv
pages:
stage: metadata
script:
- npmci node install stable
- npmci npm prepare
- npmci npm install
- npmci command npm run buildDocs
tags:
- lossless
- docker
- notpriv
only:
- tags
artifacts:
expire_in: 1 week
paths:
- public
allow_failure: true

6
.idea/vcs.xml generated
View File

@ -1,6 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View File

@ -1,20 +0,0 @@
language: node_js
before_install:
- nvm install stable
- node -v
- npm -v
- npm install -g gulp
- npm install gulp
- npm install gulp-typescript
deploy:
provider: npm
email: npm@smart-coordination.com
api_key:
secure: P0Wz+4tHBh9+DeC92ZFB8U8M8PZSfv2nQpovc6dfVuDjQ62cbozbd8w49Qf3+/TQB6Duz9ZyOIg28c25/FoJ0UhqFuVqzhOS8pn5wv3E3+i2tZSFek2MIlHJLoy2hAOVi2jqhJ504+h/ncEU9J1z71cWiDWo7Bmb5R0J+0QfsHjH8Yz/b6WBgJOsneOxp8Ne+AWr5xq1Welx9Xh9AD99frWfQM3imX0/ticOW2fiUjiebuMNIzbGiE4Z6OAcgJFNU4Ny9n6xz4B0dHGXjmAHgzMdX4P1TEjEwbhXg9hDkobPn/BKJr+ora1sf/hk4AMSaVRiw3XcxrCy77bMMJB0TQYdiMfB+ZDQ4p6orDqbazvLgGE/vlG4mdv0Xp8kHX8gfkN8tGEluOFM2d1uxV+McKnau//ak61usuuk7D5O6uiDhKLE/zfBSw89h9UCC8MFmLQ2UUVdg90lH5MVhy5xDTEuZxvifCNIDGNtIVWQ0PM251ebma2F3RqL0pafTkydfcPb/ZkR6cjKr0fwG/s3HMChYB74MfAsX5GDFw9YY20jrcnVIPWBluRelTes+ajAakjTHPpk7ZkUSH/oJGV2TKDXI08xq3CYq7aULOl46fo2oz94r1tOuUHWynTKijLPqNu/P1IcmCMqRKlpBedNxVdT2enAliE3m6DP7LVsEV4=
on:
tags: true
repo: pushrocks/smarturl
notifications:
email: false
slack:
secure: f5Uss0z9RPl/QcA/DroB8loyE93aOYI6bqCkrsiUscmZtlv/TVQtT4dxqGA6uvcG6iTQDBi3Ul88dQxWkRm4IqbhY35/iMaV2dHW4FVYMAh8GQMbsfL2sALCcufxD9blw47awv3iFcwhV1EeyesscjgL0JIjduk96v/7G/6QIO2838M1lzlgtj+kRUkim8qkaEs1je3gRrhMUIjLuAdscMXyUKYFMjWo9ACSjVUl30R/ZNemb18itIja6i92GotreBgcfEMczvy58ovDC7xdJUsY8LjMI01DwY+WPRnI0tAhsuI8moBwwcdM4e3bAjKjucQRjO33O5bMWRZ6QCiYd0DnCEFyCPQLJ4GSy/tkD00n8ijLHAOSV3AH1zNbdK1EAdSPQXDvlI36KJn/2hyQLoitGHVUPr76ujJWP82ypO2tgIp3XQU0dJVCxDuHnwJO2+hjdI+gCPqxNTpjeujHx3UdkTGNRjuuf9dlZ/D08fApjYxy2fxItTqo3QjP/nrqvBXUOPP8yPHpjIT4H2t5Pr4SJjBGI6X4qhKyFj6s9rA/Xu1rL+45zu1C3uC3z+u3T9UwrbzJ/cZM6r6UQvQmUvIfBNaMlg4I/diQCDIPL+Rhop2nylY3IcHmJnk2itn7kOqj1tohCpFEml5pRuSZy4udWywkdtyBAsHWFLF7oiQ=

11
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,11 @@
{
"version": "0.2.0",
"configurations": [
{
"command": "npm test",
"name": "Run npm test",
"request": "launch",
"type": "node-terminal"
}
]
}

26
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,26 @@
{
"json.schemas": [
{
"fileMatch": ["/npmextra.json"],
"schema": {
"type": "object",
"properties": {
"npmci": {
"type": "object",
"description": "settings for npmci"
},
"gitzone": {
"type": "object",
"description": "settings for gitzone",
"properties": {
"projectType": {
"type": "string",
"enum": ["website", "element", "service", "npm", "wcc"]
}
}
}
}
}
}
]
}

View File

@ -1,12 +0,0 @@
# smarturl
offers smart ways to handle URLs
### Buildstatus/Dependencies
[![Build Status](https://travis-ci.org/pushrocks/gulp-bootstrap.svg?branch=master)](https://travis-ci.org/pushrocks/gulp-bootstrap)
[![devDependency Status](https://david-dm.org/pushrocks/gulp-bootstrap/dev-status.svg)](https://david-dm.org/pushrocks/gulp-bootstrap#info=devDependencies)
### Usage
TODO
Cheers
Phil from Lossless Digital

52
changelog.md Normal file
View File

@ -0,0 +1,52 @@
# Changelog
## 2024-10-03 - 3.1.0 - feat(Smarturl)
Enhanced documentation and added method for property setting and chaining in Smarturl
- Added detailed comments to methods and interfaces for better code understanding.
- Introduced a method to set properties in a chainable manner.
## 2024-10-03 - 3.0.8 - fix(core)
Fix code formatting and indentation issues in smarturl.classes.smarturl.ts
- Improved code readability by fixing indentation and ensuring consistent formatting.
## 2022-07-27 - 3.0.0 to 3.0.7 - Core Updates
Maintenance and enhancement updates across multiple minor versions.
- **3.0.0**: Core updated with foundational changes.
- **3.0.1**: Further core updates with minor fixes.
- **3.0.2**: Continued enhancements and core updates.
- **3.0.3**: Additional core updates.
- **3.0.4**: Continued improvements in core functions.
- **3.0.5**: Ongoing core enhancements and updates.
- **3.0.6**: Maintenance improvements and stable updates.
## 2022-07-27 - 2.0.2 to 3.0.0 - Major Transition
Introduction of significant changes to the core.
- **2.0.2**: Introduced breaking changes with a switch to ESM (ECMAScript Modules).
## 2021-05-02 - 1.0.8 to 2.0.2 - Stability Updates
Updates focused on core stability and functionality.
- **1.0.8**: Breaking core updates were introduced to stabilize the platform.
- **2.0.0**: Core was updated for greater efficiency.
- **2.0.1**: Minor core improvements and updates.
## 2021-04-12 - 1.0.3 to 1.0.8 - Core Maintenance
Multiple updates for improving the core functionality.
- Incremental updates from 1.0.3 through 1.0.7 enhancing core stability.
## 2020-03-27 - 1.0.1 to 1.0.2 - Initial Core Enhancements
Early updates focusing on core aspects.
- Introduction of minor core improvements and fixes.
## 2015-10-21 - 0.0.1 to 0.0.4 - Project Initialization
Initial setup and foundational updates for the project.
- **0.0.1**: Project initiation and Travis integration.
- **0.0.3**: Creation of initial test infrastructure.
- **0.0.4**: Basic project setup completed.

View File

@ -1,6 +0,0 @@
/// <reference path="typings/tsd.d.ts" />
var path = require("path");
var openURL = require("open");
var smarturl = {};
smarturl.open = openURL;
module.exports = smarturl;

View File

@ -1,6 +1,4 @@
The MIT License (MIT) Copyright (c) 2020 Lossless GmbH (hello@lossless.com)
Copyright (c) 2015 Push.Rocks
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@ -19,4 +17,3 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.

31
npmextra.json Normal file
View File

@ -0,0 +1,31 @@
{
"gitzone": {
"projectType": "npm",
"module": {
"githost": "code.foss.global",
"gitscope": "push.rocks",
"gitrepo": "smarturl",
"description": "A library for parsing URLs in a detailed and flexible way.",
"npmPackagename": "@push.rocks/smarturl",
"license": "MIT",
"projectDomain": "push.rocks",
"keywords": [
"URL parsing",
"TypeScript",
"node.js",
"web development",
"URL manipulation",
"search parameters",
"HTTP",
"HTTPS"
]
}
},
"npmci": {
"npmGlobalTools": [],
"npmAccessLevel": "public"
},
"tsdoc": {
"legal": "\n## License and Legal Information\n\nThis repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. \n\n**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.\n\n### Trademarks\n\nThis project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.\n"
}
}

View File

@ -1,34 +1,53 @@
{ {
"name": "smarturl", "name": "@push.rocks/smarturl",
"version": "0.0.4", "version": "3.1.0",
"description": "offers smart ways to handle URLs", "private": false,
"main": "index.js", "description": "A library for parsing URLs in a detailed and flexible way.",
"scripts": { "main": "dist_ts/index.js",
"test": "(cd ts/compile && gulp)", "typings": "dist_ts/index.d.ts",
"reinstall": "(rm -r node_modules && npm install)", "type": "module",
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)", "author": "Lossless GmbH",
"startdev": "(git checkout master && git pull origin master)"
},
"repository": {
"type": "git",
"url": "https://github.com/pushrocks/smarturl.git"
},
"keywords": [
"json",
"jade",
"template"
],
"author": "Smart Coordination GmbH <office@push.rocks> (https://push.rocks)",
"license": "MIT", "license": "MIT",
"bugs": { "scripts": {
"url": "https://github.com/pushrocks/smarturl/issues" "test": "(tstest test/ --web)",
}, "build": "(tsbuild --web)",
"homepage": "https://github.com/pushrocks/smarturl", "buildDocs": "tsdoc"
"dependencies": {
"open": "0.0.5"
}, },
"devDependencies": { "devDependencies": {
"gulp": "3.9.0", "@gitzone/tsbuild": "^2.1.65",
"gulp-typescript": "2.9.2" "@gitzone/tsrun": "^1.2.39",
"@gitzone/tstest": "^1.0.74",
"@pushrocks/tapbundle": "^5.0.4",
"@types/node": "^18.15.11"
},
"files": [
"ts/**/*",
"ts_web/**/*",
"dist/**/*",
"dist_*/**/*",
"dist_ts/**/*",
"dist_ts_web/**/*",
"assets/**/*",
"cli.js",
"npmextra.json",
"readme.md"
],
"browserslist": [
"last 1 chrome versions"
],
"keywords": [
"URL parsing",
"TypeScript",
"node.js",
"web development",
"URL manipulation",
"search parameters",
"HTTP",
"HTTPS"
],
"homepage": "https://code.foss.global/push.rocks/smarturl",
"repository": {
"type": "git",
"url": "https://code.foss.global/push.rocks/smarturl.git"
} }
} }

4457
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

1
readme.hints.md Normal file
View File

@ -0,0 +1 @@

131
readme.md Normal file
View File

@ -0,0 +1,131 @@
# @push.rocks/smarturl
a url parsing lib
## Install
To install `@push.rocks/smarturl`, you'll need Node.js and npm on your system. Once you have those set up, run the following command in your terminal:
```bash
npm install @push.rocks/smarturl --save
```
This will add `@push.rocks/smarturl` to your project's dependencies.
## Usage
`@push.rocks/smarturl` is a library designed to simplify the parsing and manipulation of URLs within your TypeScript projects. Below is an overview of how you can leverage this library effectively in various scenarios.
### Basic URL Parsing
To start parsing URLs, you first need to import `@push.rocks/smarturl` into your TypeScript file:
```typescript
import { Smarturl } from '@push.rocks/smarturl';
```
Now, you can parse a URL string and access its components:
```typescript
const myUrl = Smarturl.createFromUrl('https://www.example.com:8080/path?query=string#hash');
console.log(myUrl.href); // https://www.example.com:8080/path?query=string#hash
console.log(myUrl.protocol); // https:
console.log(myUrl.host); // www.example.com:8080
console.log(myUrl.pathname); // /path
console.log(myUrl.search); // ?query=string
console.log(myUrl.hash); // #hash
console.log(myUrl.searchParams); // { query: 'string' }
```
### Modifying Search Parameters
`@push.rocks/smarturl` makes it easy to modify search parameters of a URL:
```typescript
// Creating a URL with initial search parameters
const myUrl = Smarturl.createFromUrl('https://www.example.com', {
searchParams: {
page: '1',
filter: 'none',
},
});
// Adding or updating search parameters
myUrl.searchParams['filter'] = 'newVal';
myUrl.searchParams['newParam'] = 'value';
console.log(myUrl.toString()); // https://www.example.com:443/?page=1&filter=newVal&newParam=value
```
### Complex URL Creation
Sometimes you may need to create a URL from parts:
```typescript
let myUrl = new Smarturl();
myUrl.protocol = 'https';
myUrl.hostname = 'www.example.com';
myUrl.port = '3000';
myUrl.pathname = '/path/to/resource';
myUrl.searchParams = {
key: 'value',
another: 'parameter'
};
console.log(myUrl.toString()); // Prints the full URL
```
### Handling Username and Password in URL
You can also include authentication details within the URL:
```typescript
const myUrl = Smarturl.createFromUrl('https://user:password@www.example.com');
console.log(myUrl.username); // user
console.log(myUrl.password); // password
```
### Working with URL Paths and Hashes
Manipulating paths and hashes is straightforward:
```typescript
const myUrl = Smarturl.createFromUrl('https://www.example.com/path/to/resource');
myUrl.hash = 'section1';
console.log(myUrl.toString()); // https://www.example.com:443/path/to/resource#section1
```
### URL Encoding and Decoding
When working with URLs, encoding and decoding of search parameters is handled automatically by the library. This ensures that the URL remains valid and interpretable by browsers and servers.
```typescript
const myUrl = Smarturl.createFromUrl('https://www.example.com');
myUrl.searchParams['redirect_uri'] = 'https://www.redirect.com/path';
console.log(myUrl.toString()); // Automatically encodes the URI
```
In addition to basic parsing and manipulation, `@push.rocks/smarturl` provides methods for deep manipulation of URLs, aiding in scenarios where complex URL operations are needed.
By understanding and utilizing these features, you can handle most URL-related tasks within your TypeScript applications with ease, ensuring that URLs are constructed, interpreted, and modified in a consistent and error-free manner.
## License and Legal Information
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository.
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
### Company Information
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

View File

@ -1,3 +0,0 @@
/// <reference path="typings/tsd.d.ts" />
var smarturl = require("./index.js");
smarturl.open("https://mojo.io");

43
test/test.both.ts Normal file
View File

@ -0,0 +1,43 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smarturl from '../ts/index.js';
let testSmarturl: smarturl.Smarturl;
tap.test('first test', async () => {
testSmarturl = new smarturl.Smarturl();
expect(testSmarturl).toBeInstanceOf(smarturl.Smarturl);
});
tap.test('should parse an URL', async () => {
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.toString());
});
tap.test('should parse an URL', async () => {
const testUrl = 'https://lossless.com:3000/';
const parsedUrl = smarturl.Smarturl.createFromUrl(testUrl, {
searchParams: {
more: 'overwritten',
},
});
console.log(parsedUrl);
console.log(parsedUrl.toString());
});
tap.test('should correctly parse ans assemble urls', async () => {
const testUrl = 'https://lossless.com/';
const parsedUrl = smarturl.Smarturl.createFromUrl(testUrl, {});
console.log(parsedUrl.toString());
expect(parsedUrl.toString()).toEqual('https://lossless.com:443/');
});
tap.start();

View File

@ -0,0 +1,17 @@
import { expect, tap } from '@pushrocks/tapbundle';
import * as smarturl from '../ts/index.js';
let testSmarturl: smarturl.Smarturl;
tap.test('first test', async () => {
testSmarturl = new smarturl.Smarturl();
expect(testSmarturl).toBeInstanceOf(smarturl.Smarturl);
});
tap.test('should create searchParams only', async () => {
const smarturlInstance = new smarturl.Smarturl();
smarturlInstance.searchParams['hello'] = 'hi_there';
console.log(smarturlInstance.toString());
})
tap.start();

8
ts/00_commitinfo_data.ts Normal file
View File

@ -0,0 +1,8 @@
/**
* autocreated commitinfo by @push.rocks/commitinfo
*/
export const commitinfo = {
name: '@push.rocks/smarturl',
version: '3.1.0',
description: 'A library for parsing URLs in a detailed and flexible way.'
}

View File

@ -1,2 +0,0 @@
nvm use v0.12.7
gulp

View File

@ -1,25 +0,0 @@
// import gulp
var gulp = require("gulp"),
gulpTypescript = require("gulp-typescript");
gulp.task('compileTS', function() {
var stream = gulp.src('../index.ts')
.pipe(gulpTypescript({
out: "index.js"
}))
.pipe(gulp.dest("../../"));
return stream;
});
gulp.task('compileTestTS', function() {
var stream = gulp.src('../test.ts')
.pipe(gulpTypescript({
out: "test.js"
}))
.pipe(gulp.dest("../../"));
return stream;
});
gulp.task('default',['compileTS','compileTestTS'], function() {
console.log('Typescript compiled');
});

View File

@ -1,2 +0,0 @@
# How to compile.
Make sure gulp and gulp-taypescript from npm are available. Then run the gulpfile in this directory.

View File

@ -1,7 +0,0 @@
/// <reference path="typings/tsd.d.ts" />
var path = require("path");
var openURL = require("open");
var smarturl = {};
smarturl.open = openURL;
module.exports = smarturl;
//# sourceMappingURL=index.js.map

View File

@ -1 +0,0 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,IAAI,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAC3B,IAAI,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AAE9B,IAAI,QAAQ,GAAO,EAAE,CAAC;AACtB,QAAQ,CAAC,IAAI,GAAG,OAAO,CAAC;AAIxB,MAAM,CAAC,OAAO,GAAG,QAAQ,CAAC"}

View File

@ -1,10 +1 @@
/// <reference path="typings/tsd.d.ts" /> export * from './smarturl.classes.smarturl.js';
var path = require("path");
var openURL = require("open");
var smarturl:any = {};
smarturl.open = openURL;
module.exports = smarturl;

View File

@ -0,0 +1,179 @@
// Import necessary plugins (if any are used in the module)
import * as plugins from './smarturl.plugins.js';
// Interface representing the structure of a URL object
export interface IUrlObject {
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;
}
// Interface representing the search parameters as a key-value pair object
export interface ISearchParams {
[key: string]: string;
}
// Main Smarturl class implementing the IUrlObject interface
export class Smarturl implements IUrlObject {
// Static method to create a Smarturl instance from a URL string
public static createFromUrl(
urlArg: string,
optionsArg?: {
searchParams?: ISearchParams;
}
): Smarturl {
// Parse the URL string using the built-in URL class
const parsedUrlInstance = new URL(urlArg);
const searchParams: ISearchParams = {};
// Array to hold key-value pairs of search parameters
const searchParamPairs: {
key: string;
value: string;
}[] = [];
// If the URL has search parameters, parse them into key-value pairs
if (parsedUrlInstance.search) {
parsedUrlInstance.search
.replace('?', '') // Remove the '?' at the beginning
.split('&') // Split the query string into individual parameters
.map((searchParamPair) => {
// Split each parameter into key and value and add to the array
searchParamPairs.push({
key: searchParamPair.split('=')[0],
value: searchParamPair.split('=')[1],
});
});
}
// Convert the array of key-value pairs into an object
for (const searchParamPair of searchParamPairs) {
searchParams[searchParamPair.key] = searchParamPair.value;
}
// Merge any additional search parameters provided in optionsArg
if (optionsArg?.searchParams) {
for (const key of Object.keys(optionsArg.searchParams)) {
searchParams[key] = optionsArg.searchParams[key];
}
}
// Reconstruct the path with updated search parameters
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]}`;
}
}
// Create an IUrlObject containing all parts of the URL
const parsedUrl: IUrlObject = {
...parsedUrlInstance, // Spread operator to include all properties from parsedUrlInstance
href: parsedUrlInstance.href,
origin: parsedUrlInstance.origin,
protocol: parsedUrlInstance.protocol,
username: parsedUrlInstance.username,
password: parsedUrlInstance.password,
host: parsedUrlInstance.host,
hostname: parsedUrlInstance.hostname,
port: parsedUrlInstance.port,
path, // Updated path with new search parameters
pathname: parsedUrlInstance.pathname,
search: parsedUrlInstance.search,
searchParams, // The searchParams object we constructed
hash: parsedUrlInstance.hash,
};
// Infer default ports if none are specified based on the protocol
if (!parsedUrl.port && parsedUrl.protocol === 'https:') {
parsedUrl.port = '443';
}
if (!parsedUrl.port && parsedUrl.protocol === 'http:') {
parsedUrl.port = '80';
}
// Create a new Smarturl instance and assign the parsed URL properties
const returnSmarturl = new Smarturl();
Object.assign(returnSmarturl, parsedUrl); // Copy all properties from parsedUrl to returnSmarturl
return returnSmarturl;
}
// Static method to create a Smarturl instance from an existing IUrlObject
public static createFromParsedUrl(parsedUrlArg: IUrlObject): Smarturl {
const returnSmarturl = new Smarturl();
Object.assign(returnSmarturl, parsedUrlArg); // Copy all properties from parsedUrlArg to returnSmarturl
return returnSmarturl;
}
// INSTANCE PROPERTIES (matching IUrlObject interface)
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 initializes searchParams as an empty object
constructor() {
this.searchParams = {};
}
// Method to create an independent clone of the current Smarturl instance
clone(): Smarturl {
const clonedInstance = new Smarturl();
Object.assign(clonedInstance, this); // Copy all properties to the new instance
clonedInstance.searchParams = { ...this.searchParams }; // Shallow copy of searchParams to prevent shared references
return clonedInstance;
}
/**
* Typed method to set a property and return the instance for chaining.
* @param prop - The property name to set (must be a key of Smarturl)
* @param value - The value to assign to the property
* @returns The Smarturl instance for method chaining
*/
set<K extends keyof this>(prop: K, value: this[K]): this {
this[prop] = value; // Set the property to the new value
return this; // Return the instance for chaining
}
// Method to convert the Smarturl instance back into a URL string
toString(): string {
let userpart = ``; // Initialize the user part of the URL (username and password)
// Construct the user part based on the presence of username and password
if (this.username && !this.password) {
userpart = `${this.username}@`;
}
if (this.username && this.password) {
userpart = `${this.username}:${this.password}@`;
}
// Construct and return the full URL string
return `${this.protocol}//${userpart}${this.hostname}:${this.port}${this.path}`;
}
}

1
ts/smarturl.plugins.ts Normal file
View File

@ -0,0 +1 @@
export {};

View File

@ -1,4 +0,0 @@
/// <reference path="typings/tsd.d.ts" />
var smarturl = require("./index.js");
smarturl.open("https://mojo.io");
//# sourceMappingURL=test.js.map

View File

@ -1 +0,0 @@
{"version":3,"file":"test.js","sourceRoot":"","sources":["test.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,IAAI,QAAQ,GAAG,OAAO,CAAC,YAAY,CAAC,CAAC;AACrC,QAAQ,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC"}

View File

@ -1,3 +0,0 @@
/// <reference path="typings/tsd.d.ts" />
var smarturl = require("./index.js");
smarturl.open("https://mojo.io");

View File

@ -1,12 +0,0 @@
{
"version": "v4",
"repo": "borisyankov/DefinitelyTyped",
"ref": "master",
"path": "typings",
"bundle": "typings/tsd.d.ts",
"installed": {
"node/node.d.ts": {
"commit": "efa0c1196d7280640e624ac1e7fa604502e7bd63"
}
}
}

File diff suppressed because it is too large Load Diff

1
ts/typings/tsd.d.ts vendored
View File

@ -1 +0,0 @@
/// <reference path="node/node.d.ts" />

14
tsconfig.json Normal file
View File

@ -0,0 +1,14 @@
{
"compilerOptions": {
"experimentalDecorators": true,
"useDefineForClassFields": false,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"verbatimModuleSyntax": true
},
"exclude": [
"dist_*/**/*.d.ts"
]
}