pattern-collector-base-regex 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 KeshavSoft
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to do so, subject to the
10
+ following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,152 @@
1
+ # pattern-collector-routesjs-import-extract 🔍
2
+
3
+ > **A high-performance ESM import statement analyzer that extracts Express router imports specifically from `routes.js` files.**
4
+
5
+ [![npm version](https://img.shields.io/npm/v/pattern-collector-routesjs-import-extract.svg?style=flat-square&color=38bdf8)](https://www.npmjs.com/package/pattern-collector-routesjs-import-extract)
6
+ [![license](https://img.shields.io/npm/l/pattern-collector-routesjs-import-extract.svg?style=flat-square&color=34d399)](LICENSE)
7
+
8
+ 🔗 **Quick Links:**
9
+ * 📦 **NPM Registry**: [npmjs.com/package/pattern-collector-routesjs-import-extract](https://www.npmjs.com/package/pattern-collector-routesjs-import-extract)
10
+ * 💻 **GitHub Repo**: [github.com/keshavsoft/pattern-collector-routesjs-import-extract](https://github.com/keshavsoft/pattern-collector-routesjs-import-extract)
11
+ * 📄 **Interactive Docs**: [keshavsoft.github.io/pattern-collector-routesjs-import-extract](https://keshavsoft.github.io/pattern-collector-routesjs-import-extract/)
12
+
13
+ ---
14
+
15
+ ## 📖 Overview
16
+
17
+ `pattern-collector-routesjs-import-extract` is a lightweight utility to parse and extract structured information from ESM import statements. It scans file contents and locates named Express router imports structured like:
18
+
19
+ ```javascript
20
+ import { router as routerFromv1 } from "./v1/routes.js";
21
+ ```
22
+
23
+ > [!IMPORTANT]
24
+ > **Parser Regex Contract**
25
+ > The current parser is intentionally regex-based. In `bin/v7/index.js`, `parseRegex` captures only named imports with `router as <alias>` from a one-level relative folder path:
26
+ >
27
+ > ```javascript
28
+ > /import\s*\{[^}]*router\s+as\s+(\w+)[^}]*\}\s*from\s*['"]\.\/([^/]+)\/.*['"]/
29
+ > ```
30
+ >
31
+ > This means the import must start with `./`, must include one folder after `./`, and must contain `router as alias` inside `{ ... }`. The parser captures the alias as `variable` and the first folder as `folderName`. Package imports such as `from "express"` are not handled here.
32
+
33
+ ---
34
+
35
+ ## ✨ Features
36
+
37
+ * **⚡ Zero Dependencies**: Light, fast, and secure.
38
+ * **📂 Focused Relative Route Extraction**: Extracts router imports pointing to `./<folder>/...`.
39
+ * **📦 ESM Native**: Built for modern ES module environments.
40
+ * **🏷️ Structured Outputs**: Returns variables, folders, line contents, and matching line numbers.
41
+
42
+ ---
43
+
44
+ ## 🚀 Installation
45
+
46
+ ```bash
47
+ npm install pattern-collector-routesjs-import-extract
48
+ ```
49
+
50
+ ---
51
+
52
+ ## 🔗 Dependency Chain
53
+
54
+ This package depends on the route-import collector below. If parser behavior changes upstream, check this dependency first:
55
+
56
+ * [`pattern-collector-routesjs-import`](https://www.npmjs.com/package/pattern-collector-routesjs-import) - listed in [`package.json`](package.json) as `^1.4.7`.
57
+
58
+ ---
59
+
60
+ ## 🛠️ API Reference
61
+
62
+ ### How Matching Works
63
+
64
+ The collector has two important stages:
65
+
66
+ 1. [`pattern-collector-routesjs-import`](https://www.npmjs.com/package/pattern-collector-routesjs-import) first finds relative import lines.
67
+ 2. This package then applies `parseRegex` to keep only imports shaped like:
68
+
69
+ ```javascript
70
+ import { router as routerFromv1 } from "./v1/routes.js";
71
+ import { router as routerFromdoctors } from "./doctors/end-points.js";
72
+ ```
73
+
74
+ Both lines match the current v7 parser because the path starts with `./<folder>/`. The filename after that folder is not restricted by `parseRegex`.
75
+
76
+ These do not match:
77
+
78
+ ```javascript
79
+ import express from "express"; // npm/package import
80
+ import routerFromv1 from "./v1/routes.js"; // default import, no router as alias
81
+ import { router } from "./v1/routes.js"; // no alias
82
+ import { router as routerFromv1 } from "../v1/routes.js"; // starts with ../, not ./
83
+ ```
84
+
85
+ ### `default(options)`
86
+
87
+ #### Parameters
88
+
89
+ An options object containing:
90
+
91
+ * **`fileContent`** `(string)`: The raw JavaScript file/code content to analyze.
92
+ * **`inShowLog`** `(boolean)` (optional): Set to `true` to log collected matches to the console.
93
+
94
+ #### Returns
95
+
96
+ * `(Object[])`: An array of matches, where each match has the structure:
97
+ * `variable` `(string)`: The imported router alias (e.g. `routerFromv1`).
98
+ * `folderName` `(string)`: The subdirectory name (e.g. `v1`).
99
+ * `line` `(string)`: The complete matching import line.
100
+ * `lineNumber` `(number)`: The line number in the source file.
101
+
102
+ ---
103
+
104
+ ## 💻 Usage Example
105
+
106
+ ```javascript
107
+ import routeImportExtract from 'pattern-collector-routesjs-import-extract';
108
+
109
+ const code = `
110
+ import express from 'express';
111
+
112
+ import { router as routerFromv1 } from "./v1/routes.js";
113
+ import { router as routerFromv2 } from "./v2/routes.js";
114
+ import { router as routerFromdoctors } from "./doctors/end-points.js";
115
+ `;
116
+
117
+ const results = routeImportExtract({
118
+ fileContent: code,
119
+ inShowLog: false
120
+ });
121
+
122
+ console.log(results);
123
+ /*
124
+ Output:
125
+ [
126
+ {
127
+ variable: 'routerFromv1',
128
+ folderName: 'v1',
129
+ line: 'import { router as routerFromv1 } from "./v1/routes.js";',
130
+ lineNumber: 4
131
+ },
132
+ {
133
+ variable: 'routerFromv2',
134
+ folderName: 'v2',
135
+ line: 'import { router as routerFromv2 } from "./v2/routes.js";',
136
+ lineNumber: 5
137
+ },
138
+ {
139
+ variable: 'routerFromdoctors',
140
+ folderName: 'doctors',
141
+ line: 'import { router as routerFromdoctors } from "./doctors/end-points.js";',
142
+ lineNumber: 6
143
+ }
144
+ ]
145
+ */
146
+ ```
147
+
148
+ ---
149
+
150
+ ## ⚖️ License
151
+
152
+ MIT License. Designed with ❤️ by [KeshavSoft](https://github.com/keshavsoft).
@@ -0,0 +1,13 @@
1
+ import fs from "fs";
2
+ import path from "path";
3
+ import { fileURLToPath } from "url";
4
+
5
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
6
+
7
+ export default function getLatestVersion() {
8
+ const versions = fs.readdirSync(path.join(__dirname, ".."))
9
+ .filter(n => /^v\d+$/.test(n))
10
+ .sort((a, b) => parseInt(a.slice(1)) - parseInt(b.slice(1)));
11
+
12
+ return versions.at(-1);
13
+ };
@@ -0,0 +1,18 @@
1
+ const parseRegex = /import\s*\{[^}]*router\s+as\s+(\w+)[^}]*\}\s*from\s*['"]\.\/([^/]+)\/.*['"]/;
2
+
3
+ const startFunc = ({ matchLine, inShowLog }) => {
4
+ if (inShowLog) console.log("matchLine : ", matchLine);
5
+
6
+ const clean = matchLine.replace(/[\r\n]/g, '');
7
+
8
+ const parts = clean.match(parseRegex);
9
+
10
+ if (parts) {
11
+ return {
12
+ variable: parts[1],
13
+ folderName: parts[2]
14
+ };
15
+ };
16
+ };
17
+
18
+ export default startFunc;
@@ -0,0 +1,16 @@
1
+ const startFunc = ({ matchLine, parseRegex, showLog }) => {
2
+ if (showLog) console.log("matchLine : ", matchLine);
3
+
4
+ const clean = matchLine.replace(/[\r\n]/g, '');
5
+
6
+ const parts = clean.match(parseRegex);
7
+
8
+ if (parts) {
9
+ return {
10
+ variable: parts[1],
11
+ folderName: parts[2]
12
+ };
13
+ };
14
+ };
15
+
16
+ export default startFunc;
package/index.js ADDED
@@ -0,0 +1,14 @@
1
+ import { createRequire } from "module";
2
+ import getLatestVersion from "./bin/core/getLatestVersion.js";
3
+
4
+ const require = createRequire(import.meta.url);
5
+
6
+ const v = getLatestVersion();
7
+ const latestModule = require(`./bin/${v}/index.js`);
8
+
9
+ const load = ({ matchLine, parseRegex, showLog }) => {
10
+
11
+ return latestModule.default({ matchLine, parseRegex, showLog });
12
+ };
13
+
14
+ export default load;
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "pattern-collector-base-regex",
3
+ "version": "1.2.1",
4
+ "description": "A high-performance pattern collector and ESM import statement analyzer.",
5
+ "keywords": [
6
+ "pattern",
7
+ "regex",
8
+ "parser",
9
+ "import",
10
+ "analyzer",
11
+ "esm",
12
+ "extractor"
13
+ ],
14
+ "dependencies": {
15
+ },
16
+ "type": "module",
17
+ "exports": {
18
+ ".": "./index.js"
19
+ },
20
+ "files": [
21
+ "bin/",
22
+ "index.js",
23
+ "README.md",
24
+ "LICENSE"
25
+ ],
26
+ "homepage": "https://github.com/keshavsoft/pattern-collector-base-regex#readme",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/keshavsoft/pattern-collector-base-regex"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/keshavsoft/pattern-collector-base-regex/issues"
33
+ }
34
+ }