Skip to content

Commit 9605cd8

Browse files
authored
refactor: move mergePaths() to utils.ts (#2905)
1 parent c6d4c47 commit 9605cd8

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

src/app.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
77
import * as colors from "@std/fmt/colors";
88
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
99
import { FreshReqContext } from "./context.ts";
10-
import {
11-
mergePaths,
12-
type Method,
13-
type Router,
14-
UrlPatternRouter,
15-
} from "./router.ts";
10+
import { type Method, type Router, UrlPatternRouter } from "./router.ts";
1611
import {
1712
type FreshConfig,
1813
normalizeConfig,
@@ -22,6 +17,7 @@ import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
2217
import type { ServerIslandRegistry } from "./context.ts";
2318
import { FinishSetup, ForgotBuild } from "./finish_setup.tsx";
2419
import { HttpError } from "./error.ts";
20+
import { mergePaths } from "./utils.ts";
2521

2622
// TODO: Completed type clashes in older Deno versions
2723
// deno-lint-ignore no-explicit-any

src/router.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,6 @@ export interface Router<T> {
2828

2929
export const IS_PATTERN = /[*:{}+?()]/;
3030

31-
export function mergePaths(a: string, b: string) {
32-
if (a === "" || a === "/" || a === "/*") return b;
33-
if (b === "/") return a;
34-
if (a.endsWith("/")) {
35-
return a.slice(0, -1) + b;
36-
} else if (!b.startsWith("/")) {
37-
return a + "/" + b;
38-
}
39-
return a + b;
40-
}
41-
4231
export class UrlPatternRouter<T> implements Router<T> {
4332
readonly _routes: Route<T>[] = [];
4433
readonly _middlewares: T[] = [];

src/router_test.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { expect } from "@std/expect";
2-
import {
3-
IS_PATTERN,
4-
mergePaths,
5-
pathToPattern,
6-
UrlPatternRouter,
7-
} from "./router.ts";
2+
import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts";
83

94
Deno.test("IS_PATTERN", () => {
105
expect(IS_PATTERN.test("/foo")).toEqual(false);
@@ -176,12 +171,3 @@ Deno.test("pathToPattern", async (t) => {
176171
expect(() => pathToPattern("foo/[[name]]-bar")).toThrow();
177172
});
178173
});
179-
180-
Deno.test("mergePaths", () => {
181-
expect(mergePaths("", "")).toEqual("");
182-
expect(mergePaths("/", "/foo")).toEqual("/foo");
183-
expect(mergePaths("/*", "/foo")).toEqual("/foo");
184-
expect(mergePaths("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
185-
expect(mergePaths("/foo/bar/", "/baz")).toEqual("/foo/bar/baz");
186-
expect(mergePaths("/foo/bar", "baz")).toEqual("/foo/bar/baz");
187-
});

src/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,22 @@ export function assertInDir(
1313
throw new Error(`Path "${tmp}" resolved outside of "${dir}"`);
1414
}
1515
}
16+
17+
/**
18+
* Joins two path segments into a single normalized path.
19+
* @example
20+
* ```ts
21+
* mergePaths("/api", "users"); // "/api/users"
22+
* mergePaths("/api/", "/users"); // "/api/users"
23+
* mergePaths("/", "/users"); // "/users"
24+
* mergePaths("", "/users"); // "/users"
25+
* mergePaths("/api", "/users"); // "/api/users"
26+
* ```
27+
*/
28+
export function mergePaths(a: string, b: string) {
29+
if (a === "" || a === "/" || a === "/*") return b;
30+
if (b === "/") return a;
31+
if (a.endsWith("/")) return a.slice(0, -1) + b;
32+
if (!b.startsWith("/")) return a + "/" + b;
33+
return a + b;
34+
}

src/utils_test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect } from "@std/expect";
2+
import { mergePaths } from "./utils.ts";
3+
4+
Deno.test("mergePaths", () => {
5+
expect(mergePaths("", "")).toEqual("");
6+
expect(mergePaths("/", "/foo")).toEqual("/foo");
7+
expect(mergePaths("/*", "/foo")).toEqual("/foo");
8+
expect(mergePaths("/foo/bar", "/baz")).toEqual("/foo/bar/baz");
9+
expect(mergePaths("/foo/bar/", "/baz")).toEqual("/foo/bar/baz");
10+
expect(mergePaths("/foo/bar", "baz")).toEqual("/foo/bar/baz");
11+
});

0 commit comments

Comments
 (0)