Skip to content

Commit

Permalink
Support arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Aug 27, 2024
1 parent e9a7e01 commit a84e7dc
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 128 deletions.
11 changes: 5 additions & 6 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ fn("/users/123/delete");

## Match

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

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

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

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

### Parse

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`.
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`.

- **path** A string.
- **options** _(optional)_
Expand All @@ -133,7 +132,7 @@ const tokens = [
{ type: "parameter", name: "foo" },
];
const path = new TokenData(tokens, "/");
const fn = $match(path);
const fn = match(path);

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

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

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.
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. `"\\("`.

### Missing parameter name

Expand Down
61 changes: 4 additions & 57 deletions src/cases.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ export const PARSER_TESTS: ParserTestSet[] = [
],
},
{
path: "/*star",
path: "/*path",
expected: [
{ type: "text", value: "/" },
{ type: "wildcard", name: "star" },
{ type: "wildcard", name: "path" },
],
},
];
Expand Down Expand Up @@ -131,7 +131,6 @@ export const COMPILE_TESTS: CompileTestSet[] = [
},
{
path: "/:test",
options: { validate: false },
tests: [
{ input: undefined, expected: null },
{ input: {}, expected: null },
Expand All @@ -141,24 +140,14 @@ export const COMPILE_TESTS: CompileTestSet[] = [
},
{
path: "/:test",
options: { validate: false, encode: false },
options: { encode: false },
tests: [
{ input: undefined, expected: null },
{ input: {}, expected: null },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
],
},
{
path: "/:test",
options: { encode: encodeURIComponent },
tests: [
{ input: undefined, expected: null },
{ input: {}, expected: null },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: "/123%2Fxyz" },
],
},
{
path: "/:test",
options: { encode: () => "static" },
Expand All @@ -170,58 +159,16 @@ export const COMPILE_TESTS: CompileTestSet[] = [
],
},
{
path: "{/:test}?",
path: "{/:test}",
options: { encode: false },
tests: [
{ input: undefined, expected: "" },
{ input: {}, expected: "" },
{ input: { test: undefined }, expected: "" },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: null },
],
},
{
path: "/:test(.*)",
options: { encode: false },
tests: [
{ input: undefined, expected: null },
{ input: {}, expected: null },
{ input: { test: "" }, expected: "/" },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
],
},
{
path: "{/:test}*",
tests: [
{ input: undefined, expected: "" },
{ input: {}, expected: "" },
{ input: { test: [] }, expected: "" },
{ input: { test: [""] }, expected: null },
{ input: { test: ["123"] }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: null },
{ input: { test: ["123", "xyz"] }, expected: "/123/xyz" },
],
},
{
path: "{/:test}*",
options: { encode: false },
tests: [
{ input: undefined, expected: "" },
{ input: {}, expected: "" },
{ input: { test: "" }, expected: null },
{ input: { test: "123" }, expected: "/123" },
{ input: { test: "123/xyz" }, expected: "/123/xyz" },
{ input: { test: ["123", "xyz"] }, expected: null },
],
},
{
path: "/{<:foo>}+",
tests: [
{ input: undefined, expected: null },
{ input: { foo: ["x", "y", "z"] }, expected: "/<x><y><z>" },
],
},
];

/**
Expand Down
Loading

0 comments on commit a84e7dc

Please sign in to comment.