@ndiinginc/router 4.3.0 → 4.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ndiinginc/router",
3
- "version": "4.3.0",
3
+ "version": "4.4.0",
4
4
  "description": "",
5
5
  "main": "./src/index.js",
6
6
  "scripts": {
package/src/node.js CHANGED
@@ -3,7 +3,9 @@ const { Pattern } = require("./pattern.js");
3
3
  class Node {
4
4
  constructor(segment = "") {
5
5
  this.segment = segment;
6
- this.pattern = new Pattern(segment);
6
+ this.hasRegExp = segment.startsWith(":") || segment.indexOf("*") !== -1;
7
+ this.hasRegExpChildren = false;
8
+ this.pattern = this.hasRegExp && new Pattern(segment);
7
9
  /**@type {Map<string,Node>}*/
8
10
  this.children = new Map();
9
11
  this.handlers = new Map([
@@ -19,11 +21,15 @@ class Node {
19
21
  ["PATCH", []],
20
22
  ["CONNECT", []],
21
23
  ]);
24
+ // debug
25
+ Object.defineProperty(this, "handlers", { configurable: true, enumerable: false, writable: true });
22
26
  }
23
27
 
24
28
  add(segment) {
25
29
  if (!this.children.has(segment)) {
26
- this.children.set(segment, new Node(segment));
30
+ const node = new Node(segment);
31
+ if (node.hasRegExp) this.hasRegExpChildren = true; // node.hasRegExp;
32
+ this.children.set(segment, node);
27
33
  }
28
34
  return this.children.get(segment);
29
35
  }
@@ -40,6 +46,7 @@ class Node {
40
46
  }
41
47
 
42
48
  for (const [segment, child] of node.children) {
49
+ if (child.hasRegExp) this.hasRegExpChildren = true; // child.hasRegExp;
43
50
  if (!this.children.has(segment)) {
44
51
  child.setSeqOffset(offset);
45
52
  this.children.set(segment, child);
package/src/router.js CHANGED
@@ -114,8 +114,9 @@ class Router {
114
114
  for (const input of this.split(path)) {
115
115
  let matched = node.children.get(input);
116
116
 
117
- if (!matched) {
117
+ if (!matched && node.hasRegExpChildren) {
118
118
  for (const [, child] of node.children) {
119
+ if (!child.hasRegExp) continue;
119
120
  const match = child.pattern.exec(input);
120
121
  if (match) {
121
122
  matched = child;