mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 17:10:25 +00:00
refactor(browser): unify fill field normalization
This commit is contained in:
32
src/browser/form-fields.ts
Normal file
32
src/browser/form-fields.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { BrowserFormField } from "./client-actions-core.js";
|
||||
|
||||
export const DEFAULT_FILL_FIELD_TYPE = "text";
|
||||
|
||||
type BrowserFormFieldValue = NonNullable<BrowserFormField["value"]>;
|
||||
|
||||
export function normalizeBrowserFormFieldRef(value: unknown): string {
|
||||
return typeof value === "string" ? value.trim() : "";
|
||||
}
|
||||
|
||||
export function normalizeBrowserFormFieldType(value: unknown): string {
|
||||
const type = typeof value === "string" ? value.trim() : "";
|
||||
return type || DEFAULT_FILL_FIELD_TYPE;
|
||||
}
|
||||
|
||||
export function normalizeBrowserFormFieldValue(value: unknown): BrowserFormFieldValue | undefined {
|
||||
return typeof value === "string" || typeof value === "number" || typeof value === "boolean"
|
||||
? value
|
||||
: undefined;
|
||||
}
|
||||
|
||||
export function normalizeBrowserFormField(
|
||||
record: Record<string, unknown>,
|
||||
): BrowserFormField | null {
|
||||
const ref = normalizeBrowserFormFieldRef(record.ref);
|
||||
if (!ref) {
|
||||
return null;
|
||||
}
|
||||
const type = normalizeBrowserFormFieldType(record.type);
|
||||
const value = normalizeBrowserFormFieldValue(record.value);
|
||||
return value === undefined ? { ref, type } : { ref, type, value };
|
||||
}
|
||||
Reference in New Issue
Block a user