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

1 lines 4.64 kB
{"version":3,"sources":["../src/index.ts","../node_modules/tsup/assets/cjs_shims.js","../src/tre.ts"],"sourcesContent":["import { Tre } from \"./tre\";\n\nexport type FuzzyRegex = {\n test: (str: string) => boolean;\n exec: (str: string) => string[] | null;\n toString: () => string;\n};\n\nexport type FuzzyRegexOptions = {\n caseInsensitive?: boolean;\n costIns?: number;\n costDel?: number;\n costSubst?: number;\n maxCost?: number;\n maxIns?: number;\n maxDel?: number;\n maxSubst?: number;\n maxErr?: number;\n};\n\nexport function fuzzyRegex(\n pattern: string | RegExp,\n options?: FuzzyRegexOptions\n): FuzzyRegex {\n const patternString = pattern instanceof RegExp ? pattern.source : pattern;\n let insensitive = true;\n if (options?.caseInsensitive !== undefined) {\n insensitive = options.caseInsensitive;\n } else if (pattern instanceof RegExp) {\n insensitive = pattern.ignoreCase;\n }\n\n if (\n options?.caseInsensitive !== undefined &&\n pattern instanceof RegExp &&\n pattern.ignoreCase !== options.caseInsensitive\n ) {\n throw new Error(\"Case sensitivity mismatch\");\n }\n\n const tre = new Tre(patternString, insensitive);\n\n function getOptions(str: string): {\n costIns: number;\n costDel: number;\n costSubst: number;\n maxCost: number;\n maxIns: number;\n maxDel: number;\n maxSubst: number;\n maxErr: number;\n } {\n const min = Math.min(str.length, patternString.length);\n const defaultMaxErrs = Math.floor(min / 10) + (min % 10 > 5 ? 1 : 0);\n return {\n costIns: options?.costIns ?? 1,\n costDel: options?.costDel ?? 1,\n costSubst: options?.costSubst ?? 1,\n maxCost: options?.maxCost ?? defaultMaxErrs,\n maxIns: options?.maxIns ?? defaultMaxErrs,\n maxDel: options?.maxDel ?? defaultMaxErrs,\n maxSubst: options?.maxSubst ?? defaultMaxErrs,\n maxErr: options?.maxErr ?? defaultMaxErrs,\n };\n }\n\n return {\n test: (str: string): boolean => {\n return tre.fuzzy(str, getOptions(str));\n },\n exec: (str: string): string[] | null => {\n return tre.fuzzyExec(str, getOptions(str));\n },\n toString: (): string => {\n return patternString;\n },\n };\n}\n","// Shim globals in cjs bundle\n// There's a weird bug that esbuild will always inject importMetaUrl\n// if we export it as `const importMetaUrl = ... __filename ...`\n// But using a function will not cause this issue\n\nconst getImportMetaUrl = () =>\n typeof document === 'undefined'\n ? new URL(`file:${__filename}`).href\n : (document.currentScript && document.currentScript.src) ||\n new URL('main.js', document.baseURI).href\n\nexport const importMetaUrl = /* @__PURE__ */ getImportMetaUrl()\n","import { TreClass } from \"./types\";\nimport { createRequire } from \"module\";\n\nconst require = createRequire(import.meta.url);\nconst path = \"./tre.node\";\nconst addon = require(path);\n\nlet TreLib: typeof TreClass | undefined;\n\nif (typeof addon === \"object\") {\n TreLib = addon.Tre;\n}\nif (typeof addon === \"string\") {\n TreLib = require(addon).Tre;\n}\n\nif (!TreLib) {\n throw new Error(\"Failed to load tre\");\n}\n\nexport const Tre = TreLib;\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACKA,IAAM,mBAAmB,MACvB,OAAO,aAAa,cAChB,IAAI,IAAI,QAAQ,UAAU,EAAE,EAAE,OAC7B,SAAS,iBAAiB,SAAS,cAAc,OAClD,IAAI,IAAI,WAAW,SAAS,OAAO,EAAE;AAEpC,IAAM,gBAAgC,iCAAiB;;;ACV9D,oBAA8B;AAE9B,IAAMA,eAAU,6BAAc,aAAe;AAC7C,IAAM,OAAO;AACb,IAAM,QAAQA,SAAQ,IAAI;AAE1B,IAAI;AAEJ,IAAI,OAAO,UAAU,UAAU;AAC7B,WAAS,MAAM;AACjB;AACA,IAAI,OAAO,UAAU,UAAU;AAC7B,WAASA,SAAQ,KAAK,EAAE;AAC1B;AAEA,IAAI,CAAC,QAAQ;AACX,QAAM,IAAI,MAAM,oBAAoB;AACtC;AAEO,IAAM,MAAM;;;AFAZ,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