mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 08:11:26 +00:00
refactor: consolidate duplicate utility functions (#12439)
* refactor: consolidate duplicate utility functions - Add escapeRegExp to src/utils.ts and remove 10 local duplicates - Rename bash-tools clampNumber to clampWithDefault (different signature) - Centralize formatError calls to use formatErrorMessage from infra/errors.ts - Re-export formatErrorMessage from cli/cli-utils.ts to preserve API * refactor: consolidate remaining escapeRegExp duplicates * refactor: consolidate sleep, stripAnsi, and clamp duplicates
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { BrowserRouteContext } from "../server-context.js";
|
||||
import type { BrowserRequest, BrowserResponse, BrowserRouteRegistrar } from "./types.js";
|
||||
import { escapeRegExp } from "../../utils.js";
|
||||
import { registerBrowserRoutes } from "./index.js";
|
||||
|
||||
type BrowserDispatchRequest = {
|
||||
@@ -22,10 +23,6 @@ type RouteEntry = {
|
||||
handler: (req: BrowserRequest, res: BrowserResponse) => void | Promise<void>;
|
||||
};
|
||||
|
||||
function escapeRegex(value: string) {
|
||||
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
function compileRoute(path: string): { regex: RegExp; paramNames: string[] } {
|
||||
const paramNames: string[] = [];
|
||||
const parts = path.split("/").map((part) => {
|
||||
@@ -34,7 +31,7 @@ function compileRoute(path: string): { regex: RegExp; paramNames: string[] } {
|
||||
paramNames.push(name);
|
||||
return "([^/]+)";
|
||||
}
|
||||
return escapeRegex(part);
|
||||
return escapeRegExp(part);
|
||||
});
|
||||
return { regex: new RegExp(`^${parts.join("/")}$`), paramNames };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user