js-dev-tool 1.1.2 → 1.1.3

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.
@@ -1,14 +1,41 @@
1
1
 
2
2
  # literate-regex
3
3
 
4
- Write readable (literate) regex sources with `#` comments, and normalize them into a compact JavaScript `RegExp.source` **at the type level**.
4
+ ## Motivation
5
5
 
6
- This package is built for the real world where regexes are often:
7
- - long,
8
- - unreadable,
9
- - and fragile.
6
+ Regular expressions are absurdly powerful a tiny automaton you can carry in your pocket.
7
+ But once a regex grows beyond “a few tokens and a prayer”, it becomes:
10
8
 
11
- With `literate-regex`, you can keep them readable (multi-line + comments) while still getting a normalized, single-line `RegExp.source` type that stays stable.
9
+ - hard to read
10
+ - easy to break
11
+ - painful to maintain
12
+
13
+ This library exists to keep regexes **literate**.
14
+
15
+ Write a multi-line, commented, PCRE-style regex source (with `#` notes), then normalize it into a compact JavaScript `RegExp.source` **while preserving the normalized source as a TypeScript string literal type**.
16
+
17
+ That gives you two superpowers:
18
+
19
+ 1. **Human-friendly editing** (readable formatting + comments)
20
+ 2. **Machine-friendly safety** (typed, normalized sources that flow through your codebase)
21
+
22
+ In short: fewer regex jump-scares, more confidence.
23
+
24
+ ### Before
25
+ ```ts
26
+ const re = /^(?:\s*\/\*\*\s+|\s+\*?\s+)(?:(?=@(...))|...)/gm;
27
+ ```
28
+
29
+ ### After
30
+ ```ts
31
+ const RE_SOURCE = `
32
+ /^ # start
33
+ (?: ... ) # jsdoc start
34
+ ... # more notes
35
+ /gm` as const;
36
+
37
+ const re = compilePCREStyleRegExpLiteral(RE_SOURCE);
38
+ ```
12
39
 
13
40
  ---
14
41
 
@@ -96,15 +123,13 @@ const src = `
96
123
  const normalized = normalizePCREStyleSource(src);
97
124
  ```
98
125
 
99
- ## helper function compilePCREStyleRegExpLiteral
100
-
126
+ ## 🔧 Runtime creation Compile PCRE Style RegExpLiteral
101
127
 
102
128
  ```ts
103
129
  import {
104
130
  TypedRegExp,
105
- normalizePCREStyleSource,
131
+ // normalizePCREStyleSource,
106
132
  compilePCREStyleRegExpLiteral,
107
- extractJsRegexPartsFromPCREStyleRegExpLiteral
108
133
  } from "literate-regex";
109
134
  import type {
110
135
  RegExpLiteralParts,
@@ -13,4 +13,10 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  */
16
- export * from "./util";
16
+ import type { TypedRegExp, PCREStyleRegExpFlags, PCREStyleRegExpParts, PCREStyleRegExpPattern, PCREStyleToJsRegExpSource } from "../";
17
+ export declare const normalizePCREStyleSource: <const S extends string>(src: S) => PCREStyleToJsRegExpSource<S>;
18
+ export declare const extractJsRegexPartsFromPCREStyleRegExpLiteral: <const S extends string, const TRegexParts extends PCREStyleRegExpParts<S>>(src: S) => {
19
+ pattern: TRegexParts["pattern"];
20
+ flags: TRegexParts["flags"];
21
+ };
22
+ export declare const compilePCREStyleRegExpLiteral: <const S extends string>(src: S) => TypedRegExp<PCREStyleRegExpPattern<S>, PCREStyleRegExpFlags<S>>;
@@ -14,19 +14,32 @@
14
14
  // See the License for the specific language governing permissions and
15
15
  // limitations under the License.
16
16
  */
17
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- var desc = Object.getOwnPropertyDescriptor(m, k);
20
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
- desc = { enumerable: true, get: function() { return m[k]; } };
22
- }
23
- Object.defineProperty(o, k2, desc);
24
- }) : (function(o, m, k, k2) {
25
- if (k2 === undefined) k2 = k;
26
- o[k2] = m[k];
27
- }));
28
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
29
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
30
- };
31
17
  Object.defineProperty(exports, "__esModule", { value: true });
32
- __exportStar(require("./util"), exports);
18
+ exports.compilePCREStyleRegExpLiteral =
19
+ exports.extractJsRegexPartsFromPCREStyleRegExpLiteral =
20
+ exports.normalizePCREStyleSource =
21
+ void 0;
22
+ const NORMALIZE_PCRE_STYLE_REGEX = /(?<!\\)\#\s*.*$|\s+/gm;
23
+ const normalizePCREStyleSource = (src) => {
24
+ return src.replace(NORMALIZE_PCRE_STYLE_REGEX, "").replace(/\\#/g, "#");
25
+ };
26
+ exports.normalizePCREStyleSource = normalizePCREStyleSource;
27
+ const extractJsRegexPartsFromPCREStyleRegExpLiteral = (src) => {
28
+ const normalized = (0, exports.normalizePCREStyleSource)(src);
29
+ if (normalized[0] !== "/")
30
+ throw new Error("Expected regex literal to start with '/'");
31
+ const lastSlash = normalized.lastIndexOf("/");
32
+ if (lastSlash <= 0)
33
+ throw new Error("Invalid regex literal: missing trailing '/'");
34
+ const pattern = normalized.slice(1, lastSlash);
35
+ const flags = normalized.slice(lastSlash + 1);
36
+ return { pattern, flags };
37
+ };
38
+ exports.extractJsRegexPartsFromPCREStyleRegExpLiteral =
39
+ extractJsRegexPartsFromPCREStyleRegExpLiteral;
40
+ const compilePCREStyleRegExpLiteral = (src) => {
41
+ const { pattern, flags } = (0,
42
+ exports.extractJsRegexPartsFromPCREStyleRegExpLiteral)(src);
43
+ return new RegExp(pattern, flags);
44
+ };
45
+ exports.compilePCREStyleRegExpLiteral = compilePCREStyleRegExpLiteral;
@@ -13,4 +13,22 @@
13
13
  // See the License for the specific language governing permissions and
14
14
  // limitations under the License.
15
15
  */
16
- export * from "./util.mjs";
16
+ const NORMALIZE_PCRE_STYLE_REGEX = /(?<!\\)\#\s*.*$|\s+/gm;
17
+ export const normalizePCREStyleSource = (src) => {
18
+ return src.replace(NORMALIZE_PCRE_STYLE_REGEX, "").replace(/\\#/g, "#");
19
+ };
20
+ export const extractJsRegexPartsFromPCREStyleRegExpLiteral = (src) => {
21
+ const normalized = normalizePCREStyleSource(src);
22
+ if (normalized[0] !== "/")
23
+ throw new Error("Expected regex literal to start with '/'");
24
+ const lastSlash = normalized.lastIndexOf("/");
25
+ if (lastSlash <= 0)
26
+ throw new Error("Invalid regex literal: missing trailing '/'");
27
+ const pattern = normalized.slice(1, lastSlash);
28
+ const flags = normalized.slice(lastSlash + 1);
29
+ return { pattern, flags };
30
+ };
31
+ export const compilePCREStyleRegExpLiteral = (src) => {
32
+ const { pattern, flags } = extractJsRegexPartsFromPCREStyleRegExpLiteral(src);
33
+ return new RegExp(pattern, flags);
34
+ };
@@ -17,12 +17,6 @@
17
17
  "types": "./dist/global.d.ts"
18
18
  }
19
19
  },
20
- "scripts": {
21
- "clean": "rimraf -g ./dist/*",
22
- "build": "tsc & tsc -p ./tsconfig-esm.json",
23
- "postbuild": "cpx ./*.d.ts dist -v & yarn run:extras && jstool -cmd rmc -rmc4ts -basePath ./dist -test \"/\\.(m?js|d\\.m?ts)$/\"",
24
- "run:extras": "jstool -cmd cjbm -root ./dist/esm -ext mjs && mv ./dist/esm/util.d.ts ./dist/esm/util.d.mts"
25
- },
26
20
  "files": [
27
21
  "README.md",
28
22
  "LICENSE",
@@ -37,4 +31,4 @@
37
31
  "pcre",
38
32
  "jsdoc"
39
33
  ]
40
- }
34
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "js-dev-tool",
3
- "version": "1.1.2",
3
+ "version": "1.1.3",
4
4
  "bin": {
5
5
  "jstool": "tools.js"
6
6
  },
@@ -1,22 +0,0 @@
1
- /*!
2
- // Copyright jeffy-g 2025
3
- //
4
- // Licensed under the Apache License, Version 2.0 (the "License");
5
- // you may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- //
8
- // http://www.apache.org/licenses/LICENSE-2.0
9
- //
10
- // Unless required by applicable law or agreed to in writing, software
11
- // distributed under the License is distributed on an "AS IS" BASIS,
12
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- // See the License for the specific language governing permissions and
14
- // limitations under the License.
15
- */
16
- import type { TypedRegExp, PCREStyleRegExpFlags, PCREStyleRegExpParts, PCREStyleRegExpPattern, PCREStyleToJsRegExpSource } from "../";
17
- export declare const normalizePCREStyleSource: <const S extends string>(src: S) => PCREStyleToJsRegExpSource<S>;
18
- export declare const extractJsRegexPartsFromPCREStyleRegExpLiteral: <const S extends string, const TRegexParts extends PCREStyleRegExpParts<S>>(src: S) => {
19
- pattern: TRegexParts["pattern"];
20
- flags: TRegexParts["flags"];
21
- };
22
- export declare const compilePCREStyleRegExpLiteral: <const S extends string>(src: S) => TypedRegExp<PCREStyleRegExpPattern<S>, PCREStyleRegExpFlags<S>>;
@@ -1,50 +0,0 @@
1
- "use strict";
2
- /*!
3
- // Copyright jeffy-g 2025
4
- //
5
- // Licensed under the Apache License, Version 2.0 (the "License");
6
- // you may not use this file except in compliance with the License.
7
- // You may obtain a copy of the License at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // Unless required by applicable law or agreed to in writing, software
12
- // distributed under the License is distributed on an "AS IS" BASIS,
13
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- // See the License for the specific language governing permissions and
15
- // limitations under the License.
16
- */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.compilePCREStyleRegExpLiteral = exports.extractJsRegexPartsFromPCREStyleRegExpLiteral = exports.normalizePCREStyleSource = void 0;
19
- const NORMALIZE_PCRE_STYLE_REGEX = /(?<!\\)\#\s*.*$|\s+/gm;
20
- const normalizePCREStyleSource = (src) => {
21
- return src
22
- .replace(/\\#/g, "\x00#")
23
- .replace(NORMALIZE_PCRE_STYLE_REGEX, "")
24
- .replace(/\x00#/g, "#");
25
- };
26
- exports.normalizePCREStyleSource = normalizePCREStyleSource;
27
- const extractJsRegexPartsFromPCREStyleRegExpLiteral = (src) => {
28
- const normalized = (0, exports.normalizePCREStyleSource)(src);
29
- if (normalized[0] !== "/")
30
- throw new Error("Expected regex literal to start with '/'");
31
- const lastSlash = normalized.lastIndexOf("/");
32
- if (lastSlash <= 0)
33
- throw new Error("Invalid regex literal: missing trailing '/'");
34
- const pattern = normalized.slice(1, lastSlash);
35
- const flags = normalized.slice(lastSlash + 1);
36
- return { pattern, flags };
37
- };
38
- exports.extractJsRegexPartsFromPCREStyleRegExpLiteral = extractJsRegexPartsFromPCREStyleRegExpLiteral;
39
- const compilePCREStyleRegExpLiteral = (src) => {
40
- const normalized = (0, exports.normalizePCREStyleSource)(src);
41
- if (normalized[0] !== "/")
42
- throw new Error("Expected regex literal to start with '/'");
43
- const lastSlash = normalized.lastIndexOf("/");
44
- if (lastSlash <= 0)
45
- throw new Error("Invalid regex literal: missing trailing '/'");
46
- const pattern = normalized.slice(1, lastSlash);
47
- const flags = normalized.slice(lastSlash + 1);
48
- return new RegExp(pattern, flags);
49
- };
50
- exports.compilePCREStyleRegExpLiteral = compilePCREStyleRegExpLiteral;
@@ -1,16 +0,0 @@
1
- /*!
2
- // Copyright jeffy-g 2025
3
- //
4
- // Licensed under the Apache License, Version 2.0 (the "License");
5
- // you may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- //
8
- // http://www.apache.org/licenses/LICENSE-2.0
9
- //
10
- // Unless required by applicable law or agreed to in writing, software
11
- // distributed under the License is distributed on an "AS IS" BASIS,
12
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- // See the License for the specific language governing permissions and
14
- // limitations under the License.
15
- */
16
- export * from "./util";
@@ -1,44 +0,0 @@
1
- /*!
2
- // Copyright jeffy-g 2025
3
- //
4
- // Licensed under the Apache License, Version 2.0 (the "License");
5
- // you may not use this file except in compliance with the License.
6
- // You may obtain a copy of the License at
7
- //
8
- // http://www.apache.org/licenses/LICENSE-2.0
9
- //
10
- // Unless required by applicable law or agreed to in writing, software
11
- // distributed under the License is distributed on an "AS IS" BASIS,
12
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- // See the License for the specific language governing permissions and
14
- // limitations under the License.
15
- */
16
- const NORMALIZE_PCRE_STYLE_REGEX = /(?<!\\)\#\s*.*$|\s+/gm;
17
- export const normalizePCREStyleSource = (src) => {
18
- return src
19
- .replace(/\\#/g, "\x00#")
20
- .replace(NORMALIZE_PCRE_STYLE_REGEX, "")
21
- .replace(/\x00#/g, "#");
22
- };
23
- export const extractJsRegexPartsFromPCREStyleRegExpLiteral = (src) => {
24
- const normalized = normalizePCREStyleSource(src);
25
- if (normalized[0] !== "/")
26
- throw new Error("Expected regex literal to start with '/'");
27
- const lastSlash = normalized.lastIndexOf("/");
28
- if (lastSlash <= 0)
29
- throw new Error("Invalid regex literal: missing trailing '/'");
30
- const pattern = normalized.slice(1, lastSlash);
31
- const flags = normalized.slice(lastSlash + 1);
32
- return { pattern, flags };
33
- };
34
- export const compilePCREStyleRegExpLiteral = (src) => {
35
- const normalized = normalizePCREStyleSource(src);
36
- if (normalized[0] !== "/")
37
- throw new Error("Expected regex literal to start with '/'");
38
- const lastSlash = normalized.lastIndexOf("/");
39
- if (lastSlash <= 0)
40
- throw new Error("Invalid regex literal: missing trailing '/'");
41
- const pattern = normalized.slice(1, lastSlash);
42
- const flags = normalized.slice(lastSlash + 1);
43
- return new RegExp(pattern, flags);
44
- };