UNPKG

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

87 lines (83 loc) 2.91 kB
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, 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); } }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { fuzzyRegex }); //# sourceMappingURL=index.cjs.map