Gathering detailed insights and metrics for regexparam
Approximately 800 new packages are uploaded to the npm registry every day. This number can vary, but it reflects the active and growing nature of the JavaScript development community.
Gathering detailed insights and metrics for regexparam
Approximately 800 new packages are uploaded to the npm registry every day. This number can vary, but it reflects the active and growing nature of the JavaScript development community.
npm install regexparam
99.4
Supply Chain
99.4
Quality
77.6
Maintenance
100
Vulnerability
100
License
568 Stars
84 Commits
23 Forks
10 Watching
1 Branches
5 Contributors
Updated on 16 Nov 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
2.7%
103,911
Compared to previous day
Last week
5.2%
547,335
Compared to previous week
Last month
3.9%
2,248,779
Compared to previous month
Last year
75.8%
21,658,785
Compared to previous year
A tiny (399B) utility that converts route patterns into RegExp. Limited alternative to
path-to-regexp
🙇
With regexparam
, you may turn a pathing string (eg, /users/:id
) into a regular expression.
An object with shape of { keys, pattern }
is returned, where pattern
is the RegExp
and keys
is an array of your parameter name(s) in the order that they appeared.
Unlike path-to-regexp
, this module does not create a keys
dictionary, nor mutate an existing variable. Also, this only ships a parser, which only accept strings. Similarly, and most importantly, regexparam
only handles basic pathing operators:
/foo
, /foo/bar
)/:title
, /books/:title
, /books/:genre/:title
)/movies/:title.mp4
, /movies/:title.(mp4|mov)
)/:title?
, /books/:title?
, /books/:genre/:title?
)*
, /books/*
, /books/:genre/*
)/books/*?
)This module exposes three module definitions:
dist/index.js
dist/index.mjs
dist/index.min.js
$ npm install --save regexparam
1import { parse, inject } from 'regexparam'; 2 3// Example param-assignment 4function exec(path, result) { 5 let i=0, out={}; 6 let matches = result.pattern.exec(path); 7 while (i < result.keys.length) { 8 out[ result.keys[i] ] = matches[++i] || null; 9 } 10 return out; 11} 12 13 14// Parameter, with Optional Parameter 15// --- 16let foo = parse('/books/:genre/:title?') 17// foo.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?\/?$/i 18// foo.keys => ['genre', 'title'] 19 20foo.pattern.test('/books/horror'); //=> true 21foo.pattern.test('/books/horror/goosebumps'); //=> true 22 23exec('/books/horror', foo); 24//=> { genre: 'horror', title: null } 25 26exec('/books/horror/goosebumps', foo); 27//=> { genre: 'horror', title: 'goosebumps' } 28 29 30// Parameter, with suffix 31// --- 32let bar = parse('/movies/:title.(mp4|mov)'); 33// bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)\/?$/i 34// bar.keys => ['title'] 35 36bar.pattern.test('/movies/narnia'); //=> false 37bar.pattern.test('/movies/narnia.mp3'); //=> false 38bar.pattern.test('/movies/narnia.mp4'); //=> true 39 40exec('/movies/narnia.mp4', bar); 41//=> { title: 'narnia' } 42 43 44// Wildcard 45// --- 46let baz = parse('users/*'); 47// baz.pattern => /^\/users\/(.*)\/?$/i 48// baz.keys => ['*'] 49 50baz.pattern.test('/users'); //=> false 51baz.pattern.test('/users/lukeed'); //=> true 52baz.pattern.test('/users/'); //=> true 53 54 55// Optional Wildcard 56// --- 57let baz = parse('/users/*?'); 58// baz.pattern => /^\/users(?:\/(.*))?(?=$|\/)/i 59// baz.keys => ['*'] 60 61baz.pattern.test('/users'); //=> true 62baz.pattern.test('/users/lukeed'); //=> true 63baz.pattern.test('/users/'); //=> true 64 65 66// Injecting 67// --- 68 69inject('/users/:id', { 70 id: 'lukeed' 71}); //=> '/users/lukeed' 72 73inject('/movies/:title.mp4', { 74 title: 'narnia' 75}); //=> '/movies/narnia.mp4' 76 77inject('/:foo/:bar?/:baz?', { 78 foo: 'aaa' 79}); //=> '/aaa' 80 81inject('/:foo/:bar?/:baz?', { 82 foo: 'aaa', 83 baz: 'ccc' 84}); //=> '/aaa/ccc' 85 86inject('/posts/:slug/*', { 87 slug: 'hello', 88}); //=> '/posts/hello' 89 90inject('/posts/:slug/*', { 91 slug: 'hello', 92 '*': 'x/y/z', 93}); //=> '/posts/hello/x/y/z' 94 95// Missing non-optional value 96// ~> keeps the pattern in output 97inject('/hello/:world', { 98 abc: 123 99}); //=> '/hello/:world'
Important: When matching/testing against a generated RegExp, your path must begin with a leading slash (
"/"
)!
For fine-tuned control, you may pass a RegExp
value directly to regexparam
as its only parameter.
In these situations, regexparam
does not parse nor manipulate your pattern in any way! Because of this, regexparam
has no "insight" on your route, and instead trusts your input fully. In code, this means that the return value's keys
is always equal to false
and the pattern
is identical to your input value.
This also means that you must manage and parse your own keys
~!
You may use named capture groups or traverse the matched segments manually the "old-fashioned" way:
Important: Please check your target browsers' and target Node.js runtimes' support!
1// Named capture group 2const named = regexparam.parse(/^\/posts[/](?<year>[0-9]{4})[/](?<month>[0-9]{2})[/](?<title>[^\/]+)/i); 3const { groups } = named.pattern.exec('/posts/2019/05/hello-world'); 4console.log(groups); 5//=> { year: '2019', month: '05', title: 'hello-world' } 6 7// Widely supported / "Old-fashioned" 8const named = regexparam.parse(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i); 9const [url, year, month, title] = named.pattern.exec('/posts/2019/05/hello-world'); 10console.log(year, month, title); 11//=> 2019 05 hello-world
Returns: Object
Parse a route pattern into an equivalent RegExp pattern. Also collects the names of pattern's parameters as a keys
array. An input
that's already a RegExp is kept as is, and regexparam
makes no additional insights.
Returns a { keys, pattern }
object, where pattern
is always a RegExp
instance and keys
is either false
or a list of extracted parameter names.
Important: The
keys
will always befalse
wheninput
is a RegExp and it will always be an Array wheninput
is a string.
Type: string
or RegExp
When input
is a string, it's treated as a route pattern and an equivalent RegExp is generated.
Note: It does not matter if
input
strings begin with a/
— it will be added if missing.
When input
is a RegExp, it will be used as is – no modifications will be made.
Type: boolean
Default: false
Should the RegExp
match URLs that are longer than the str
pattern itself?
By default, the generated RegExp
will test that the URL begins and ends with the pattern.
Important: When
input
is a RegExp, theloose
argument is ignored!
1const { parse } = require('regexparam'); 2 3parse('/users').pattern.test('/users/lukeed'); //=> false 4parse('/users', true).pattern.test('/users/lukeed'); //=> true 5 6parse('/users/:name').pattern.test('/users/lukeed/repos'); //=> false 7parse('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true
Returns: string
Returns a new string by replacing the pattern
segments/parameters with their matching values.
Important: Named segments (eg,
/:name
) that do not have avalues
match will be kept in the output. This is true except for optional segments (eg,/:name?
) and wildcard segments (eg,/*
).
Type: string
The route pattern that to receive injections.
Type: Record<string, string>
The values to be injected. The keys within values
must match the pattern
's segments in order to be replaced.
Note: To replace a wildcard segment (eg,
/*
), define avalues['*']
key.
As of version 1.3.0
, you may use regexparam
with Deno. These options are all valid:
1// The official Deno registry: 2import regexparam from 'https://deno.land/x/regexparam/src/index.js'; 3// Third-party CDNs with ESM support: 4import regexparam from 'https://cdn.skypack.dev/regexparam'; 5import regexparam from 'https://esm.sh/regexparam';
Note: All registries support versioned URLs, if desired.
The above examples always resolve to the latest published version.
RegExp
s.MIT © Luke Edwards
No vulnerabilities found.
Reason
license file detected
Details
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
Found 4/19 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-11
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More