BREAKING CHANGE: Hopper regex mode uses ECMAScript Unicode syntax instead of Python re. Matching runs in a cancellable worker with a five-second deadline; literal mode keeps Unicode casefold matching.
28 lines
756 B
TypeScript
28 lines
756 B
TypeScript
import { parentPort, workerData } from "node:worker_threads";
|
|
import { z } from "zod";
|
|
|
|
const input = z
|
|
.strictObject({
|
|
pattern: z.string().min(1),
|
|
caseSensitive: z.boolean(),
|
|
values: z.array(z.string()),
|
|
})
|
|
.parse(workerData);
|
|
|
|
let expression: RegExp | undefined;
|
|
try {
|
|
expression = new RegExp(input.pattern, input.caseSensitive ? "u" : "iu");
|
|
} catch (cause: unknown) {
|
|
parentPort?.postMessage({
|
|
status: "invalid_pattern",
|
|
reason: cause instanceof Error ? cause.message : String(cause),
|
|
});
|
|
}
|
|
if (expression !== undefined) {
|
|
const indices = [];
|
|
for (const [index, value] of input.values.entries()) {
|
|
if (expression.test(value)) indices.push(index);
|
|
}
|
|
parentPort?.postMessage({ status: "matched", indices });
|
|
}
|