fuzzy-regex
Version:
A regular expression library for Node.js that allows for a configurable number of mismatches (fuzzy matching), powered by the high-performance [TRE](https://laurikari.net/tre/) regex engine. This package supports both ESM and CommonJS, and provides a simp
57 lines (56 loc) • 1.58 kB
JavaScript
// src/tre.ts
import { createRequire } from "module";
var require2 = createRequire(import.meta.url);
var path = "./tre.node";
var addon = require2(path);
var TreLib;
if (typeof addon === "object") {
TreLib = addon.Tre;
}
if (typeof addon === "string") {
TreLib = require2(addon).Tre;
}
if (!TreLib) {
throw new Error("Failed to load tre");
}
var Tre = TreLib;
// src/index.ts
function fuzzyRegex(pattern, caseInsensitive) {
const patternString = pattern instanceof RegExp ? pattern.source : pattern;
let insensitive = true;
if (caseInsensitive !== void 0) {
insensitive = caseInsensitive;
} else if (pattern instanceof RegExp) {
insensitive = pattern.ignoreCase;
}
if (caseInsensitive !== void 0 && pattern instanceof RegExp && pattern.ignoreCase !== caseInsensitive) {
throw new Error("Case sensitivity mismatch");
}
const tre = new Tre(patternString, insensitive);
return {
test: (str, maxErrors) => {
let errs = 0;
if (maxErrors === void 0) {
const min = Math.min(str.length, patternString.length);
errs = Math.floor(min / 5) + (min % 5 > 2 ? 1 : 0);
} else {
errs = maxErrors;
}
return tre.fuzzy(str, errs);
},
exec: (str, maxErrors) => {
let errs = 0;
if (maxErrors === void 0) {
const min = Math.min(str.length, patternString.length);
errs = Math.floor(min / 5) + (min % 5 > 2 ? 1 : 0);
} else {
errs = maxErrors;
}
return tre.fuzzyExec(str, errs);
}
};
}
export {
fuzzyRegex
};
//# sourceMappingURL=index.js.map