Skip to content

Commit 422f630

Browse files
benmerckxlukeed
andauthored
break: support optional wildcard pattern (#25)
* Optional wildcards * Wild can be omitted for optional wildcard inject - we're not expecting a null value * Apply suggestions from code review * chore: update readme * chore: update module size --------- Co-authored-by: Luke Edwards <[email protected]>
1 parent 5290e87 commit 422f630

File tree

6 files changed

+29
-6
lines changed

6 files changed

+29
-6
lines changed

index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ export type RouteParams<T extends string> =
2121
? { [K in P]: string }
2222
: T extends `${string}*`
2323
? { wild: string }
24+
: T extends `${string}*?`
25+
? { wild?: string }
2426
: {};
2527

2628
export function inject<T extends string>(route: T, values: RouteParams<T>): string;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "regexparam",
33
"version": "2.0.2",
44
"repository": "lukeed/regexparam",
5-
"description": "A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇‍",
5+
"description": "A tiny (405B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇‍",
66
"unpkg": "dist/index.min.js",
77
"module": "dist/index.mjs",
88
"main": "dist/index.js",

readme.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# regexparam [![CI](https://github.com/lukeed/regexparam/actions/workflows/ci.yml/badge.svg)](https://github.com/lukeed/regexparam/actions/workflows/ci.yml)
22

3-
> A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇
3+
> A tiny (405B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇
44
55
With `regexparam`, you may turn a pathing string (eg, `/users/:id`) into a regular expression.
66

@@ -13,6 +13,7 @@ Unlike [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp), this modu
1313
* Parameter w/ Suffix (`/movies/:title.mp4`, `/movies/:title.(mp4|mov)`)
1414
* Optional Parameters (`/:title?`, `/books/:title?`, `/books/:genre/:title?`)
1515
* Wildcards (`*`, `/books/*`, `/books/:genre/*`)
16+
* Optional Wildcard (`/books/*?`)
1617

1718
This module exposes three module definitions:
1819

@@ -35,7 +36,7 @@ import { parse, inject } from 'regexparam';
3536
// Example param-assignment
3637
function exec(path, result) {
3738
let i=0, out={};
38-
let matches = result.pattern.exec(path);
39+
let matches = result.pattern.exec(path) || [];
3940
while (i < result.keys.length) {
4041
out[ result.keys[i] ] = matches[++i] || null;
4142
}
@@ -82,6 +83,23 @@ let baz = parse('users/*');
8283
baz.pattern.test('/users'); //=> false
8384
baz.pattern.test('/users/lukeed'); //=> true
8485

86+
exec('/users', baz);
87+
//=> { wild: 'lukeed/repos/new' }
88+
exec('/users/lukeed/repos/new', baz);
89+
//=> { wild: 'lukeed/repos/new' }
90+
91+
92+
// Optional Wildcard
93+
// ---
94+
let baz = parse('/users/*?');
95+
// baz.pattern => /^\/users(?:\/(.*))?(?=$|\/)/i
96+
// baz.keys => ['wild']
97+
98+
baz.pattern.test('/users'); //=> true
99+
baz.pattern.test('/users/lukeed'); //=> true
100+
101+
exec('/users', baz);
102+
//=> { wild: null }
85103
exec('/users/lukeed/repos/new', baz);
86104
//=> { wild: 'lukeed/repos/new' }
87105

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function parse(str, loose) {
77
c = tmp[0];
88
if (c === '*') {
99
keys.push('wild');
10-
pattern += '/(.*)';
10+
pattern += tmp[1] === '?' ? '(?:/(.*))?' : '/(.*)';
1111
} else if (c === ':') {
1212
o = tmp.indexOf('?', 1);
1313
ext = tmp.indexOf('.', 1);

test/inject.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ run('/foo/:id/:a?/:b?/:bar?', '/foo/123/xxx', { id: 123, bar: 'xxx' });
6464
run('/foo/:id/:a?/:b?/:bar?', '/foo/123/aa/xxx', { id: 123, a: 'aa', bar: 'xxx' });
6565

6666
run('/foo/:bar/*', '/foo/123', { bar: '123' });
67+
run('/foo/:bar/*?', '/foo/123', { bar: '123' });
68+
6769
run('/foo/:bar/*', '/foo/123/aa/bb/cc', { bar: '123', wild: 'aa/bb/cc' });
70+
run('/foo/:bar/*?', '/foo/123/aa/bb/cc', { bar: '123', wild: 'aa/bb/cc' });
6871

6972
// NOTE: Missing non-optional values
7073
// ---

test/parse.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ test('execs', () => {
290290
toExec('/books/*', '/books/world/howdy/', { wild:'world/howdy/' });
291291

292292
// console.log('/books/*?');
293-
toExec('/books/*?', '/books', false);
293+
toExec('/books/*?', '/books', { wild:null });
294294
toExec('/books/*?', '/books/', { wild:null });
295295
toExec('/books/*?', '/books/world', { wild:'world' });
296296
toExec('/books/*?', '/books/world/', { wild:'world/' });
@@ -354,7 +354,7 @@ test('execs :: loose', () => {
354354
toLooseExec('/books/*', '/books/world/howdy/', { wild:'world/howdy/' });
355355

356356
// console.log('/books/*?');
357-
toLooseExec('/books/*?', '/books', false);
357+
toLooseExec('/books/*?', '/books', { wild:null });
358358
toLooseExec('/books/*?', '/books/', { wild:null });
359359
toLooseExec('/books/*?', '/books/world', { wild:'world' });
360360
toLooseExec('/books/*?', '/books/world/', { wild:'world/' });

0 commit comments

Comments
 (0)