Skip to content

Commit a84e7dc

Browse files
committed
Support arrays
1 parent e9a7e01 commit a84e7dc

File tree

3 files changed

+99
-128
lines changed

3 files changed

+99
-128
lines changed

Readme.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ fn("/users/123/delete");
6262

6363
## Match
6464

65-
The `match` function returns a function for transforming paths into parameters:
65+
The `match` function returns a function for matching strings against a path:
6666

67-
- **path** A string.
67+
- **path** String or array of strings.
6868
- **options** _(optional)_ (See [parse](#parse) for more options)
6969
- **sensitive** Regexp will be case sensitive. (default: `false`)
7070
- **end** Validate the match reaches the end of the string. (default: `true`)
@@ -83,7 +83,6 @@ The `compile` function will return a function for transforming parameters into a
8383

8484
- **path** A string.
8585
- **options** (See [parse](#parse) for more options)
86-
- **sensitive** Regexp will be case sensitive. (default: `false`)
8786
- **encode** Function for encoding input strings for output into the path, or `false` to disable entirely. (default: `encodeURIComponent`)
8887

8988
```js
@@ -110,7 +109,7 @@ toPathRaw({ id: "%3A%2F" }); //=> "/user/%3A%2F"
110109

111110
### Parse
112111

113-
The `parse` function accepts a string and returns `TokenData`, the set of tokens and other metadata parsed from the input string. `TokenData` is can used with `$match` and `$compile`.
112+
The `parse` function accepts a string and returns `TokenData`, the set of tokens and other metadata parsed from the input string. `TokenData` is can used with `match` and `compile`.
114113

115114
- **path** A string.
116115
- **options** _(optional)_
@@ -133,7 +132,7 @@ const tokens = [
133132
{ type: "parameter", name: "foo" },
134133
];
135134
const path = new TokenData(tokens, "/");
136-
const fn = $match(path);
135+
const fn = match(path);
137136

138137
fn("/test"); //=> { path: '/test', index: 0, params: { foo: 'test' } }
139138
```
@@ -152,7 +151,7 @@ In past releases, `?`, `*`, and `+` were used to denote optional or repeating pa
152151

153152
### Unexpected `(`, `)`, `[`, `]`, etc.
154153

155-
Previous major versions contained features that aren't currently supported, such as custom prefixes and suffixes for parameters, and the ability to set a parameter regexp. To avoid ambiguity any character used to alter the regexp of a previous release has been reserved in this release.
154+
Previous versions of Path-to-RegExp used these for RegExp features. This version no longer supports them so they've been reserved to avoid ambiguity. To use these characters literally, escape them with a backslash, e.g. `"\\("`.
156155

157156
### Missing parameter name
158157

src/cases.spec.ts

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ export const PARSER_TESTS: ParserTestSet[] = [
7979
],
8080
},
8181
{
82-
path: "/*star",
82+
path: "/*path",
8383
expected: [
8484
{ type: "text", value: "/" },
85-
{ type: "wildcard", name: "star" },
85+
{ type: "wildcard", name: "path" },
8686
],
8787
},
8888
];
@@ -131,7 +131,6 @@ export const COMPILE_TESTS: CompileTestSet[] = [
131131
},
132132
{
133133
path: "/:test",
134-
options: { validate: false },
135134
tests: [
136135
{ input: undefined, expected: null },
137136
{ input: {}, expected: null },
@@ -141,24 +140,14 @@ export const COMPILE_TESTS: CompileTestSet[] = [
141140
},
142141
{
143142
path: "/:test",
144-
options: { validate: false, encode: false },
143+
options: { encode: false },
145144
tests: [
146145
{ input: undefined, expected: null },
147146
{ input: {}, expected: null },
148147
{ input: { test: "123" }, expected: "/123" },
149148
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
150149
],
151150
},
152-
{
153-
path: "/:test",
154-
options: { encode: encodeURIComponent },
155-
tests: [
156-
{ input: undefined, expected: null },
157-
{ input: {}, expected: null },
158-
{ input: { test: "123" }, expected: "/123" },
159-
{ input: { test: "123/xyz" }, expected: "/123%2Fxyz" },
160-
],
161-
},
162151
{
163152
path: "/:test",
164153
options: { encode: () => "static" },
@@ -170,58 +159,16 @@ export const COMPILE_TESTS: CompileTestSet[] = [
170159
],
171160
},
172161
{
173-
path: "{/:test}?",
162+
path: "{/:test}",
174163
options: { encode: false },
175164
tests: [
176165
{ input: undefined, expected: "" },
177166
{ input: {}, expected: "" },
178167
{ input: { test: undefined }, expected: "" },
179168
{ input: { test: "123" }, expected: "/123" },
180-
{ input: { test: "123/xyz" }, expected: null },
181-
],
182-
},
183-
{
184-
path: "/:test(.*)",
185-
options: { encode: false },
186-
tests: [
187-
{ input: undefined, expected: null },
188-
{ input: {}, expected: null },
189-
{ input: { test: "" }, expected: "/" },
190-
{ input: { test: "123" }, expected: "/123" },
191169
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
192170
],
193171
},
194-
{
195-
path: "{/:test}*",
196-
tests: [
197-
{ input: undefined, expected: "" },
198-
{ input: {}, expected: "" },
199-
{ input: { test: [] }, expected: "" },
200-
{ input: { test: [""] }, expected: null },
201-
{ input: { test: ["123"] }, expected: "/123" },
202-
{ input: { test: "123/xyz" }, expected: null },
203-
{ input: { test: ["123", "xyz"] }, expected: "/123/xyz" },
204-
],
205-
},
206-
{
207-
path: "{/:test}*",
208-
options: { encode: false },
209-
tests: [
210-
{ input: undefined, expected: "" },
211-
{ input: {}, expected: "" },
212-
{ input: { test: "" }, expected: null },
213-
{ input: { test: "123" }, expected: "/123" },
214-
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
215-
{ input: { test: ["123", "xyz"] }, expected: null },
216-
],
217-
},
218-
{
219-
path: "/{<:foo>}+",
220-
tests: [
221-
{ input: undefined, expected: null },
222-
{ input: { foo: ["x", "y", "z"] }, expected: "/<x><y><z>" },
223-
],
224-
},
225172
];
226173

227174
/**

0 commit comments

Comments
 (0)