graphmatch 1.0.0

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/asd.js ADDED
@@ -0,0 +1,81 @@
1
+
2
+ import graphmatch from './dist/index.js';
3
+
4
+
5
+ const GRAPH = {
6
+ regex: /foo/,
7
+ children: [
8
+ {
9
+ regex: /\//,
10
+ children: [
11
+ {
12
+ regex: /bar/,
13
+ children: [
14
+ {
15
+ regex: /\//,
16
+ children: [
17
+ {
18
+ regex: /qux/
19
+ }
20
+ ]
21
+ }
22
+ ]
23
+ },
24
+ {
25
+ regex: /baz/,
26
+ children: [
27
+ {
28
+ regex: /\//,
29
+ children: [
30
+ {
31
+ regex: /qux/
32
+ }
33
+ ]
34
+ }
35
+ ]
36
+ }
37
+ ]
38
+ }
39
+ ]
40
+ };
41
+
42
+ // Let's now match against the graph, fully
43
+
44
+ console.log ('1.');
45
+
46
+ console.log ( graphmatch ( GRAPH, 'foo/bar/qux' ) ); // => true
47
+ console.log ( graphmatch ( GRAPH, 'foo/baz/qux' ) ); // => true
48
+
49
+ console.log ( graphmatch ( GRAPH, 'foo/bar/whoops' ) ); // => false
50
+ console.log ( graphmatch ( GRAPH, 'foo/baz' ) ); // => false
51
+
52
+ // Let's now match against the graph, partially
53
+ // A partial match happens when any matching node in the graph reaches the end of the string
54
+
55
+ console.log ('2.');
56
+
57
+ console.log ( graphmatch ( GRAPH, 'foo/bar/qux', { partial: true } ) ); // => true
58
+ console.log ( graphmatch ( GRAPH, 'foo/bar/', { partial: true } ) ); // => true
59
+ console.log ( graphmatch ( GRAPH, 'foo/bar', { partial: true } ) ); // => true
60
+ console.log ( graphmatch ( GRAPH, 'foo/', { partial: true } ) ); // => true
61
+ console.log ( graphmatch ( GRAPH, 'foo', { partial: true } ) ); // => true
62
+
63
+ console.log ( graphmatch ( GRAPH, 'foo/bar/whoops', { partial: true } ) ); // => false
64
+ console.log ( graphmatch ( GRAPH, 'foo/barsomething', { partial: true } ) ); // => false
65
+ console.log ( graphmatch ( GRAPH, 'bar', { partial: true } ) ); // => false
66
+
67
+ // Let's now compile the whole graph to a single regex
68
+ // This is useful if you expect to match against the graph multiple times
69
+ // It's faster to compile the graph once and match against it multiple times
70
+
71
+ console.log ('3.');
72
+
73
+ const fullRe = graphmatch.compile ( GRAPH ); // => RegExp
74
+
75
+ console.log ( fullRe.test ( 'foo/bar/qux' ) ); // => true
76
+ console.log ( fullRe.test ( 'foo/bar' ) ); // => false
77
+
78
+ const partialRe = graphmatch.compile ( GRAPH, { partial: true } ); // => RegExp
79
+
80
+ console.log ( partialRe.test ( 'foo/bar/qux' ) ); // => true
81
+ console.log ( partialRe.test ( 'foo/bar' ) ); // => true
@@ -0,0 +1,7 @@
1
+ import type { Node, Options } from './types.js';
2
+ declare const graphmatch: {
3
+ (node: Node, input: string, options?: Options): boolean;
4
+ compile(node: Node, options?: Options): RegExp;
5
+ };
6
+ export type { Node, Options };
7
+ export default graphmatch;
package/dist/index.js ADDED
@@ -0,0 +1,14 @@
1
+ /* IMPORT */
2
+ import { getNodeFlags, getNodeSource } from './utils.js';
3
+ /* MAIN */
4
+ const graphmatch = (node, input, options) => {
5
+ return graphmatch.compile(node, options).test(input);
6
+ };
7
+ /* UTILITIES */
8
+ graphmatch.compile = (node, options) => {
9
+ const partial = options?.partial ?? false;
10
+ const source = getNodeSource(node, partial);
11
+ const flags = getNodeFlags(node);
12
+ return new RegExp(`^(?:${source})$`, flags);
13
+ };
14
+ export default graphmatch;
@@ -0,0 +1,8 @@
1
+ type Node = {
2
+ regex?: RegExp;
3
+ children?: Node[];
4
+ };
5
+ type Options = {
6
+ partial?: boolean;
7
+ };
8
+ export type { Node, Options };
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ /* MAIN */
2
+ export {};
@@ -0,0 +1,5 @@
1
+ import type { Node } from './types.js';
2
+ declare const getNodeFlags: (node: Node) => string;
3
+ declare const getNodeSource: (node: Node, partial: boolean) => string;
4
+ declare const visitNode: (node: Node, visitor: (node: Node) => void) => void;
5
+ export { getNodeFlags, getNodeSource, visitNode };
package/dist/utils.js ADDED
@@ -0,0 +1,43 @@
1
+ /* IMPORT */
2
+ /* MAIN */
3
+ const getNodeFlags = (node) => {
4
+ let flags = '';
5
+ visitNode(node, node => {
6
+ if (!node.regex)
7
+ return;
8
+ const nodeFlags = node.regex.flags;
9
+ flags || (flags = nodeFlags);
10
+ if (flags !== nodeFlags) {
11
+ throw new Error(`Inconsistent RegExp flags used: "${flags}" and "${nodeFlags}"`);
12
+ }
13
+ });
14
+ return flags;
15
+ };
16
+ const getNodeSource = (node, partial) => {
17
+ let source = '';
18
+ if (node.regex) {
19
+ source += partial ? '(?:$|' : '';
20
+ source += node.regex.source;
21
+ }
22
+ if (node.children?.length) {
23
+ const children = node.children.map(node => getNodeSource(node, partial)).filter(Boolean);
24
+ if (children.length) {
25
+ const needsWrapperGroup = (children.length > 1) || (partial && !source.length);
26
+ source += needsWrapperGroup ? partial ? '(?:$|' : '(?:' : '';
27
+ source += children.join('|');
28
+ source += needsWrapperGroup ? ')' : '';
29
+ }
30
+ }
31
+ if (node.regex) {
32
+ source += partial ? ')' : '';
33
+ }
34
+ return source;
35
+ };
36
+ const visitNode = (node, visitor) => {
37
+ visitor(node);
38
+ node.children?.forEach(node => {
39
+ visitNode(node, visitor);
40
+ });
41
+ };
42
+ /* EXPORT */
43
+ export { getNodeFlags, getNodeSource, visitNode };
package/license ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025-present Fabio Spampinato
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a
6
+ copy of this software and associated documentation files (the "Software"),
7
+ to deal in the Software without restriction, including without limitation
8
+ the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
+ and/or sell copies of the Software, and to permit persons to whom the
10
+ Software is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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
20
+ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
+ DEALINGS IN THE SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "graphmatch",
3
+ "repository": "github:fabiospampinato/graphmatch",
4
+ "description": "A low-level utility for matching a string against a directed acyclic graph of regexes.",
5
+ "version": "1.0.0",
6
+ "type": "module",
7
+ "main": "dist/index.js",
8
+ "exports": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "scripts": {
11
+ "benchmark": "tsex benchmark",
12
+ "benchmark:watch": "tsex benchmark --watch",
13
+ "clean": "tsex clean",
14
+ "compile": "tsex compile",
15
+ "compile:watch": "tsex compile --watch",
16
+ "test": "tsex test",
17
+ "test:watch": "tsex test --watch",
18
+ "prepublishOnly": "tsex prepare"
19
+ },
20
+ "keywords": [
21
+ "regex",
22
+ "regexp",
23
+ "dag",
24
+ "graph",
25
+ "match"
26
+ ],
27
+ "devDependencies": {
28
+ "benchloop": "^2.1.1",
29
+ "fava": "^0.3.5",
30
+ "tsex": "^4.0.2",
31
+ "typescript": "^5.9.3"
32
+ },
33
+ "dependencies": {
34
+ "lodash": "^4.17.21"
35
+ }
36
+ }
package/readme.md ADDED
@@ -0,0 +1,100 @@
1
+ # Graphmatch
2
+
3
+ A low-level utility for matching a string against a directed acyclic graph of regexes.
4
+
5
+ - It supports matching strings partially too.
6
+ - It supports compiling the whole graph to a regex, even for partial matches.
7
+ - The graph will always be matched against the input string from the very start.
8
+ - RegExp flags are supported as long as they are the same for all the regexes in the graph.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ npm install graphmatch
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ```ts
19
+ import graphmatch from 'graphmatch';
20
+
21
+ // Let's say we would like to match against this glob: foo/{bar,baz}/qux
22
+ // Let's express that as a graph of regexes that this library can match against
23
+ // Whether you reuse the "qux" node or not doesn't matter, both are supported
24
+
25
+ const GRAPH = {
26
+ regex: /foo/,
27
+ children: [
28
+ {
29
+ regex: /\//,
30
+ children: [
31
+ {
32
+ regex: /bar/,
33
+ children: [
34
+ {
35
+ regex: /\//,
36
+ children: [
37
+ {
38
+ regex: /qux/
39
+ }
40
+ ]
41
+ }
42
+ ]
43
+ },
44
+ {
45
+ regex: /baz/,
46
+ children: [
47
+ {
48
+ regex: /\//,
49
+ children: [
50
+ {
51
+ regex: /qux/
52
+ }
53
+ ]
54
+ }
55
+ ]
56
+ }
57
+ ]
58
+ }
59
+ ]
60
+ };
61
+
62
+ // Let's now match against the graph, fully
63
+
64
+ graphmatch ( GRAPH, 'foo/bar/qux' ); // => true
65
+ graphmatch ( GRAPH, 'foo/baz/qux' ); // => true
66
+
67
+ graphmatch ( GRAPH, 'foo/bar/whoops' ); // => false
68
+ graphmatch ( GRAPH, 'foo/baz' ); // => false
69
+
70
+ // Let's now match against the graph, partially
71
+ // A partial match happens when any matching node in the graph reaches the end of the string
72
+
73
+ graphmatch ( GRAPH, 'foo/bar/qux', { partial: true } ); // => true
74
+ graphmatch ( GRAPH, 'foo/bar/', { partial: true } ); // => true
75
+ graphmatch ( GRAPH, 'foo/bar', { partial: true } ); // => true
76
+ graphmatch ( GRAPH, 'foo/', { partial: true } ); // => true
77
+ graphmatch ( GRAPH, 'foo', { partial: true } ); // => true
78
+
79
+ graphmatch ( GRAPH, 'foo/bar/whoops', { partial: true } ); // => false
80
+ graphmatch ( GRAPH, 'foo/barsomething', { partial: true } ); // => false
81
+ graphmatch ( GRAPH, 'bar', { partial: true } ); // => false
82
+
83
+ // Let's now compile the whole graph to a single regex
84
+ // This is useful if you expect to match against the graph multiple times
85
+ // It's faster to compile the graph once and match against it multiple times
86
+
87
+ const fullRe = graphmatch.compile ( GRAPH ); // => RegExp
88
+
89
+ fullRe.test ( 'foo/bar/qux' ); // => true
90
+ fullRe.test ( 'foo/bar' ); // => false
91
+
92
+ const partialRe = graphmatch.compile ( GRAPH, { partial: true } ); // => RegExp
93
+
94
+ partialRe.test ( 'foo/bar/qux' ); // => true
95
+ partialRe.test ( 'foo/bar' ); // => true
96
+ ```
97
+
98
+ ## License
99
+
100
+ MIT © Fabio Spampinato