Skip to content

Commit 8b292f8

Browse files
committed
feat: add loose parameter
1 parent 934fc01 commit 8b292f8

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

readme.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,33 @@ exec('/users/lukeed/repos/new', baz);
9090

9191
## API
9292

93-
### regexparam(str)
94-
93+
### regexparam(str, loose)
9594
Returns: `Object`
9695

9796
#### str
98-
9997
Type: `String`
10098

10199
The route/pathing string to convert.
102100

103101
> **Note:** It does not matter if your `str` begins with a `/` — it will be added if missing.
104102
103+
#### loose
104+
Type: `Boolean`<br>
105+
Default: `false`
106+
107+
Should the `RegExp` match URLs that are longer than the [`str`](#str) pattern itself?<br>
108+
By default, the generated `RegExp` will test that the URL begins and _ends with_ the pattern.
109+
110+
```js
111+
const rgx = require('regexparam');
112+
113+
rgx('/users').pattern.test('/users/lukeed'); //=> false
114+
rgx('/users', true).pattern.test('/users/lukeed'); //=> true
115+
116+
rgx('/users/:name').pattern.test('/users/lukeed/repos'); //=> false
117+
rgx('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true
118+
```
119+
105120

106121
## Related
107122

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default function (str) {
1+
export default function (str, loose) {
22
var c, o, tmp, ext, keys=[], pattern='', arr=str.split('/');
33
arr[0] || arr.shift();
44

@@ -20,6 +20,6 @@ export default function (str) {
2020

2121
return {
2222
keys: keys,
23-
pattern: new RegExp('^' + pattern + '\/?$', 'i')
23+
pattern: new RegExp('^' + pattern + (loose ? '(?:$|\/)' : '\/?$'), 'i')
2424
};
2525
}

0 commit comments

Comments
 (0)