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
90 lines (86 loc) • 3.09 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
fuzzyRegex: () => fuzzyRegex
});
module.exports = __toCommonJS(index_exports);
// node_modules/tsup/assets/cjs_shims.js
var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
// src/tre.ts
var import_module = require("module");
var require2 = (0, import_module.createRequire)(importMetaUrl);
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, options) {
const patternString = pattern instanceof RegExp ? pattern.source : pattern;
let insensitive = true;
if (options?.caseInsensitive !== void 0) {
insensitive = options.caseInsensitive;
} else if (pattern instanceof RegExp) {
insensitive = pattern.ignoreCase;
}
if (options?.caseInsensitive !== void 0 && pattern instanceof RegExp && pattern.ignoreCase !== options.caseInsensitive) {
throw new Error("Case sensitivity mismatch");
}
const tre = new Tre(patternString, insensitive);
function getOptions(str) {
const min = Math.min(str.length, patternString.length);
const defaultMaxErrs = Math.floor(min / 10) + (min % 10 > 5 ? 1 : 0);
return {
costIns: options?.costIns ?? 1,
costDel: options?.costDel ?? 1,
costSubst: options?.costSubst ?? 1,
maxCost: options?.maxCost ?? defaultMaxErrs,
maxIns: options?.maxIns ?? defaultMaxErrs,
maxDel: options?.maxDel ?? defaultMaxErrs,
maxSubst: options?.maxSubst ?? defaultMaxErrs,
maxErr: options?.maxErr ?? defaultMaxErrs
};
}
return {
test: (str) => {
return tre.fuzzy(str, getOptions(str));
},
exec: (str) => {
return tre.fuzzyExec(str, getOptions(str));
},
toString: () => {
return patternString;
}
};
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
fuzzyRegex
});
//# sourceMappingURL=index.cjs.map