A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇♂️
Optional wildcard patterns are now fully supported! (#25): 422f630, 5362fba
Previously, an optional wildcard pattern would parse but it didn't behave correctly.
Essentially the "optional" part (?
) was ignored, meaning that optional wildcards were no different than wildcards.
This has been fixed (thanks @benmerckx), but doing so changed the generated RegExp
(and thus, the matching behavior) for the optional wildcard pattern use case.
All other usage is unaffected!
The majority of use cases will see no difference – especially since the previous behavior was unwanted/unexpected anyway.
let { pattern } = parse('/users/*?');
// Before:
pattern.test('/users'); //=> false << wonky ❌
pattern.test('/users/'); //=> true
pattern.test('/users/123'); //=> true
// After:
pattern.test('/users'); //=> true << YAY ✅
pattern.test('/users/'); //=> true
pattern.test('/users/123'); //=> true
Renamed the reserved wildcard key (for wildcard value segment) from wild
to *
:
This allows users to now construct named route patterns using the "wild" placeholder
// Before:
parse("/books/:genre/*"); //=> { keys: ["genre", "wild"], ... }
parse("/:wild"); //=> { keys: ["wild"], ... }
// ^^ this meant a named parameter would present like a wildcard, bad! ❌
// After:
parse("/books/:genre/*"); //=> { keys: ["genre", "*"], ... }
parse("/:wild"); //=> { keys: ["wild"], ... }
// ^^ this allows named parameter to look like a named parameter, good! ✅
Full Changelog: https://github.com/lukeed/regexparam/compare/v2.0.2...v3.0.0
Remove unnecessary /*#__PURE__*/
comment (#28): 93708aa
Fixed a warning when using recent Rollup/Vite versions
Add "default"
export condition for Svelte users (#26): 57af4a5
Not sure why 😅 but can't hurt
Full Changelog: https://github.com/lukeed/regexparam/compare/v2.0.1...v2.0.2
types
condition for TypeScript Node16
module resolution (#23): 25f6ebe
Thank you @daaku~!
Full Changelog: https://github.com/lukeed/regexparam/compare/v2.0.0...v2.0.1
Convert default
export to named parse
export: d439b9c
Important: No functionality has changed! Simply -how- it's imported
-- import regexparam from 'regexparam';
++ import { parse } from 'regexparam';
Require Node 8.x minimum runtime: bc36b93 Previously required Node 6.x
Support native ESM imports via "exports" mapping: f2604b2 Note: This is potentially breaking for users of Node 13.0 thru 13.7 – upgrade! All of Node 13.x is officially obsolete!
Conditional exports were defined, which means that CommonJS usage is still supported.
Added new inject
function: 9c1a166, 3958c19, 3579e63
Convenience function for injecting values into a route pattern string.
Note: This is fully tree-shakable! Your bundle won't include it if you don't use it.
import { inject } from 'regexparam';
inject('/users/:id', {
id: 'lukeed'
}); //=> '/users/lukeed'
inject('/movies/:title.mp4', {
title: 'narnia'
}); //=> '/movies/narnia.mp4'
inject('/:foo/:bar?/:baz?', {
foo: 'aaa'
}); //=> '/aaa'
inject('/:foo/:bar?/:baz?', {
foo: 'aaa',
baz: 'ccc'
}); //=> '/aaa/ccc'
inject('/posts/:slug/*', {
slug: 'hello',
}); //=> '/posts/hello'
inject('/posts/:slug/*', {
slug: 'hello',
wild: 'x/y/z',
}); //=> '/posts/hello/x/y/z'
// Missing non-optional value
// ~> keeps the pattern in output
inject('/hello/:world', {
abc: 123
}); //=> '/hello/:world'
uvu
for test runner: 76a7d1dc8
for test coverage: 5126f71.gitignore
patterns: 1524a64bundt
version: 59a1f35RegExp
patterns (#7): a4b91bbRegExp
patterns: 7a4f933, d6d4c3bRegExp
patterns: 0d0e89aRegExp
patterns: 0573304, dd056cbAdd loose
parameter, allowing the RegExp
to match URLs that would otherwise be too long: 8b292f8
const rgx = require('regexparam');
rgx('/users').pattern.test('/users/lukeed'); //=> false
rgx('/users', true).pattern.test('/users/lukeed'); //=> true
rgx('/users/:name').pattern.test('/users/lukeed/repos'); //=> false
rgx('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true
loose
test suite: 2ed86b2"exec"
tests, using false
to describe no match: 934fc01Added suffix support for parameter matching: 0896539
const regexparam = require('regexparam');
const myURL = '/movies/narnia.mp4';
// Before:
let old = regexparam('/movies/:title');
exec(myURL, old);
//=> { title: 'narnia.mp4' }
// Now have to parse this String, remove ".mp4", etc
// Except, maybe I didn't want to allow MP4 files at all?
// After:
let now = regexparam('/movies/:title.mp4');
exec(myURL, now);
//=> { title: 'narnia' }
This is great because my pattern can exclude file extensions (or whatever) that I don't want to permit. In this case, the URL "/movies/narnia.mov"
won't match the pattern, and so it will be ignored entirely.
I can also define a group of suffices to allow.
Let's change the example to allow MP4 and MOV files:
// After (v2):
let now = regexparam('/movies/:title.(mp4|mov)');
exec('/movies/narnia.mp4', now);
//=> { title: 'narnia' }
exec('/movies/narnia.mov', now);
//=> { title: 'narnia' }
exec('/movies/narnia.wav', now);
//=> {} (no match)