lukeed / regexparam Goto Github PK
View Code? Open in Web Editor NEWA tiny (394B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` ๐โโ๏ธ
License: MIT License
A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` ๐โโ๏ธ
License: MIT License
Typescript and esm support would be great !
vite v5.0.2 building for production...
node_modules/regexparam/dist/index.mjs (28:10) A comment
"/*#__PURE__*/"
in "node_modules/regexparam/dist/index.mjs" contains an annotation that Rollup cannot interpret due to the position of the comment. The comment will be removed to avoid issues.
Hi! I decided to build own router and stuck on small mismatch with path-to-regexp behaviour.
I expect /:page
matching /dashboard/activities
do not include slash in the end of dashboard in exec array.
https://codesandbox.io/s/intelligent-fermat-mwv42
Hey @lukeed
Thanks for the great module. I just wanted to say it would be great to have support for named wildcards.
So for example
@/:*branch/~/:*id
Right now it names both of them as wild
, so the first one would get overtwriten
It would be nice to be able to match 0 or more characters for a wildcard:
/books/:genre/*?
=> match both /books/abc
and /books/abc/xyz
I am using this library to convert Express routes to RegExp
s and then give those RegExp
s to Workbox.
My issue is that Workbox matches the generated RegExp
s to absolute URLs. Therefore, I added an option to optionally make the matching loose at the start as well:
export default function (str, looseStart, looseEnd) {
if (str instanceof RegExp) return { keys:false, pattern:str };
var c, o, tmp, ext, keys=[], pattern='', arr = str.split('/');
arr[0] || arr.shift();
while (tmp = arr.shift()) {
c = tmp[0];
if (c === '*') {
keys.push('wild');
pattern += '/(.*)';
} else if (c === ':') {
o = tmp.indexOf('?', 1);
ext = tmp.indexOf('.', 1);
keys.push( tmp.substring(1, !!~o ? o : !!~ext ? ext : tmp.length) );
pattern += !!~o && !~ext ? '(?:/([^/]+?))?' : '/([^/]+?)';
if (!!~ext) pattern += (!!~o ? '?' : '') + '\\' + tmp.substring(ext);
} else {
pattern += '/' + tmp;
}
}
return {
keys: keys,
pattern: new RegExp((looseStart ? '' : '^') + pattern + (looseEnd ? '(?=$|\\?|\/)' : '\/?$'), 'i')
};
}
So,
rgx('/users/:name', true).pattern.test('https://example.com/users/lukeed'); //=> true
rgx('/users/:name', true, true).pattern.test('https://example.com/users/lukeed/repos'); //=> true
Would you consider adding this feature to the library?
Hi,
would you modify the generated RegExp
to match URL
s with search parameter list when the loose option is set to true
?
rgx('/users/:name', true).pattern.test('/users/lukeed?q=hello'); //=> true
Please see lukeed/trouter#25
The issue stems from \/?
being matched at the end of all patterns in regexparam: https://github.com/aral/regexparam/blob/master/src/index.js#L28
node_modules/regexparam/dist/regexparam.js:3
var c, o, tmp, ext, keys=[], pattern='', arr = str.split('/');
^
TypeError: str.split is not a function
at module.exports
when I use
app.post((req, res) => {
console.log(req.body);
res.end();
})
in my server.js
Hey,
this might be a longshot and not related to your repo but I tried to use your library because it matches exactly what I needed but create react app doesn't seem to be able to minify it.
Here's the error message I receive:
yarn run v1.5.1
$ react-scripts build
Creating an optimized production build...
Failed to compile.
Failed to minify the code from this file:
./node_modules/regexparam/dist/regexparam.es.js:1
Read more here: http://bit.ly/2tRViJ9
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c react-scripts build
Directory: /Users/antonio/dev/sandbox/regex-param-cra
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/Users/antonio/dev/sandbox/regex-param-cra/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
I created a repo to reproduce the issue: repo
As you can see I simply import regexpath in the index.js
To reproduce just run: yarn && yarn build
Cheers,
Would be cool to have this library published on Deno. Although, it imports fine from import rg from 'https://esm.sh/regexparam'
but having it on the Deno registry could bring more users to the library.
In case you're interested: https://dev.to/craigmorten/how-to-publish-deno-modules-2cg6
It looks like the regex output changes between versions 1.2.1
and 1.3.0
when the loose
property is set to true
:
v.1.2.1: /^\/users(?:$|\/)/i
v1.3.0: /^\/users(?=$|\/)/i
I get consistent output when it's not set though:
v1.2.1: /^\/users\/?$/i
v.1.3.0: /^\/users\/?$/i
Looks like there's no test case for this
parse("/*.js")
In the example above, the regex includes .js
in the wild
param.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.