MCPcopy
hub / github.com/freshframework/fresh / patternToSegments

Function patternToSegments

packages/fresh/src/router.ts:300–333  ·  view source on GitHub ↗
(
  path: string,
  root: string,
  includeLast: boolean = false,
)

Source from the content-addressed store, hash-verified

298}
299
300export function patternToSegments(
301 path: string,
302 root: string,
303 includeLast: boolean = false,
304): string[] {
305 const out: string[] = [root];
306
307 if (path === "/" || path === "*" || path === "/*") return out;
308
309 // Strip optional groups like {/:param}? before segmenting, so that
310 // /api{/:opt}?/endpoint produces the same segments as /api/endpoint.
311 // This ensures middleware registered at /api applies to routes with
312 // optional parameters under /api.
313 const cleaned = path.replace(/\{[^}]*\}\??/g, "");
314
315 let start = -1;
316 for (let i = 0; i < cleaned.length; i++) {
317 const ch = cleaned[i];
318
319 if (ch === "/") {
320 if (i > 0) {
321 const raw = cleaned.slice(start + 1, i);
322 out.push(raw);
323 }
324 start = i;
325 }
326 }
327
328 if (includeLast && start < cleaned.length - 1) {
329 out.push(cleaned.slice(start + 1));
330 }
331
332 return out;
333}
334
335export function mergePath(
336 basePath: string,

Callers 2

router_test.tsFile · 0.90
getOrCreateSegmentFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected