Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional wildcards #25

Merged
merged 5 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type RouteParams<T extends string> =
? { [K in P]: string }
: T extends `${string}*`
? { wild: string }
: T extends `${string}*?`
? { wild?: string }
: {};

export function inject<T extends string>(route: T, values: RouteParams<T>): string;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "regexparam",
"version": "2.0.1",
"repository": "lukeed/regexparam",
"description": "A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇‍",
"description": "A tiny (405B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇‍",
"unpkg": "dist/index.min.js",
"module": "dist/index.mjs",
"main": "dist/index.js",
Expand Down
22 changes: 20 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# regexparam [![CI](https://github.com/lukeed/regexparam/actions/workflows/ci.yml/badge.svg)](https://github.com/lukeed/regexparam/actions/workflows/ci.yml)

> A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇
> A tiny (405B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇

With `regexparam`, you may turn a pathing string (eg, `/users/:id`) into a regular expression.

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

This module exposes three module definitions:

Expand All @@ -35,7 +36,7 @@ import { parse, inject } from 'regexparam';
// Example param-assignment
function exec(path, result) {
let i=0, out={};
let matches = result.pattern.exec(path);
let matches = result.pattern.exec(path) || [];
while (i < result.keys.length) {
out[ result.keys[i] ] = matches[++i] || null;
}
Expand Down Expand Up @@ -82,6 +83,23 @@ let baz = parse('users/*');
baz.pattern.test('/users'); //=> false
baz.pattern.test('/users/lukeed'); //=> true

exec('/users', baz);
//=> { wild: 'lukeed/repos/new' }
exec('/users/lukeed/repos/new', baz);
//=> { wild: 'lukeed/repos/new' }


// Optional Wildcard
// ---
let baz = parse('/users/*?');
// baz.pattern => /^\/users(?:\/(.*))?(?=$|\/)/i
// baz.keys => ['wild']

baz.pattern.test('/users'); //=> true
baz.pattern.test('/users/lukeed'); //=> true

exec('/users', baz);
//=> { wild: null }
exec('/users/lukeed/repos/new', baz);
//=> { wild: 'lukeed/repos/new' }

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export function parse(str, loose) {
c = tmp[0];
if (c === '*') {
keys.push('wild');
pattern += '/(.*)';
pattern += tmp[1] === '?' ? '(?:/(.*))?' : '/(.*)';
} else if (c === ':') {
o = tmp.indexOf('?', 1);
ext = tmp.indexOf('.', 1);
Expand Down
3 changes: 3 additions & 0 deletions test/inject.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ run('/foo/:id/:a?/:b?/:bar?', '/foo/123/xxx', { id: 123, bar: 'xxx' });
run('/foo/:id/:a?/:b?/:bar?', '/foo/123/aa/xxx', { id: 123, a: 'aa', bar: 'xxx' });

run('/foo/:bar/*', '/foo/123', { bar: '123' });
run('/foo/:bar/*?', '/foo/123', { bar: '123' });

run('/foo/:bar/*', '/foo/123/aa/bb/cc', { bar: '123', wild: 'aa/bb/cc' });
run('/foo/:bar/*?', '/foo/123/aa/bb/cc', { bar: '123', wild: 'aa/bb/cc' });

// NOTE: Missing non-optional values
// ---
Expand Down
4 changes: 2 additions & 2 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ test('execs', () => {
toExec('/books/*', '/books/world/howdy/', { wild:'world/howdy/' });

// console.log('/books/*?');
toExec('/books/*?', '/books', false);
toExec('/books/*?', '/books', { wild:null });
toExec('/books/*?', '/books/', { wild:null });
toExec('/books/*?', '/books/world', { wild:'world' });
toExec('/books/*?', '/books/world/', { wild:'world/' });
Expand Down Expand Up @@ -354,7 +354,7 @@ test('execs :: loose', () => {
toLooseExec('/books/*', '/books/world/howdy/', { wild:'world/howdy/' });

// console.log('/books/*?');
toLooseExec('/books/*?', '/books', false);
toLooseExec('/books/*?', '/books', { wild:null });
toLooseExec('/books/*?', '/books/', { wild:null });
toLooseExec('/books/*?', '/books/world', { wild:'world' });
toLooseExec('/books/*?', '/books/world/', { wild:'world/' });
Expand Down