Skip to content

Commit

Permalink
fix: url.ts: failed to parse scheme-less host, incorrectly prepended …
Browse files Browse the repository at this point in the history
…http scheme; +test examples
  • Loading branch information
jimmy-zhening-luo committed Nov 23, 2024
1 parent 603f2d3 commit 8e666f1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
29 changes: 24 additions & 5 deletions src/lib/object/url.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
import { expect } from "chai";
import url from "./url";

const TEST_URLS = {
const TEST = {
OK: "https://www.example.com/path/to/foo?a=1&b=2&c=3#fragment",
ERROR: "$%^&*",
cases: {
throw: {
empty: "",
http: "https://",
},
ok: {
scheme: "scriptable://",
schemeHost: "scriptable://host",
schemeHostBlank: "scriptable:///",
host: "example.com",
hostPath: "example.com/path",
},
},
},
parts = url(TEST_URLS.OK);
parts = url(TEST.OK);

describe("Object: URL", function () {
describe("shape", function () {
Expand All @@ -16,11 +29,17 @@ describe("Object: URL", function () {
});
describe("throws", function () {
it("on non-URL input", function () {
expect(() => url(TEST_URLS.ERROR))
expect(() => url(TEST.ERROR))
.throws();
expect(() => url(TEST.cases.throw.empty))
.throws();
expect(() => url(TEST.cases.throw.http))
.throws();
});
it("but not on valid URL input", function () {
expect(() => url(TEST_URLS.OK))
expect(() => url(TEST.OK))
.does.not.throw();
expect(() => Object.values(TEST.cases.ok).map(u => url(u))
.does.not.throw();

Check failure on line 43 in src/lib/object/url.spec.ts

View workflow job for this annotation

GitHub Actions / Build/Publish (dev)

Property 'does' does not exist on type '{ scheme: string; host: string; path: string; query: string; fragment: string; }[]'.

Check failure on line 43 in src/lib/object/url.spec.ts

View workflow job for this annotation

GitHub Actions / Build/Publish (dev)

')' expected.
});
});
Expand All @@ -40,7 +59,7 @@ describe("Object: URL", function () {
);
});
it("which are all strings", function () {
expect(Object.entries(parts).every(([,value]) => typeof value === "string"))
expect(Object.values(parts).every(part => typeof part === "string"))
.ok;
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/lib/object/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export default function (string: string) {
query = "",
fragment = "",
} = (/^(?:(?<scheme>[^:/?#]+):)?(?:\/\/(?<host>[^/?#]*))?(?<path>[^?#]*)(?:\?(?<query>[^#]*))?(?:#(?<fragment>.*))?/u)
.exec(`${tryHttp ? "https" : ""}${string}`)
?.groups
?? {};
.exec(`${tryHttp ? "https://" : ""}${string}`)
?.groups ?? {};

return scheme === "" || tryHttp && host === "" && path === ""
? null
Expand Down

0 comments on commit 8e666f1

Please sign in to comment.