@jround/re-template-tag 0.0.0-stage → 3.0.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,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Axel Rauschmayer
4
+ Copyright (c) 2026 Jesus Redondo
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,89 @@
1
- # Temporary Holding Version
1
+ # @jround/re-template-tag: easily compose regular expressions
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This package provides the template tag `re` for composing regular expressions.
4
+
5
+ It is a TypeScript fork of [rauschma/re-template-tag](https://github.com/rauschma/re-template-tag),
6
+ published as `@jround/re-template-tag`. The unscoped `re-template-tag` on npm is the original 2.0.1.
7
+
8
+ ## Install
9
+
10
+ ```sh
11
+ npm install @jround/re-template-tag
12
+ ```
13
+
14
+ Requirements:
15
+
16
+ * Node.js 24 or newer.
17
+ * ES module. Use `import`, or `require()` on Node.js 24, which loads ES modules.
18
+ * Type declarations are included. Do not install `@types/re-template-tag`.
19
+
20
+ ## Basic syntax
21
+
22
+ Syntax: the following two expressions produce the same regular expression.
23
+
24
+ ```js
25
+ re`/abc/gu`
26
+ /abc/gu
27
+ ```
28
+
29
+ ## Composing regular expressions
30
+
31
+ ```js
32
+ import {re} from '@jround/re-template-tag';
33
+
34
+ const RE_YEAR = /([0-9]{4})/;
35
+ const RE_MONTH = /([0-9]{2})/;
36
+ const RE_DAY = /([0-9]{2})/;
37
+ const RE_DATE = re`/^${RE_YEAR}-${RE_MONTH}-${RE_DAY}$/u`;
38
+
39
+ RE_DATE.test('2017-01-23'); // true
40
+ ```
41
+
42
+ A `RegExp` substitution inserts its source. A string substitution is matched literally.
43
+ Any other substitution throws `Error`.
44
+
45
+ ## Escaping text
46
+
47
+ `quoteText` escapes every regular expression metacharacter, including inside parentheses
48
+ or a character class. `re` applies it to every string substitution.
49
+
50
+ ```js
51
+ import {quoteText} from '@jround/re-template-tag';
52
+
53
+ quoteText('1+1=2'); // '1\\+1\\x3d2'
54
+ ```
55
+
56
+ `- = ! < > :` become hex escapes such as `\x2d`, because the `u` and `v` flags reject a
57
+ backslash before them. The result is a literal match with any flags.
58
+
59
+ ## Checks
60
+
61
+ ```sh
62
+ npm run typecheck
63
+ npm run lint
64
+ npm run build
65
+ npm test
66
+ ```
67
+
68
+ `npm test` builds first, then the unit tests import the package by its own name,
69
+ which resolves through `exports` to `dist/index.js`.
70
+
71
+ ## Releases
72
+
73
+ Changes are listed in [CHANGELOG.md](./CHANGELOG.md). Pushing a tag `vX.Y.Z` that matches the
74
+ `version` in `package.json` publishes that version to npm with provenance.
75
+
76
+ ## More information
77
+
78
+ * Take a look at [the unit tests](https://github.com/JSRound/re-template-tag/blob/master/test/index.test.js).
79
+ * Check out [the blog post on re-template-tag](http://2ality.com/2017/07/re-template-tag.html).
80
+
81
+ ## Related work
82
+
83
+ * http://xregexp.com
84
+ * https://github.com/DmitrySoshnikov/regexp-tree
85
+ * https://github.com/shannonmoeller/regx
86
+
87
+ ## Acknowledgement
88
+
89
+ The syntax for separating flags from the actual regular expression is based on an idea by [Mathias Bynens](https://twitter.com/mathias/).
@@ -0,0 +1,14 @@
1
+ /** A pattern fragment, or text that must be matched literally. */
2
+ type PatternSubstitution = RegExp | string;
3
+ /**
4
+ * Compose a regular expression.
5
+ * A `RegExp` substitution inserts its source. A string substitution is escaped.
6
+ * Start the template with `/` to set flags: `` re`/pattern/flags` ``.
7
+ */
8
+ export declare function re(template: TemplateStringsArray, ...substitutions: readonly PatternSubstitution[]): RegExp;
9
+ /**
10
+ * Escape every regex metacharacter, including inside parentheses or a character class.
11
+ * The result is valid with any flags, including `u` and `v`.
12
+ */
13
+ export declare function quoteText(text: string): string;
14
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,86 @@
1
+ const FLAGS = /^[dgimsuvy]*$/;
2
+ /**
3
+ * Compose a regular expression.
4
+ * A `RegExp` substitution inserts its source. A string substitution is escaped.
5
+ * Start the template with `/` to set flags: `` re`/pattern/flags` ``.
6
+ */
7
+ export function re(template, ...substitutions) {
8
+ const pattern = composePattern(template, substitutions);
9
+ const { source, flags } = splitPattern(pattern);
10
+ return new RegExp(source, flags);
11
+ }
12
+ function composePattern(template, substitutions) {
13
+ let pattern = transformRaw(template.raw[0] ?? '');
14
+ for (const [index, substitution] of substitutions.entries()) {
15
+ pattern += substitutionText(substitution);
16
+ pattern += transformRaw(template.raw[index + 1] ?? '');
17
+ }
18
+ return pattern;
19
+ }
20
+ function substitutionText(substitution) {
21
+ if (isRegExpFragment(substitution)) {
22
+ return substitution.source;
23
+ }
24
+ if (isLiteralText(substitution)) {
25
+ return quoteText(substitution);
26
+ }
27
+ throw new Error(`Illegal substitution: ${String(substitution)}`);
28
+ }
29
+ function isRegExpFragment(substitution) {
30
+ return substitution instanceof RegExp;
31
+ }
32
+ function isLiteralText(substitution) {
33
+ return typeof substitution === 'string';
34
+ }
35
+ function splitPattern(pattern) {
36
+ if (!startsWithDelimiter(pattern)) {
37
+ return { source: pattern, flags: '' };
38
+ }
39
+ const closingSlash = pattern.lastIndexOf('/');
40
+ if (isMissingClosingSlash(closingSlash)) {
41
+ throw new Error(missingClosingSlashMessage(pattern));
42
+ }
43
+ const flags = pattern.slice(closingSlash + 1);
44
+ if (!isFlagString(flags)) {
45
+ throw new Error(`Invalid regular expression flags: ${flags}`);
46
+ }
47
+ return { source: pattern.slice(1, closingSlash), flags };
48
+ }
49
+ function startsWithDelimiter(pattern) {
50
+ return pattern.startsWith('/');
51
+ }
52
+ function isMissingClosingSlash(closingSlash) {
53
+ return closingSlash === 0;
54
+ }
55
+ function isFlagString(flags) {
56
+ return FLAGS.test(flags);
57
+ }
58
+ function missingClosingSlashMessage(pattern) {
59
+ return ('If the `re` string starts with a slash, it must end with a second slash and zero or more flags: ' +
60
+ pattern);
61
+ }
62
+ function transformRaw(text) {
63
+ return text.replace(/\\`/g, '`');
64
+ }
65
+ /**
66
+ * Escape every regex metacharacter, including inside parentheses or a character class.
67
+ * The result is valid with any flags, including `u` and `v`.
68
+ */
69
+ export function quoteText(text) {
70
+ return text.replace(/[\\^$.*+?()[\]{}|=!<>:-]/g, escapeCharacter);
71
+ }
72
+ /** Characters that `u` and `v` mode accept after a backslash. */
73
+ const SYNTAX_CHARACTER = /^[\\^$.*+?()[\]{}|]$/;
74
+ function escapeCharacter(character) {
75
+ if (isSyntaxCharacter(character)) {
76
+ return `\\${character}`;
77
+ }
78
+ return hexEscape(character);
79
+ }
80
+ function isSyntaxCharacter(character) {
81
+ return SYNTAX_CHARACTER.test(character);
82
+ }
83
+ /** `-` becomes `\x2d`: a literal in every flag mode, never a range inside a character class. */
84
+ function hexEscape(character) {
85
+ return `\\x${character.charCodeAt(0).toString(16).padStart(2, '0')}`;
86
+ }
package/package.json CHANGED
@@ -1,6 +1,58 @@
1
1
  {
2
2
  "name": "@jround/re-template-tag",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "3.0.1",
4
+ "description": "Template tag that composes regular expressions from RegExp and escaped string fragments.",
5
+ "keywords": [
6
+ "regexp",
7
+ "regular-expression",
8
+ "template-tag",
9
+ "tagged-template"
10
+ ],
11
+ "homepage": "https://github.com/JSRound/re-template-tag#readme",
12
+ "bugs": "https://github.com/JSRound/re-template-tag/issues",
13
+ "type": "module",
14
+ "engines": {
15
+ "node": ">=24"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "README.md",
20
+ "LICENSE"
21
+ ],
22
+ "sideEffects": false,
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ }
28
+ },
29
+ "scripts": {
30
+ "typecheck": "tsc --noEmit",
31
+ "lint": "eslint .",
32
+ "build": "tsc -p tsconfig.build.json",
33
+ "pretest": "npm run build",
34
+ "test": "node --test",
35
+ "check:tarball": "node scripts/check-tarball.mjs",
36
+ "prepare": "npm run build"
37
+ },
38
+ "author": "Axel Rauschmayer",
39
+ "contributors": [
40
+ "Jesus Redondo <github@jesusredondo.com>"
41
+ ],
42
+ "license": "MIT",
43
+ "repository": {
44
+ "type": "git",
45
+ "url": "git+https://github.com/JSRound/re-template-tag.git"
46
+ },
47
+ "publishConfig": {
48
+ "access": "public",
49
+ "provenance": true
50
+ },
51
+ "devDependencies": {
52
+ "@eslint/js": "^10.0.1",
53
+ "@types/node": "^26.6.4",
54
+ "eslint": "^10.12.0",
55
+ "typescript": "^6.0.3",
56
+ "typescript-eslint": "^8.71.1"
57
+ }
58
+ }