MCPcopy
hub / github.com/loggerhead/json4u / urlToMap

Function urlToMap

src/lib/worker/command/urlToJSON.ts:19–69  ·  view source on GitHub ↗
(s: string, maxLevel?: number)

Source from the content-addressed store, hash-verified

17}
18
19export function urlToMap(s: string, maxLevel?: number): Map<string, string | Map<string, any>> {
20 const isFullURI = isURI(s);
21 const u = new URL(isFullURI ? s : `http://json4u.com/${s.replace(/^\//, "")}`);
22 const m = new Map();
23
24 if (isFullURI) {
25 u.protocol && m.set("Protocol", u.protocol.replace(/:$/, ""));
26 u.hostname && m.set("Host", u.hostname);
27 }
28
29 u.username && m.set("Username", u.username);
30 u.password && m.set("Password", u.password);
31 u.port && m.set("Port", u.port);
32 u.pathname && m.set("Path", u.pathname);
33 u.hash && m.set("Hash", u.hash);
34
35 if (maxLevel === undefined || maxLevel > 0) {
36 const q = new Map();
37 const dups = new Map();
38
39 u.searchParams.forEach((_, name) => {
40 dups.set(name, (dups.get(name) ?? 0) + 1);
41 });
42
43 u.searchParams.forEach((value, name) => {
44 let v: string | ReturnType<typeof urlToMap> = value;
45 const lv = maxLevel !== undefined ? maxLevel - 1 : undefined;
46
47 if ((lv ?? 1) > 0 && isURI(value)) {
48 v = urlToMap(value, lv);
49 }
50
51 if (dups.get(name) > 1) {
52 const vv = q.get(name) ?? [];
53 vv.push(v);
54 q.set(name, vv);
55 } else {
56 q.set(name, v);
57 }
58 });
59
60 if (q.size > 0) {
61 m.set("Query", q);
62 }
63 } else if (u.searchParams.size > 0) {
64 const q = u.searchParams.toString();
65 m.set("Query", q);
66 }
67
68 return m;
69}
70
71function isURI(s: string) {
72 return typeof s === "string" && /^\w+:\/\/.*/g.test(s);

Callers 2

url.tsFile · 0.90
urlToJSONFunction · 0.85

Calls 4

isURIFunction · 0.85
setMethod · 0.80
toStringMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected