First implementation of Plimi
This commit is contained in:
41
src/tools/text-case/run.ts
Normal file
41
src/tools/text-case/run.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user