Pattern-Collector-AnyJS

A high-performance pattern collector and ESM import statement analyzer for JavaScript, specializing in analyzing and mapping Express router imports to their registrations.

🚀 Express Auditor 🧩 Custom Regex Matching 📦 ESM Static Analyzer 🔥 Pure JS

🎮 Interactive Playgrounds

🧩 Regex Pattern Collector

Demonstrates global ES module import extraction from javascript code using custom regex filters.

Explore Regex Collector →
★ Featured (Routes Audit)

🚀 Express Route Auditor

The primary core story: analyzes routes by mapping imports directly to their registered mount paths.

Explore Routes Auditor →

🎯 Targeted Line Auditor

Uses strict line regular expressions to locate hook calls, middlewares, and application use lines.

Explore Line Auditor →

📖 Technical Reference

📦 Installation

Install the package into your project using npm:

npm install pattern-collector-anyjs

🛠️ Basic Usage

Import and invoke the default function in your ES Modules:

import patternCollector from 'pattern-collector-anyjs';

const story = patternCollector({
  fileContent: routesText,
  parseRegex: /import\s*\{[^}]*router\s+as\s+(\w+)[^}]*\}\s*from\s*['"]\.\/([^/]+)\/.*['"]/,
  searchRegex: /^[ \t]*import\b.*from\s+['"]\.[^'"]*['"];/gm
});

🛠️ API Signature Reference

The default exported function accepts a single configuration object parameter:

patternCollector({ fileContent, parseRegex, searchRegex, inShowLog })
Parameter Type Description
fileContent string The raw router file content text to scan.
parseRegex RegExp A RegExp that extracts variable (first group) and subfolder path (second group) from import matches.
searchRegex RegExp A global (/g or /gm) RegExp pattern to identify the target import lines.
inShowLog boolean Optional. Set to true to print debugging steps. Defaults to false.

⚡ Regex Configuration: JSON vs JS Modules

Storing regular expressions in standard configuration files presents a design trade-off between strict JSON structure and native JS syntax:

📄 JSON Configurations (.json)

JSON files cannot store native RegExp literals, only plain strings. This forces developers to double-escape special characters (e.g., \\s* instead of \s*) and requires the library to dynamically parse them into RegExp objects at runtime using new RegExp().

⚡ JS Module Configurations (.js)

Renaming configurations to JS modules (e.g. extractRegex.js) allows you to export native RegExp literals directly. This removes the double-escaping overhead, avoids runtime string-to-regexp compilation, and allows clean syntax like /^[ \t]*import\b/gm.

💡 Note on Compatibility: To support both worlds, the internal parser still features a robust convertToRegExp check. It automatically converts regex strings to RegExp objects on the fly, allowing consumers to use whichever setup works best for their workflow.