pattern-collector-anyjs-matches
v1.3.9
Published
A high-performance pattern matching and line tracking utility for JavaScript/ESM, wrapping pattern-collector.
Maintainers
Readme
pattern-collector-anyjs-matches 🔍
A high-performance pattern matching and line tracking utility for JavaScript/ESM, wrapping pattern-collector.
🔗 Quick Links:
- 📦 NPM Registry: npmjs.com/package/pattern-collector-anyjs-matches
- 💻 GitHub Repo: github.com/keshavsoft/pattern-collector-anyjs-matches
- 📄 Interactive Docs: keshavsoft.github.io/pattern-collector-anyjs-matches
- ⚙️ Publish Workflow: .github/workflows/npm-publish.yml
📖 Overview
pattern-collector-anyjs-matches is a lightweight wrapper around pattern-collector. It accepts a file content string and a regular expression, runs the regex match, and returns all matches along with line contents and 1-indexed line numbers.
This utility serves as the parsing step for extracting imports, function usage, and other patterns downstream.
✨ Features
- ⚡ Zero External Dependencies: Relying only on the lightweight
pattern-collectorcore. - 🏷️ Line & Line Number Tracking: Automatically calculates the exact line number where each match occurred.
- 📦 ESM Native: Built for modern ES module environments.
🚀 Installation
npm install pattern-collector-anyjs-matches🔗 Dependency Chain
pattern-collector- listed inpackage.jsonas^1.5.10.
🛠️ API Reference
default(options)
The default export is a function that collects all occurrences of a search regex in the file.
Parameters
An options object containing:
fileContent(string): The raw text or code content to search.searchRegex(RegExp): A regular expression with the global (g) flag to match patterns in the content.showLog(boolean)(optional): Whether to log debugging output.
Returns
Object[]: An array of matches found. If no matches are found, it returns an empty array.match(string): The exact matched substring.line(string): The full line of code containing the match.lineNumber(number): The 1-indexed line number in the source file.
💻 Usage Example
import getAllMatches from 'pattern-collector-anyjs-matches';
const code = `
import { exec } from "child_process";
import dotenv from 'dotenv';
`;
const matches = getAllMatches({
fileContent: code,
searchRegex: /import\s+[\s\S]*?\s+from\s+['"][^'"]+['"]/g
});
console.log(matches);
/*
Output:
[
{
match: 'import { exec } from "child_process"',
line: 'import { exec } from "child_process";',
lineNumber: 2
},
{
match: "import dotenv from 'dotenv'",
line: "import dotenv from 'dotenv';",
lineNumber: 3
}
]
*/⚖️ License
MIT License. Designed with ❤️ by KeshavSoft.
