Skip to content

Commit 0573304

Browse files
committed
chore: update readme docs
1 parent 0d0e89a commit 0573304

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

readme.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,48 @@ exec('/users/lukeed/repos/new', baz);
8787

8888
> **Important:** When matching/testing against a generated RegExp, your path **must** begin with a leading slash (`"/"`)!
8989
90+
## Regular Expressions
91+
92+
For fine-tuned control, you may pass a `RegExp` value directly to `regexparam` as its only parameter.
93+
94+
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.
95+
96+
This also means that you must manage and parse your own `keys`~!<br>
97+
You may use [named capture groups](https://javascript.info/regexp-groups#named-groups) or traverse the matched segments manually the "old-fashioned" way:
98+
99+
> **Important:** Please check your target browsers' and target [Node.js runtimes' support](https://node.green/#ES2018-features--RegExp-named-capture-groups)!
100+
101+
```js
102+
// Named capture group
103+
const named = regexparam(/^\/posts[/](?<year>[0-9]{4})[/](?<month>[0-9]{2})[/](?<title>[^\/]+)/i);
104+
const { groups } = named.pattern.exec('/posts/2019/05/hello-world');
105+
console.log(groups);
106+
//=> { year: '2019', month: '05', title: 'hello-world' }
107+
108+
// Widely supported / "Old-fashioned"
109+
const named = regexparam(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i);
110+
const [url, year, month, title] = named.pattern.exec('/posts/2019/05/hello-world');
111+
console.log(year, month, title);
112+
//=> 2019 05 hello-world
113+
```
114+
90115

91116
## API
92117

118+
There are two API variants:
119+
120+
1) When passing a `String` input, the `loose` parameter is able to affect the output. [View API](#regexparamstr-loose)
121+
122+
2) When passing a `RegExp` value, that must be `regexparam`'s _only_ argument.<br>
123+
Your pattern is saved as written, so `loose` is ignored entirely. [View API](#regexparamrgx)
124+
93125
### regexparam(str, loose)
94126
Returns: `Object`
95127

128+
Returns a `{ keys, pattern }` object, where `pattern` is a generated `RegExp` instance and `keys` is a list of extracted parameter names.
129+
96130
#### str
97-
Type: `String`
131+
Type: `String` or `RegExp`
98132

99133
The route/pathing string to convert.
100134

@@ -117,6 +151,18 @@ rgx('/users/:name').pattern.test('/users/lukeed/repos'); //=> false
117151
rgx('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true
118152
```
119153

154+
### regexparam(rgx)
155+
Returns: `Object`
156+
157+
Returns a `{ keys, pattern }` object, where pattern is _identical_ to your `rgx` and `keys` is `false`, always.
158+
159+
#### rgx
160+
Type: `RegExp`
161+
162+
Your RegExp pattern.
163+
164+
> **Important:** This pattern is used _as is_! No parsing or interpreting is done on your behalf.
165+
120166

121167
## Related
122168

0 commit comments

Comments
 (0)