graphmatch 1.0.2 → 1.1.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/dist/types.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  type Node = {
2
+ partial?: boolean;
2
3
  regex: RegExp;
3
4
  children?: Node[];
4
5
  } | {
6
+ partial?: boolean;
5
7
  regex?: RegExp;
6
8
  children: Node[];
7
9
  };
package/dist/utils.js CHANGED
@@ -36,22 +36,24 @@ const getNodeSourceWithCache = (node, partial, cache) => {
36
36
  const cached = cache.get(node);
37
37
  if (cached !== undefined)
38
38
  return cached;
39
+ const isNodePartial = node.partial ?? partial;
39
40
  let source = '';
40
41
  if (node.regex) {
41
- source += partial ? '(?:$|' : '';
42
+ source += isNodePartial ? '(?:$|' : '';
42
43
  source += node.regex.source;
43
44
  }
44
45
  if (node.children?.length) {
45
46
  const children = uniq(node.children.map(node => getNodeSourceWithCache(node, partial, cache)).filter(Boolean));
46
47
  if (children?.length) {
47
- const needsWrapperGroup = (children.length > 1) || (partial && !source.length);
48
- source += needsWrapperGroup ? partial ? '(?:$|' : '(?:' : '';
48
+ const isSomeChildNonPartial = node.children.some(child => !child.regex || !(child.partial ?? partial));
49
+ const needsWrapperGroup = (children.length > 1) || (isNodePartial && (!source.length || isSomeChildNonPartial));
50
+ source += needsWrapperGroup ? isNodePartial ? '(?:$|' : '(?:' : '';
49
51
  source += children.join('|');
50
52
  source += needsWrapperGroup ? ')' : '';
51
53
  }
52
54
  }
53
55
  if (node.regex) {
54
- source += partial ? ')' : '';
56
+ source += isNodePartial ? ')' : '';
55
57
  }
56
58
  cache.set(node, source);
57
59
  return source;
package/package.json CHANGED
@@ -2,7 +2,8 @@
2
2
  "name": "graphmatch",
3
3
  "repository": "github:fabiospampinato/graphmatch",
4
4
  "description": "A low-level utility for matching a string against a directed acyclic graph of regexes.",
5
- "version": "1.0.2",
5
+ "license": "MIT",
6
+ "version": "1.1.1",
6
7
  "type": "module",
7
8
  "main": "dist/index.js",
8
9
  "exports": "./dist/index.js",
package/readme.md CHANGED
@@ -3,6 +3,7 @@
3
3
  A low-level utility for matching a string against a directed acyclic graph of regexes.
4
4
 
5
5
  - It supports matching strings partially too.
6
+ - It supports fine-grained control over which nodes in the graph can be matched partially or not.
6
7
  - It supports compiling the whole graph to a regex, even for partial matches.
7
8
  - The graph will always be matched against the input string from the very start.
8
9
  - RegExp flags are supported as long as they are the same for all the regexes in the graph.
@@ -68,7 +69,7 @@ graphmatch ( GRAPH, 'foo/bar/whoops' ); // => false
68
69
  graphmatch ( GRAPH, 'foo/baz' ); // => false
69
70
 
70
71
  // 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
+ // A partial match happens when any node in the graph sees the end of the string, either before or after the node's regex is executed
72
73
 
73
74
  graphmatch ( GRAPH, 'foo/bar/qux', { partial: true } ); // => true
74
75
  graphmatch ( GRAPH, 'foo/bar/', { partial: true } ); // => true
@@ -80,6 +81,24 @@ graphmatch ( GRAPH, 'foo/bar/whoops', { partial: true } ); // => false
80
81
  graphmatch ( GRAPH, 'foo/barsomething', { partial: true } ); // => false
81
82
  graphmatch ( GRAPH, 'bar', { partial: true } ); // => false
82
83
 
84
+ // As we just saw partial matching is set to "false" by default, and there is an option to set it to "true" by default
85
+ // But you can also decide on a node-by-node basis whether partial matching should be enabled or disabled for that particular node
86
+ // A partial node will match if the whole input string has been consumed, right before or after the node's regex is executed
87
+ // This has higher priority compared to the global setting
88
+ // This is useful for fine-grained control over which nodes can be matched partially
89
+
90
+ const NODE_NEVER_PARTIAL = { // This node will never match partially
91
+ partial: false,
92
+ regex: /foo/,
93
+ children: [],
94
+ };
95
+
96
+ const NODE_ALWAYS_PARTIAL = { // This node will always match partially
97
+ partial: true,
98
+ regex: /foo/,
99
+ children: [],
100
+ };
101
+
83
102
  // Let's now compile the whole graph to a single regex
84
103
  // This is useful if you expect to match against the graph multiple times
85
104
  // It's faster to compile the graph once and match against it multiple times