convert-route
v0.1.2
Published
Convert between rou3, Next.js, and path-to-regexp route patterns
Maintainers
Readme
convert-route
Convert between path-to-regexp, rou3, next.js, RegExp, etc. route patterns.
Table of Contents
Installation
npm install convert-route
# or
yarn add convert-route
# or
pnpm add convert-routeUsage
import { fromPathToRegexpV8 } from 'convert-route/path-to-regexp-v8';
import { toRou3 } from 'convert-route/rou3';
// Convert from path-to-regexp v8 format to rou3 format
const rou3Pattern = toRou3(fromPathToRegexpV8('/users/*id'));
console.log(rou3Pattern); // ['/users/**:id']Supported Features
| Feature | Next.js | rou3 | path-to-regexp v6 | path-to-regexp v8 | RegExp |
|----------------------|----------------|----------------|-------------------|-------------------|--------|
| Named Segments | [param] | :param | :param | :param | Yes |
| Optional Segments | No | *, *:param | :param? | {/:param} | Yes |
| Catch-all (Wildcard) | [...param] | **:param | :param+ | *param | Yes |
| Optional Catch-all | [[...param]] | ** | :param* | {/*param} | Yes |
| Suffix Matching | No | WIP | WIP | WIP | WIP |
| Prefix Matching | No | WIP | WIP | WIP | WIP |
rou3
import { fromRou3, toRou3 } from 'convert-route/rou3';
const intermediateRepresentation = fromRou3('/users/:id');
const rou3Pattern = toRou3(intermediateRepresentation);
// Note: toRou3 returns an array of patterns since some patterns need to be expressed as a combination of multiple patterns
console.log(rou3Pattern); // ['/users/:id']Next.js
// Note: There is no `toNextFs` function. If you have a need for such helper, please open an issue
import { fromNextFs } from 'convert-route/next-fs';
const intermediateRepresentation = fromNextFs('/users/[id]');path-to-regexp v6
import { fromPathToRegexpV6, toPathToRegexpV6 } from 'convert-route/path-to-regexp-v6';
const intermediateRepresentation = fromPathToRegexpV6('/users/:id');
const pathToRegexV6Pattern = toPathToRegexpV6(intermediateRepresentation);
console.log(pathToRegexV6Pattern); // '/users/:id'path-to-regexp v8
import { fromPathToRegexpV8, toPathToRegexpV8 } from 'convert-route/path-to-regexp-v8';
const intermediateRepresentation = fromPathToRegexpV8('/users/:id');
const pathToRegexV8Pattern = toPathToRegexpV8(intermediateRepresentation);
console.log(pathToRegexV8Pattern); // '/users/:id'RegExp
import { toRegexp } from 'convert-route/regexp';
import { fromPathToRegexpV8 } from 'convert-route/path-to-regexp-v8';
const intermediateRepresentation = fromPathToRegexpV8('/users/:id');
const regexp = toRegexp(intermediateRepresentation);
console.log(regexp); // /^\/users\/(?<id>[^/]+)\/?$/