@marianmeres/simple-router 2.0.0 → 2.1.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/README.md CHANGED
@@ -40,6 +40,7 @@ Route segments:
40
40
  - `[name(regex)]` matches if `regex.test(segment)` is truthy
41
41
  - `exact?` or `[name]?` marks segment as optional
42
42
  - `[...name]` matches "rest segments"
43
+ - `*` matches zero or more following segments
43
44
 
44
45
  Few notes on segments separators (which is slash `/` by default):
45
46
  - multiple ones are always normalized to single,
@@ -68,8 +69,11 @@ Few notes on segments separators (which is slash `/` by default):
68
69
  | `/foo/[id([0-9]+)]/[bar]` | `/foo/bar/baz` | `null` |
69
70
  | `/foo/[([0-9]+)]` | `/foo/123` | `null` (missing name before regex) |
70
71
  | `/foo/[bar]?` | `/foo` | `{}` |
71
- | `/foo/[bar]?` | `/foo/bar` | `{ bar: 'bar' }` |
72
- | `/foo/[bar]?/baz` | `/foo/bar/baz` | `{ bar: 'bar' }` |
73
- | `/[...path]/[file]` | `/foo/bar/baz.js` | `{ path': 'foo/bar', file: 'baz.js' }`|
72
+ | `/foo/[bar]?` | `/foo/bar` | `{ bar: "bar" }` |
73
+ | `/foo/[bar]?/baz` | `/foo/bar/baz` | `{ bar: "bar" }` |
74
+ | `/[...path]/[file]` | `/foo/bar/baz.js` | `{ path: "foo/bar", file: "baz.js" }`|
75
+ | `/foo/*` | `/foo/bar/baz.js` | `{}` |
76
+ | `/[foo]/*` | `/foo/bar` | `{ foo: "foo" }` |
77
+ | `/*` | `/foo/bar` | `{}` |
74
78
  See [tests](tests) or [examples](examples) for more.
75
79
 
package/dist/index.cjs CHANGED
@@ -120,7 +120,8 @@ class SimpleRoute {
120
120
  });
121
121
  segments = newSegments;
122
122
  }
123
- // return early special case: first segment is a wildcard
123
+ // return early no-op special case: first segment is a wildcard
124
+ // (no need to process further)
124
125
  if (this._parsed?.[0]?.segment === SimpleRoute.WILDCARD) {
125
126
  return matched;
126
127
  }
@@ -145,6 +146,9 @@ class SimpleRoute {
145
146
  if (p.name) {
146
147
  matched[decodeURIComponent(p.name)] = decodeURIComponent(s);
147
148
  }
149
+ if (p.segment === SimpleRoute.WILDCARD) {
150
+ break;
151
+ }
148
152
  }
149
153
  return matched;
150
154
  }
@@ -171,6 +175,13 @@ class SimpleRouter {
171
175
  _dbg(...a) {
172
176
  SimpleRouter.debug && console.log('[SimpleRouter]', ...a);
173
177
  }
178
+ /** Will report map of registered routes and their labels. Intended for debugging. */
179
+ info() {
180
+ return this._routes.reduce((m, r) => {
181
+ m[r[0].route] = r[3];
182
+ return m;
183
+ }, {});
184
+ }
174
185
  reset() {
175
186
  this._routes = [];
176
187
  return this;
package/dist/index.js CHANGED
@@ -118,7 +118,8 @@ class SimpleRoute {
118
118
  });
119
119
  segments = newSegments;
120
120
  }
121
- // return early special case: first segment is a wildcard
121
+ // return early no-op special case: first segment is a wildcard
122
+ // (no need to process further)
122
123
  if (this._parsed?.[0]?.segment === SimpleRoute.WILDCARD) {
123
124
  return matched;
124
125
  }
@@ -143,6 +144,9 @@ class SimpleRoute {
143
144
  if (p.name) {
144
145
  matched[decodeURIComponent(p.name)] = decodeURIComponent(s);
145
146
  }
147
+ if (p.segment === SimpleRoute.WILDCARD) {
148
+ break;
149
+ }
146
150
  }
147
151
  return matched;
148
152
  }
@@ -169,6 +173,13 @@ class SimpleRouter {
169
173
  _dbg(...a) {
170
174
  SimpleRouter.debug && console.log('[SimpleRouter]', ...a);
171
175
  }
176
+ /** Will report map of registered routes and their labels. Intended for debugging. */
177
+ info() {
178
+ return this._routes.reduce((m, r) => {
179
+ m[r[0].route] = r[3];
180
+ return m;
181
+ }, {});
182
+ }
172
183
  reset() {
173
184
  this._routes = [];
174
185
  return this;
package/dist/router.d.ts CHANGED
@@ -13,6 +13,8 @@ export declare class SimpleRouter {
13
13
  [route: string]: Function;
14
14
  });
15
15
  protected _dbg(...a: any[]): void;
16
+ /** Will report map of registered routes and their labels. Intended for debugging. */
17
+ info(): Record<string, string>;
16
18
  reset(): this;
17
19
  get current(): {
18
20
  route: string;
package/package.json CHANGED
@@ -1,50 +1,51 @@
1
1
  {
2
- "name": "@marianmeres/simple-router",
3
- "version": "2.0.0",
4
- "description": "Minimalistic route parser for sapper-like regex routes",
5
- "type": "module",
6
- "main": "./dist/index.cjs",
7
- "module": "./dist/index.js",
8
- "exports": {
9
- "require": "./dist/index.cjs",
10
- "import": "./dist/index.js"
11
- },
12
- "types": "./dist/index.d.ts",
13
- "files": [
14
- "dist"
15
- ],
16
- "scripts": {
17
- "clean": "rimraf dist/*",
18
- "build": "npm run clean && rollup -c",
19
- "dev": "npm run clean && rollup -c -w",
20
- "prettier": "prettier --write \"{src,tests,examples}/**/*.{js,ts,json}\"",
21
- "release": "release",
22
- "test": "test-runner",
23
- "test:watch": "nodemon -q -w dist -w tests -x test-runner --"
24
- },
25
- "repository": {
26
- "type": "git",
27
- "url": "git+https://github.com/marianmeres/simple-router.git"
28
- },
29
- "keywords": [
30
- "router"
31
- ],
32
- "author": "Marian Meres <marian@meres.sk>",
33
- "license": "WTFPL",
34
- "bugs": {
35
- "url": "https://github.com/marianmeres/simple-router/issues"
36
- },
37
- "homepage": "https://github.com/marianmeres/simple-router#readme",
38
- "devDependencies": {
39
- "@marianmeres/release": "^0.0.7",
40
- "@marianmeres/test-runner": "^2.0.8",
41
- "@rollup/plugin-typescript": "^9.0.2",
42
- "@types/node": "^14.18.33",
43
- "lodash": "^4.17.21",
44
- "prettier": "^2.2.1",
45
- "rimraf": "^3.0.2",
46
- "rollup": "^3.3.0",
47
- "tslib": "^2.4.1",
48
- "typescript": "^4.8.4"
49
- }
2
+ "name": "@marianmeres/simple-router",
3
+ "version": "2.1.0",
4
+ "description": "Minimalistic route parser for sapper-like regex routes",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "exports": {
9
+ "require": "./dist/index.cjs",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "types": "./dist/index.d.ts",
13
+ "files": [
14
+ "dist"
15
+ ],
16
+ "scripts": {
17
+ "clean": "rimraf dist/*",
18
+ "build": "npm run clean && rollup -c",
19
+ "dev": "npm run clean && rollup -c -w",
20
+ "prettier": "prettier --write \"{src,tests,examples}/**/*.{js,ts,json}\"",
21
+ "release": "release -v minor",
22
+ "release:patch": "release -v patch",
23
+ "test": "test-runner",
24
+ "test:watch": "nodemon -q -w dist -w tests -x test-runner --"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "git+https://github.com/marianmeres/simple-router.git"
29
+ },
30
+ "keywords": [
31
+ "router"
32
+ ],
33
+ "author": "Marian Meres <marian@meres.sk>",
34
+ "license": "WTFPL",
35
+ "bugs": {
36
+ "url": "https://github.com/marianmeres/simple-router/issues"
37
+ },
38
+ "homepage": "https://github.com/marianmeres/simple-router#readme",
39
+ "devDependencies": {
40
+ "@marianmeres/release": "^1.1.2",
41
+ "@marianmeres/test-runner": "^2.0.8",
42
+ "@rollup/plugin-typescript": "^9.0.2",
43
+ "@types/node": "^14.18.33",
44
+ "lodash": "^4.17.21",
45
+ "prettier": "^2.2.1",
46
+ "rimraf": "^3.0.2",
47
+ "rollup": "^3.3.0",
48
+ "tslib": "^2.4.1",
49
+ "typescript": "^4.8.4"
50
+ }
50
51
  }