A high-performance pattern collector and ESM import statement analyzer for JavaScript, specializing in analyzing and mapping Express router imports to their registrations.
Demonstrates global ES module import extraction from javascript code using custom regex filters.
The primary core story: analyzes routes by mapping imports directly to their registered mount paths.
Uses strict line regular expressions to locate hook calls, middlewares, and application use lines.
Install the package into your project using npm:
npm install pattern-collector-anyjs
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
});
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. |
Storing regular expressions in standard configuration files presents a design trade-off between strict JSON structure and native JS syntax:
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().
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.
convertToRegExp check. It automatically converts regex strings to RegExp objects on the fly, allowing consumers to use whichever setup works best for their workflow.