mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-03-19 07:54:23 +01:00
Run oxfmt across repo, add format script and docs
Add .oxfmtignore to skip generated bindings and wasm-pack output. Add npm format script, update DEVELOPMENT.md for Vite+ toolchain, and format all non-generated files with oxfmt. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
{
|
||||
"name": "@yaak/template-function-regex",
|
||||
"displayName": "Regex Template Functions",
|
||||
"description": "Template functions for working with regular expressions",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"description": "Template functions for working with regular expressions",
|
||||
"scripts": {
|
||||
"build": "yaakcli build",
|
||||
"dev": "yaakcli dev",
|
||||
|
||||
@@ -1,73 +1,73 @@
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from '@yaakapp/api';
|
||||
import type { TemplateFunctionArg } from '@yaakapp-internal/plugins';
|
||||
import type { CallTemplateFunctionArgs, Context, PluginDefinition } from "@yaakapp/api";
|
||||
import type { TemplateFunctionArg } from "@yaakapp-internal/plugins";
|
||||
|
||||
const inputArg: TemplateFunctionArg = {
|
||||
type: 'text',
|
||||
name: 'input',
|
||||
label: 'Input Text',
|
||||
type: "text",
|
||||
name: "input",
|
||||
label: "Input Text",
|
||||
multiLine: true,
|
||||
};
|
||||
|
||||
const regexArg: TemplateFunctionArg = {
|
||||
type: 'text',
|
||||
name: 'regex',
|
||||
label: 'Regular Expression',
|
||||
placeholder: '\\w+',
|
||||
defaultValue: '.*',
|
||||
type: "text",
|
||||
name: "regex",
|
||||
label: "Regular Expression",
|
||||
placeholder: "\\w+",
|
||||
defaultValue: ".*",
|
||||
description:
|
||||
'A JavaScript regular expression. Use a capture group to reference parts of the match in the replacement.',
|
||||
"A JavaScript regular expression. Use a capture group to reference parts of the match in the replacement.",
|
||||
};
|
||||
|
||||
export const plugin: PluginDefinition = {
|
||||
templateFunctions: [
|
||||
{
|
||||
name: 'regex.match',
|
||||
description: 'Extract text using a regular expression',
|
||||
name: "regex.match",
|
||||
description: "Extract text using a regular expression",
|
||||
args: [inputArg, regexArg],
|
||||
previewArgs: [regexArg.name],
|
||||
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const input = String(args.values.input ?? '');
|
||||
const regex = new RegExp(String(args.values.regex ?? ''));
|
||||
const input = String(args.values.input ?? "");
|
||||
const regex = new RegExp(String(args.values.regex ?? ""));
|
||||
|
||||
const match = input.match(regex);
|
||||
return match?.groups
|
||||
? (Object.values(match.groups)[0] ?? '')
|
||||
: (match?.[1] ?? match?.[0] ?? '');
|
||||
? (Object.values(match.groups)[0] ?? "")
|
||||
: (match?.[1] ?? match?.[0] ?? "");
|
||||
},
|
||||
},
|
||||
{
|
||||
name: 'regex.replace',
|
||||
description: 'Replace text using a regular expression',
|
||||
name: "regex.replace",
|
||||
description: "Replace text using a regular expression",
|
||||
previewArgs: [regexArg.name],
|
||||
args: [
|
||||
inputArg,
|
||||
regexArg,
|
||||
{
|
||||
type: 'text',
|
||||
name: 'replacement',
|
||||
label: 'Replacement Text',
|
||||
placeholder: 'hello $1',
|
||||
type: "text",
|
||||
name: "replacement",
|
||||
label: "Replacement Text",
|
||||
placeholder: "hello $1",
|
||||
description:
|
||||
'The replacement text. Use $1, $2, ... to reference capture groups or $& to reference the entire match.',
|
||||
"The replacement text. Use $1, $2, ... to reference capture groups or $& to reference the entire match.",
|
||||
},
|
||||
{
|
||||
type: 'text',
|
||||
name: 'flags',
|
||||
label: 'Flags',
|
||||
placeholder: 'g',
|
||||
defaultValue: 'g',
|
||||
type: "text",
|
||||
name: "flags",
|
||||
label: "Flags",
|
||||
placeholder: "g",
|
||||
defaultValue: "g",
|
||||
optional: true,
|
||||
description:
|
||||
'Regular expression flags (g for global, i for case-insensitive, m for multiline, etc.)',
|
||||
"Regular expression flags (g for global, i for case-insensitive, m for multiline, etc.)",
|
||||
},
|
||||
],
|
||||
async onRender(_ctx: Context, args: CallTemplateFunctionArgs): Promise<string | null> {
|
||||
const input = String(args.values.input ?? '');
|
||||
const replacement = String(args.values.replacement ?? '');
|
||||
const flags = String(args.values.flags || '');
|
||||
const input = String(args.values.input ?? "");
|
||||
const replacement = String(args.values.replacement ?? "");
|
||||
const flags = String(args.values.flags || "");
|
||||
const regex = String(args.values.regex);
|
||||
|
||||
if (!regex) return '';
|
||||
if (!regex) return "";
|
||||
|
||||
return input.replace(new RegExp(String(args.values.regex), flags), replacement);
|
||||
},
|
||||
|
||||
@@ -1,196 +1,196 @@
|
||||
import type { Context } from '@yaakapp/api';
|
||||
import { describe, expect, it } from 'vite-plus/test';
|
||||
import { plugin } from '../src';
|
||||
import type { Context } from "@yaakapp/api";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
import { plugin } from "../src";
|
||||
|
||||
describe('regex.match', () => {
|
||||
const matchFunction = plugin.templateFunctions?.find((f) => f.name === 'regex.match');
|
||||
describe("regex.match", () => {
|
||||
const matchFunction = plugin.templateFunctions?.find((f) => f.name === "regex.match");
|
||||
|
||||
it('should exist', () => {
|
||||
it("should exist", () => {
|
||||
expect(matchFunction).toBeDefined();
|
||||
});
|
||||
|
||||
it('should extract first capture group', async () => {
|
||||
it("should extract first capture group", async () => {
|
||||
const result = await matchFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'Hello (\\w+)',
|
||||
input: 'Hello World',
|
||||
regex: "Hello (\\w+)",
|
||||
input: "Hello World",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('World');
|
||||
expect(result).toBe("World");
|
||||
});
|
||||
|
||||
it('should extract named capture group', async () => {
|
||||
it("should extract named capture group", async () => {
|
||||
const result = await matchFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'Hello (?<name>\\w+)',
|
||||
input: 'Hello World',
|
||||
regex: "Hello (?<name>\\w+)",
|
||||
input: "Hello World",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('World');
|
||||
expect(result).toBe("World");
|
||||
});
|
||||
|
||||
it('should return full match when no capture groups', async () => {
|
||||
it("should return full match when no capture groups", async () => {
|
||||
const result = await matchFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'Hello \\w+',
|
||||
input: 'Hello World',
|
||||
regex: "Hello \\w+",
|
||||
input: "Hello World",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('Hello World');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
|
||||
it('should return empty string when no match', async () => {
|
||||
it("should return empty string when no match", async () => {
|
||||
const result = await matchFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'Goodbye',
|
||||
input: 'Hello World',
|
||||
regex: "Goodbye",
|
||||
input: "Hello World",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('');
|
||||
expect(result).toBe("");
|
||||
});
|
||||
|
||||
it('should return empty string when regex is empty', async () => {
|
||||
it("should return empty string when regex is empty", async () => {
|
||||
const result = await matchFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: '',
|
||||
input: 'Hello World',
|
||||
regex: "",
|
||||
input: "Hello World",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('');
|
||||
expect(result).toBe("");
|
||||
});
|
||||
|
||||
it('should return empty string when input is empty', async () => {
|
||||
it("should return empty string when input is empty", async () => {
|
||||
const result = await matchFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'Hello',
|
||||
input: '',
|
||||
regex: "Hello",
|
||||
input: "",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('');
|
||||
expect(result).toBe("");
|
||||
});
|
||||
});
|
||||
|
||||
describe('regex.replace', () => {
|
||||
const replaceFunction = plugin.templateFunctions?.find((f) => f.name === 'regex.replace');
|
||||
describe("regex.replace", () => {
|
||||
const replaceFunction = plugin.templateFunctions?.find((f) => f.name === "regex.replace");
|
||||
|
||||
it('should exist', () => {
|
||||
it("should exist", () => {
|
||||
expect(replaceFunction).toBeDefined();
|
||||
});
|
||||
|
||||
it('should replace one occurrence by default', async () => {
|
||||
it("should replace one occurrence by default", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'o',
|
||||
input: 'Hello World',
|
||||
replacement: 'a',
|
||||
regex: "o",
|
||||
input: "Hello World",
|
||||
replacement: "a",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('Hella World');
|
||||
expect(result).toBe("Hella World");
|
||||
});
|
||||
|
||||
it('should replace with capture groups', async () => {
|
||||
it("should replace with capture groups", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: '(\\w+) (\\w+)',
|
||||
input: 'Hello World',
|
||||
replacement: '$2 $1',
|
||||
regex: "(\\w+) (\\w+)",
|
||||
input: "Hello World",
|
||||
replacement: "$2 $1",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('World Hello');
|
||||
expect(result).toBe("World Hello");
|
||||
});
|
||||
|
||||
it('should replace with full match reference', async () => {
|
||||
it("should replace with full match reference", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'World',
|
||||
input: 'Hello World',
|
||||
replacement: '[$&]',
|
||||
regex: "World",
|
||||
input: "Hello World",
|
||||
replacement: "[$&]",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('Hello [World]');
|
||||
expect(result).toBe("Hello [World]");
|
||||
});
|
||||
|
||||
it('should respect flags parameter', async () => {
|
||||
it("should respect flags parameter", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'hello',
|
||||
input: 'Hello World',
|
||||
replacement: 'Hi',
|
||||
flags: 'i',
|
||||
regex: "hello",
|
||||
input: "Hello World",
|
||||
replacement: "Hi",
|
||||
flags: "i",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('Hi World');
|
||||
expect(result).toBe("Hi World");
|
||||
});
|
||||
|
||||
it('should handle empty replacement', async () => {
|
||||
it("should handle empty replacement", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'World',
|
||||
input: 'Hello World',
|
||||
replacement: '',
|
||||
regex: "World",
|
||||
input: "Hello World",
|
||||
replacement: "",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('Hello ');
|
||||
expect(result).toBe("Hello ");
|
||||
});
|
||||
|
||||
it('should return original input when no match', async () => {
|
||||
it("should return original input when no match", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'Goodbye',
|
||||
input: 'Hello World',
|
||||
replacement: 'Hi',
|
||||
regex: "Goodbye",
|
||||
input: "Hello World",
|
||||
replacement: "Hi",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('Hello World');
|
||||
expect(result).toBe("Hello World");
|
||||
});
|
||||
|
||||
it('should return empty string when regex is empty', async () => {
|
||||
it("should return empty string when regex is empty", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: '',
|
||||
input: 'Hello World',
|
||||
replacement: 'Hi',
|
||||
regex: "",
|
||||
input: "Hello World",
|
||||
replacement: "Hi",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('');
|
||||
expect(result).toBe("");
|
||||
});
|
||||
|
||||
it('should return empty string when input is empty', async () => {
|
||||
it("should return empty string when input is empty", async () => {
|
||||
const result = await replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: 'Hello',
|
||||
input: '',
|
||||
replacement: 'Hi',
|
||||
regex: "Hello",
|
||||
input: "",
|
||||
replacement: "Hi",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
expect(result).toBe('');
|
||||
expect(result).toBe("");
|
||||
});
|
||||
|
||||
it('should throw on invalid regex', async () => {
|
||||
it("should throw on invalid regex", async () => {
|
||||
const fn = replaceFunction?.onRender({} as Context, {
|
||||
values: {
|
||||
regex: '[',
|
||||
input: 'Hello World',
|
||||
replacement: 'Hi',
|
||||
regex: "[",
|
||||
input: "Hello World",
|
||||
replacement: "Hi",
|
||||
},
|
||||
purpose: 'send',
|
||||
purpose: "send",
|
||||
});
|
||||
await expect(fn).rejects.toThrow(
|
||||
'Invalid regular expression: /[/: Unterminated character class',
|
||||
"Invalid regular expression: /[/: Unterminated character class",
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user