mirror of
https://github.com/openclaw/openclaw.git
synced 2026-06-05 10:02:56 +00:00
refactor: extract normalization core package
Extract shared normalization/coercion helpers into private @openclaw/normalization-core workspace package while preserving existing plugin SDK helper subpaths.\n\nAlso keeps direct normalization-core imports internal, wires UI/build/loader resolution, and replaces the slow PR network CodeQL lane with a fast added-line boundary scan while retaining full CodeQL for scheduled/manual runs.\n\nVerification: local moved tests, plugin SDK boundary tests, extension loader tests, agents-support shard, UI build/test, build artifacts, lint, workflow guards, autoreview, and GitHub CI passed on PR head 963d893715.
This commit is contained in:
committed by
GitHub
parent
94814f3516
commit
00d8d7ead0
117
packages/normalization-core/src/string-normalization.ts
Normal file
117
packages/normalization-core/src/string-normalization.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { normalizeOptionalLowercaseString, normalizeOptionalString } from "./string-coerce.js";
|
||||
|
||||
export function normalizeStringEntries(list?: ReadonlyArray<unknown>) {
|
||||
return (list ?? []).map((entry) => normalizeOptionalString(String(entry)) ?? "").filter(Boolean);
|
||||
}
|
||||
|
||||
export function normalizeStringEntriesLower(list?: ReadonlyArray<unknown>) {
|
||||
return normalizeStringEntries(list).map((entry) => normalizeOptionalLowercaseString(entry) ?? "");
|
||||
}
|
||||
|
||||
export function uniqueValues<T>(values: Iterable<T>): T[] {
|
||||
return [...new Set(values)];
|
||||
}
|
||||
|
||||
export function uniqueStrings(values: Iterable<string>): string[] {
|
||||
return uniqueValues(values);
|
||||
}
|
||||
|
||||
export function sortUniqueStrings(values: Iterable<string>): string[] {
|
||||
return uniqueStrings(values).toSorted((left, right) =>
|
||||
left < right ? -1 : left > right ? 1 : 0,
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeUniqueStringEntries(values?: Iterable<unknown>): string[] {
|
||||
return uniqueStrings(normalizeStringEntries(values ? [...values] : undefined));
|
||||
}
|
||||
|
||||
export function normalizeUniqueStringEntriesLower(values?: Iterable<unknown>): string[] {
|
||||
return uniqueStrings(
|
||||
normalizeStringEntriesLower(values ? [...values] : undefined).filter(Boolean),
|
||||
);
|
||||
}
|
||||
|
||||
export function normalizeSortedUniqueStringEntries(values?: Iterable<unknown>): string[] {
|
||||
return sortUniqueStrings(normalizeUniqueStringEntries(values));
|
||||
}
|
||||
|
||||
export function normalizeTrimmedStringList(value: unknown): string[] {
|
||||
if (!Array.isArray(value)) {
|
||||
return [];
|
||||
}
|
||||
return value.flatMap((entry) => {
|
||||
const normalized = normalizeOptionalString(entry);
|
||||
return normalized ? [normalized] : [];
|
||||
});
|
||||
}
|
||||
|
||||
export function normalizeUniqueTrimmedStringList(value: unknown): string[] {
|
||||
return uniqueStrings(normalizeTrimmedStringList(value));
|
||||
}
|
||||
|
||||
export function normalizeSortedUniqueTrimmedStringList(value: unknown): string[] {
|
||||
return sortUniqueStrings(normalizeTrimmedStringList(value));
|
||||
}
|
||||
|
||||
export function normalizeOptionalTrimmedStringList(value: unknown): string[] | undefined {
|
||||
const normalized = normalizeTrimmedStringList(value);
|
||||
return normalized.length > 0 ? normalized : undefined;
|
||||
}
|
||||
|
||||
export function normalizeArrayBackedTrimmedStringList(value: unknown): string[] | undefined {
|
||||
if (!Array.isArray(value)) {
|
||||
return undefined;
|
||||
}
|
||||
return normalizeTrimmedStringList(value);
|
||||
}
|
||||
|
||||
export function normalizeSingleOrTrimmedStringList(value: unknown): string[] {
|
||||
if (Array.isArray(value)) {
|
||||
return normalizeTrimmedStringList(value);
|
||||
}
|
||||
const normalized = normalizeOptionalString(value);
|
||||
return normalized ? [normalized] : [];
|
||||
}
|
||||
|
||||
export function normalizeUniqueSingleOrTrimmedStringList(value: unknown): string[] {
|
||||
return uniqueStrings(normalizeSingleOrTrimmedStringList(value));
|
||||
}
|
||||
|
||||
export function normalizeCsvOrLooseStringList(value: unknown): string[] {
|
||||
if (Array.isArray(value)) {
|
||||
return normalizeStringEntries(value);
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
return value
|
||||
.split(",")
|
||||
.map((entry) => entry.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function normalizeSlugInput(raw?: string | null) {
|
||||
return (normalizeOptionalLowercaseString(raw) ?? "").normalize("NFC");
|
||||
}
|
||||
|
||||
export function normalizeHyphenSlug(raw?: string | null) {
|
||||
const trimmed = normalizeSlugInput(raw);
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
const dashed = trimmed.replace(/\s+/g, "-");
|
||||
const cleaned = dashed.replace(/[^\p{L}\p{M}\p{N}#@._+-]+/gu, "-");
|
||||
return cleaned.replace(/-{2,}/g, "-").replace(/^[-.]+|[-.]+$/g, "");
|
||||
}
|
||||
|
||||
export function normalizeAtHashSlug(raw?: string | null) {
|
||||
const trimmed = normalizeSlugInput(raw);
|
||||
if (!trimmed) {
|
||||
return "";
|
||||
}
|
||||
const withoutPrefix = trimmed.replace(/^[@#]+/, "");
|
||||
const dashed = withoutPrefix.replace(/[\s_]+/g, "-");
|
||||
const cleaned = dashed.replace(/[^\p{L}\p{M}\p{N}-]+/gu, "-");
|
||||
return cleaned.replace(/-{2,}/g, "-").replace(/^-+|-+$/g, "");
|
||||
}
|
||||
Reference in New Issue
Block a user