From 935ca39945bfa2f66fd1439991f7d5dddd939737 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sun, 15 Feb 2026 05:05:42 +0000 Subject: [PATCH] refactor(auto-reply): share directive arg parsing --- src/auto-reply/reply/directive-parsing.ts | 40 +++++++++++++++++++++++ src/auto-reply/reply/exec/directive.ts | 30 +++-------------- src/auto-reply/reply/queue/directive.ts | 30 +++-------------- 3 files changed, 50 insertions(+), 50 deletions(-) create mode 100644 src/auto-reply/reply/directive-parsing.ts diff --git a/src/auto-reply/reply/directive-parsing.ts b/src/auto-reply/reply/directive-parsing.ts new file mode 100644 index 0000000000..1576a2b3bf --- /dev/null +++ b/src/auto-reply/reply/directive-parsing.ts @@ -0,0 +1,40 @@ +export function skipDirectiveArgPrefix(raw: string): number { + let i = 0; + const len = raw.length; + while (i < len && /\s/.test(raw[i])) { + i += 1; + } + if (raw[i] === ":") { + i += 1; + while (i < len && /\s/.test(raw[i])) { + i += 1; + } + } + return i; +} + +export function takeDirectiveToken( + raw: string, + startIndex: number, +): { token: string | null; nextIndex: number } { + let i = startIndex; + const len = raw.length; + while (i < len && /\s/.test(raw[i])) { + i += 1; + } + if (i >= len) { + return { token: null, nextIndex: i }; + } + const start = i; + while (i < len && !/\s/.test(raw[i])) { + i += 1; + } + if (start === i) { + return { token: null, nextIndex: i }; + } + const token = raw.slice(start, i); + while (i < len && /\s/.test(raw[i])) { + i += 1; + } + return { token, nextIndex: i }; +} diff --git a/src/auto-reply/reply/exec/directive.ts b/src/auto-reply/reply/exec/directive.ts index 44fdfeda8f..abdb19e9b6 100644 --- a/src/auto-reply/reply/exec/directive.ts +++ b/src/auto-reply/reply/exec/directive.ts @@ -1,4 +1,5 @@ import type { ExecAsk, ExecHost, ExecSecurity } from "../../../infra/exec-approvals.js"; +import { skipDirectiveArgPrefix, takeDirectiveToken } from "../directive-parsing.js"; type ExecDirectiveParse = { cleaned: string; @@ -48,17 +49,8 @@ function parseExecDirectiveArgs(raw: string): Omit< > & { consumed: number; } { - let i = 0; const len = raw.length; - while (i < len && /\s/.test(raw[i])) { - i += 1; - } - if (raw[i] === ":") { - i += 1; - while (i < len && /\s/.test(raw[i])) { - i += 1; - } - } + let i = skipDirectiveArgPrefix(raw); let consumed = i; let execHost: ExecHost | undefined; let execSecurity: ExecSecurity | undefined; @@ -75,21 +67,9 @@ function parseExecDirectiveArgs(raw: string): Omit< let invalidNode = false; const takeToken = (): string | null => { - if (i >= len) { - return null; - } - const start = i; - while (i < len && !/\s/.test(raw[i])) { - i += 1; - } - if (start === i) { - return null; - } - const token = raw.slice(start, i); - while (i < len && /\s/.test(raw[i])) { - i += 1; - } - return token; + const res = takeDirectiveToken(raw, i); + i = res.nextIndex; + return res.token; }; const splitToken = (token: string): { key: string; value: string } | null => { diff --git a/src/auto-reply/reply/queue/directive.ts b/src/auto-reply/reply/queue/directive.ts index 9621d2fafc..1a22746c88 100644 --- a/src/auto-reply/reply/queue/directive.ts +++ b/src/auto-reply/reply/queue/directive.ts @@ -1,5 +1,6 @@ import type { QueueDropPolicy, QueueMode } from "./types.js"; import { parseDurationMs } from "../../../cli/parse-duration.js"; +import { skipDirectiveArgPrefix, takeDirectiveToken } from "../directive-parsing.js"; import { normalizeQueueDropPolicy, normalizeQueueMode } from "./normalize.js"; function parseQueueDebounce(raw?: string): number | undefined { @@ -45,17 +46,8 @@ function parseQueueDirectiveArgs(raw: string): { rawDrop?: string; hasOptions: boolean; } { - let i = 0; const len = raw.length; - while (i < len && /\s/.test(raw[i])) { - i += 1; - } - if (raw[i] === ":") { - i += 1; - while (i < len && /\s/.test(raw[i])) { - i += 1; - } - } + let i = skipDirectiveArgPrefix(raw); let consumed = i; let queueMode: QueueMode | undefined; let queueReset = false; @@ -68,21 +60,9 @@ function parseQueueDirectiveArgs(raw: string): { let rawDrop: string | undefined; let hasOptions = false; const takeToken = (): string | null => { - if (i >= len) { - return null; - } - const start = i; - while (i < len && !/\s/.test(raw[i])) { - i += 1; - } - if (start === i) { - return null; - } - const token = raw.slice(start, i); - while (i < len && /\s/.test(raw[i])) { - i += 1; - } - return token; + const res = takeDirectiveToken(raw, i); + i = res.nextIndex; + return res.token; }; while (i < len) { const token = takeToken();