quasiurl 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # quasiurl
2
+
3
+ *`URL`-like object for templated URLs*
4
+
5
+ Unlike `URL`, `QuasiURL`:
6
+ - can have an empty `origin`, `pathname`, `hostname`, `protocol`;
7
+ - preserves templating characters without URL-encoding them;
8
+ - doesn't implement the entire `URL` spec.
package/dist/index.js ADDED
@@ -0,0 +1,31 @@
1
+ // index.ts
2
+ var QuasiURL = class {
3
+ hash;
4
+ hostname;
5
+ href;
6
+ origin;
7
+ pathname;
8
+ protocol;
9
+ search;
10
+ constructor(url) {
11
+ let head = url.match(/^((\w+:)?\/\/([^/]+))(\/.*|$)/);
12
+ let tail = url.match(/(\?[^#]+)(#.+)?$/);
13
+ let origin = head?.[1] ?? "";
14
+ let pathname = url.replace(/^((\w+:)?\/\/[^/]+)/, "").replace(/\?.*$/, "").replace(/#.*$/, "");
15
+ let search = tail?.[1] ?? "";
16
+ let hash = tail?.[2] ?? "";
17
+ this.origin = origin;
18
+ this.pathname = pathname;
19
+ this.search = search;
20
+ this.hash = hash;
21
+ this.href = `${origin}${pathname}${search}${hash}`;
22
+ this.hostname = head?.[3] ?? "";
23
+ this.protocol = head?.[2] ?? "";
24
+ }
25
+ toString() {
26
+ return this.href;
27
+ }
28
+ };
29
+ export {
30
+ QuasiURL
31
+ };
package/dist/tests.js ADDED
@@ -0,0 +1,111 @@
1
+ // index.ts
2
+ var QuasiURL = class {
3
+ hash;
4
+ hostname;
5
+ href;
6
+ origin;
7
+ pathname;
8
+ protocol;
9
+ search;
10
+ constructor(url2) {
11
+ let head = url2.match(/^((\w+:)?\/\/([^/]+))(\/.*|$)/);
12
+ let tail = url2.match(/(\?[^#]+)(#.+)?$/);
13
+ let origin = head?.[1] ?? "";
14
+ let pathname = url2.replace(/^((\w+:)?\/\/[^/]+)/, "").replace(/\?.*$/, "").replace(/#.*$/, "");
15
+ let search = tail?.[1] ?? "";
16
+ let hash = tail?.[2] ?? "";
17
+ this.origin = origin;
18
+ this.pathname = pathname;
19
+ this.search = search;
20
+ this.hash = hash;
21
+ this.href = `${origin}${pathname}${search}${hash}`;
22
+ this.hostname = head?.[3] ?? "";
23
+ this.protocol = head?.[2] ?? "";
24
+ }
25
+ toString() {
26
+ return this.href;
27
+ }
28
+ };
29
+
30
+ // tests.ts
31
+ var k = 0;
32
+ function assert(predicate) {
33
+ let n = `00${++k}`.slice(-3);
34
+ if (predicate) console.log(n, "passed");
35
+ else {
36
+ console.error(n, "failed");
37
+ console.error();
38
+ console.error("[!] failed");
39
+ process.exit(1);
40
+ }
41
+ }
42
+ function createQuasiURL(url2) {
43
+ console.log(`
44
+ ${JSON.stringify(url2)}`);
45
+ return new QuasiURL(url2);
46
+ }
47
+ var url = createQuasiURL("/sections/:id");
48
+ assert(url.origin === "");
49
+ assert(url.pathname === "/sections/:id");
50
+ assert(url.search === "");
51
+ assert(url.hash === "");
52
+ assert(url.href === "/sections/:id");
53
+ assert(url.hostname === "");
54
+ assert(url.protocol === "");
55
+ url = createQuasiURL("/x{/:name}");
56
+ assert(url.origin === "");
57
+ assert(url.pathname === "/x{/:name}");
58
+ assert(url.search === "");
59
+ assert(url.hash === "");
60
+ assert(url.href === "/x{/:name}");
61
+ assert(url.hostname === "");
62
+ assert(url.protocol === "");
63
+ url = createQuasiURL("/x?a=1&b=nnn#start");
64
+ assert(url.origin === "");
65
+ assert(url.pathname === "/x");
66
+ assert(url.search === "?a=1&b=nnn");
67
+ assert(url.hash === "#start");
68
+ assert(url.href === "/x?a=1&b=nnn#start");
69
+ assert(url.hostname === "");
70
+ assert(url.protocol === "");
71
+ url = createQuasiURL("https://a.aa/chapter/1");
72
+ assert(url.origin === "https://a.aa");
73
+ assert(url.pathname === "/chapter/1");
74
+ assert(url.search === "");
75
+ assert(url.hash === "");
76
+ assert(url.href === "https://a.aa/chapter/1");
77
+ assert(url.hostname === "a.aa");
78
+ assert(url.protocol === "https:");
79
+ url = createQuasiURL("https://a.aa");
80
+ assert(url.origin === "https://a.aa");
81
+ assert(url.pathname === "");
82
+ assert(url.search === "");
83
+ assert(url.hash === "");
84
+ assert(url.href === "https://a.aa");
85
+ assert(url.hostname === "a.aa");
86
+ assert(url.protocol === "https:");
87
+ url = createQuasiURL("");
88
+ assert(url.origin === "");
89
+ assert(url.pathname === "");
90
+ assert(url.search === "");
91
+ assert(url.hash === "");
92
+ assert(url.href === "");
93
+ assert(url.hostname === "");
94
+ assert(url.protocol === "");
95
+ url = createQuasiURL("?x=321&y=567#start");
96
+ assert(url.origin === "");
97
+ assert(url.pathname === "");
98
+ assert(url.search === "?x=321&y=567");
99
+ assert(url.hash === "#start");
100
+ assert(url.href === "?x=321&y=567#start");
101
+ assert(url.hostname === "");
102
+ assert(url.protocol === "");
103
+ url = createQuasiURL("x/y?a=1");
104
+ assert(url.origin === "");
105
+ assert(url.pathname === "x/y");
106
+ assert(url.search === "?a=1");
107
+ assert(url.hash === "");
108
+ assert(url.href === "x/y?a=1");
109
+ assert(url.hostname === "");
110
+ assert(url.protocol === "");
111
+ console.log("\npassed");
package/index.ts ADDED
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Unlike URL, QuasiURL:
3
+ * - can have an empty `origin`, `pathname`, `hostname`, `protocol`;
4
+ * - preserves templating characters without URL-encoding them.
5
+ */
6
+ export class QuasiURL {
7
+ hash: string;
8
+ hostname: string;
9
+ href: string;
10
+ origin: string;
11
+ pathname: string;
12
+ protocol: string;
13
+ search: string;
14
+
15
+ constructor(url: string) {
16
+ let head = url.match(/^((\w+:)?\/\/([^/]+))(\/.*|$)/);
17
+ let tail = url.match(/(\?[^#]+)(#.+)?$/);
18
+
19
+ let origin = head?.[1] ?? '';
20
+ let pathname = url
21
+ .replace(/^((\w+:)?\/\/[^/]+)/, '')
22
+ .replace(/\?.*$/, '')
23
+ .replace(/#.*$/, '');
24
+ let search = tail?.[1] ?? '';
25
+ let hash = tail?.[2] ?? '';
26
+
27
+ this.origin = origin;
28
+ this.pathname = pathname;
29
+ this.search = search;
30
+ this.hash = hash;
31
+
32
+ this.href = `${origin}${pathname}${search}${hash}`;
33
+ this.hostname = head?.[3] ?? '';
34
+ this.protocol = head?.[2] ?? '';
35
+ }
36
+ toString() {
37
+ return this.href;
38
+ }
39
+ }
package/package.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "name": "quasiurl",
3
+ "version": "1.0.0",
4
+ "description": "URL for templating",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "scripts": {
8
+ "build": "npx npm-run-all clean -p compile compile-tests",
9
+ "clean": "node -e \"require('node:fs').rmSync('dist', {force: true, recursive: true});\"",
10
+ "compile": "npx esbuild index.ts --bundle --outdir=dist --platform=neutral",
11
+ "compile-tests": "npx esbuild tests.ts --bundle --outdir=dist --platform=neutral",
12
+ "prepublishOnly": "npm run build",
13
+ "preversion": "npx npm-run-all shape build test",
14
+ "shape": "npx codeshape",
15
+ "test": "node dist/tests.js"
16
+ },
17
+ "author": "axtk",
18
+ "license": "ISC",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "git://github.com/axtk/quasiurl.git"
22
+ },
23
+ "keywords": [
24
+ "url",
25
+ "template"
26
+ ],
27
+ "devDependencies": {
28
+ "@types/node": "^24.0.4"
29
+ }
30
+ }
package/tests.ts ADDED
@@ -0,0 +1,102 @@
1
+ import {QuasiURL} from '.';
2
+
3
+ let k = 0;
4
+
5
+ function assert(predicate: boolean) {
6
+ let n = `00${++k}`.slice(-3);
7
+
8
+ if (predicate) console.log(n, 'passed');
9
+ else {
10
+ console.error(n, 'failed');
11
+ console.error();
12
+ console.error('[!] failed');
13
+ process.exit(1);
14
+ }
15
+ }
16
+
17
+ function createQuasiURL(url: string) {
18
+ console.log(`\n${JSON.stringify(url)}`);
19
+ return new QuasiURL(url);
20
+ }
21
+
22
+ let url = createQuasiURL('/sections/:id');
23
+
24
+ assert(url.origin === '');
25
+ assert(url.pathname === '/sections/:id');
26
+ assert(url.search === '');
27
+ assert(url.hash === '');
28
+ assert(url.href === '/sections/:id');
29
+ assert(url.hostname === '');
30
+ assert(url.protocol === '');
31
+
32
+ url = createQuasiURL('/x{/:name}');
33
+
34
+ assert(url.origin === '');
35
+ assert(url.pathname === '/x{/:name}');
36
+ assert(url.search === '');
37
+ assert(url.hash === '');
38
+ assert(url.href === '/x{/:name}');
39
+ assert(url.hostname === '');
40
+ assert(url.protocol === '');
41
+
42
+ url = createQuasiURL('/x?a=1&b=nnn#start');
43
+
44
+ assert(url.origin === '');
45
+ assert(url.pathname === '/x');
46
+ assert(url.search === '?a=1&b=nnn');
47
+ assert(url.hash === '#start');
48
+ assert(url.href === '/x?a=1&b=nnn#start');
49
+ assert(url.hostname === '');
50
+ assert(url.protocol === '');
51
+
52
+ url = createQuasiURL('https://a.aa/chapter/1');
53
+
54
+ assert(url.origin === 'https://a.aa');
55
+ assert(url.pathname === '/chapter/1');
56
+ assert(url.search === '');
57
+ assert(url.hash === '');
58
+ assert(url.href === 'https://a.aa/chapter/1');
59
+ assert(url.hostname === 'a.aa');
60
+ assert(url.protocol === 'https:');
61
+
62
+ url = createQuasiURL('https://a.aa');
63
+
64
+ assert(url.origin === 'https://a.aa');
65
+ assert(url.pathname === '');
66
+ assert(url.search === '');
67
+ assert(url.hash === '');
68
+ assert(url.href === 'https://a.aa');
69
+ assert(url.hostname === 'a.aa');
70
+ assert(url.protocol === 'https:');
71
+
72
+ url = createQuasiURL('');
73
+
74
+ assert(url.origin === '');
75
+ assert(url.pathname === '');
76
+ assert(url.search === '');
77
+ assert(url.hash === '');
78
+ assert(url.href === '');
79
+ assert(url.hostname === '');
80
+ assert(url.protocol === '');
81
+
82
+ url = createQuasiURL('?x=321&y=567#start');
83
+
84
+ assert(url.origin === '');
85
+ assert(url.pathname === '');
86
+ assert(url.search === '?x=321&y=567');
87
+ assert(url.hash === '#start');
88
+ assert(url.href === '?x=321&y=567#start');
89
+ assert(url.hostname === '');
90
+ assert(url.protocol === '');
91
+
92
+ url = createQuasiURL('x/y?a=1');
93
+
94
+ assert(url.origin === '');
95
+ assert(url.pathname === 'x/y');
96
+ assert(url.search === '?a=1');
97
+ assert(url.hash === '');
98
+ assert(url.href === 'x/y?a=1');
99
+ assert(url.hostname === '');
100
+ assert(url.protocol === '');
101
+
102
+ console.log('\npassed');
package/tsconfig.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "include": [
3
+ "index.ts",
4
+ "tests.ts"
5
+ ],
6
+ "compilerOptions": {
7
+ "lib": ["ESNext", "DOM"],
8
+ "target": "ESNext",
9
+ "outDir": "dist",
10
+ "moduleResolution": "node",
11
+ "strict": true,
12
+ "noUnusedLocals": true,
13
+ "noUnusedParameters": true
14
+ }
15
+ }