42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import type { ToolInput } from "../../core/io/input-types";
|
|
import type { ToolResult } from "../../core/io/output-types";
|
|
import type { TextCaseOptions } from "./index";
|
|
|
|
export async function runTextCase(
|
|
input: ToolInput,
|
|
options: TextCaseOptions
|
|
): Promise<ToolResult> {
|
|
const text = input.text || "";
|
|
if (!text) {
|
|
throw new Error("No text provided.");
|
|
}
|
|
|
|
// A simple tokenizer that splits by whitespace or punctuation
|
|
const words = text.match(/[A-Z]+(?![a-z])|[A-Z]?[a-z]+|\d+/g) || [];
|
|
|
|
const outputStr = (() => {
|
|
switch (options.to) {
|
|
case "UPPERCASE":
|
|
return text.toUpperCase();
|
|
case "lowercase":
|
|
return text.toLowerCase();
|
|
case "camelCase":
|
|
return words.map((w, i) => {
|
|
const lower = w.toLowerCase();
|
|
return i === 0 ? lower : lower.charAt(0).toUpperCase() + lower.slice(1);
|
|
}).join("");
|
|
case "snake_case":
|
|
return words.map(w => w.toLowerCase()).join("_");
|
|
case "kebab-case":
|
|
return words.map(w => w.toLowerCase()).join("-");
|
|
default:
|
|
return text;
|
|
}
|
|
})();
|
|
|
|
return {
|
|
type: "text",
|
|
value: outputStr,
|
|
};
|
|
}
|