regexparam
Advanced tools
Comparing version 1.3.0 to 2.0.0
{ | ||
"name": "regexparam", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"repository": "lukeed/regexparam", | ||
"description": "A tiny (308B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇", | ||
"unpkg": "dist/regexparam.min.js", | ||
"module": "dist/regexparam.mjs", | ||
"main": "dist/regexparam.js", | ||
"types": "types.d.ts", | ||
"description": "A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to `path-to-regexp` 🙇", | ||
"unpkg": "dist/index.min.js", | ||
"module": "dist/index.mjs", | ||
"main": "dist/index.js", | ||
"types": "index.d.ts", | ||
"license": "MIT", | ||
@@ -14,12 +14,18 @@ "author": { | ||
"email": "luke.edwards05@gmail.com", | ||
"url": "lukeed.com" | ||
"url": "https://lukeed.com" | ||
}, | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"build": "bundt", | ||
"pretest": "npm run build", | ||
"test": "tape test/*.js | tap-spec" | ||
"test": "uvu -r esm test" | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
}, | ||
"./package.json": "./package.json" | ||
}, | ||
"files": [ | ||
@@ -32,9 +38,11 @@ "*.d.ts", | ||
"route", | ||
"routing" | ||
"routing", | ||
"inject", | ||
"parse" | ||
], | ||
"devDependencies": { | ||
"bundt": "^0.3.0", | ||
"tap-spec": "^4.1.1", | ||
"tape": "^4.8.0" | ||
"bundt": "1.1.2", | ||
"esm": "3.2.25", | ||
"uvu": "0.5.1" | ||
} | ||
} |
132
readme.md
@@ -1,4 +0,4 @@ | ||
# regexparam [![Build Status](https://badgen.now.sh/travis/lukeed/regexparam)](https://travis-ci.org/lukeed/regexparam) | ||
# regexparam [![CI](https://github.com/lukeed/regexparam/actions/workflows/ci.yml/badge.svg)](https://github.com/lukeed/regexparam/actions/workflows/ci.yml) | ||
> A tiny (308B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇 | ||
> A tiny (394B) utility that converts route patterns into RegExp. Limited alternative to [`path-to-regexp`](https://github.com/pillarjs/path-to-regexp) 🙇 | ||
@@ -17,6 +17,7 @@ With `regexparam`, you may turn a pathing string (eg, `/users/:id`) into a regular expression. | ||
This module exposes two module definitions: | ||
This module exposes three module definitions: | ||
* **CommonJS**: `dist/regexparam.js` | ||
* **ESModule**: `dist/regexparam.mjs` | ||
* **CommonJS**: [`dist/index.js`](https://unpkg.com/regexparam/dist/index.js) | ||
* **ESModule**: [`dist/index.mjs`](https://unpkg.com/regexparam/dist/index.mjs) | ||
* **UMD**: [`dist/index.min.js`](https://unpkg.com/regexparam/dist/index.min.js) | ||
@@ -33,3 +34,3 @@ ## Install | ||
```js | ||
const regexparam = require('regexparam'); | ||
import { parse, inject } from 'regexparam'; | ||
@@ -49,3 +50,3 @@ // Example param-assignment | ||
// --- | ||
let foo = regexparam('/books/:genre/:title?') | ||
let foo = parse('/books/:genre/:title?') | ||
// foo.pattern => /^\/books\/([^\/]+?)(?:\/([^\/]+?))?\/?$/i | ||
@@ -66,3 +67,3 @@ // foo.keys => ['genre', 'title'] | ||
// --- | ||
let bar = regexparam('/movies/:title.(mp4|mov)'); | ||
let bar = parse('/movies/:title.(mp4|mov)'); | ||
// bar.pattern => /^\/movies\/([^\/]+?)\.(mp4|mov)\/?$/i | ||
@@ -81,3 +82,3 @@ // bar.keys => ['title'] | ||
// --- | ||
let baz = regexparam('users/*'); | ||
let baz = parse('users/*'); | ||
// baz.pattern => /^\/users\/(.*)\/?$/i | ||
@@ -91,2 +92,38 @@ // baz.keys => ['wild'] | ||
//=> { wild: 'lukeed/repos/new' } | ||
// Injecting | ||
// --- | ||
inject('/users/:id', { | ||
id: 'lukeed' | ||
}); //=> '/users/lukeed' | ||
inject('/movies/:title.mp4', { | ||
title: 'narnia' | ||
}); //=> '/movies/narnia.mp4' | ||
inject('/:foo/:bar?/:baz?', { | ||
foo: 'aaa' | ||
}); //=> '/aaa' | ||
inject('/:foo/:bar?/:baz?', { | ||
foo: 'aaa', | ||
baz: 'ccc' | ||
}); //=> '/aaa/ccc' | ||
inject('/posts/:slug/*', { | ||
slug: 'hello', | ||
}); //=> '/posts/hello' | ||
inject('/posts/:slug/*', { | ||
slug: 'hello', | ||
wild: 'x/y/z', | ||
}); //=> '/posts/hello/x/y/z' | ||
// Missing non-optional value | ||
// ~> keeps the pattern in output | ||
inject('/hello/:world', { | ||
abc: 123 | ||
}); //=> '/hello/:world' | ||
``` | ||
@@ -109,3 +146,3 @@ | ||
// Named capture group | ||
const named = regexparam(/^\/posts[/](?<year>[0-9]{4})[/](?<month>[0-9]{2})[/](?<title>[^\/]+)/i); | ||
const named = regexparam.parse(/^\/posts[/](?<year>[0-9]{4})[/](?<month>[0-9]{2})[/](?<title>[^\/]+)/i); | ||
const { groups } = named.pattern.exec('/posts/2019/05/hello-world'); | ||
@@ -116,3 +153,3 @@ console.log(groups); | ||
// Widely supported / "Old-fashioned" | ||
const named = regexparam(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i); | ||
const named = regexparam.parse(/^\/posts[/]([0-9]{4})[/]([0-9]{2})[/]([^\/]+)/i); | ||
const [url, year, month, title] = named.pattern.exec('/posts/2019/05/hello-world'); | ||
@@ -126,23 +163,23 @@ console.log(year, month, title); | ||
There are two API variants: | ||
### regexparam.parse(input: RegExp) | ||
### regexparam.parse(input: string, loose?: boolean) | ||
Returns: `Object` | ||
1) When passing a `String` input, the `loose` parameter is able to affect the output. [View API](#regexparamstr-loose) | ||
Parse a route pattern into an equivalent RegExp pattern. Also collects the names of pattern's parameters as a `keys` array. An `input` that's already a RegExp is kept as is, and `regexparam` makes no additional insights. | ||
2) When passing a `RegExp` value, that must be `regexparam`'s _only_ argument.<br> | ||
Your pattern is saved as written, so `loose` is ignored entirely. [View API](#regexparamrgx) | ||
Returns a `{ keys, pattern }` object, where `pattern` is always a `RegExp` instance and `keys` is either `false` or a list of extracted parameter names. | ||
### regexparam(str, loose) | ||
Returns: `Object` | ||
> **Important:** The `keys` will _always_ be `false` when `input` is a RegExp and it will _always_ be an Array when `input` is a string. | ||
Returns a `{ keys, pattern }` object, where `pattern` is a generated `RegExp` instance and `keys` is a list of extracted parameter names. | ||
#### input | ||
Type: `string` or `RegExp` | ||
#### str | ||
Type: `String` | ||
When `input` is a string, it's treated as a route pattern and an equivalent RegExp is generated. | ||
The route/pathing string to convert. | ||
> **Note:** It does not matter if `input` strings begin with a `/` — it will be added if missing. | ||
> **Note:** It does not matter if your `str` begins with a `/` — it will be added if missing. | ||
When `input` is a RegExp, it will be used **as is** – no modifications will be made. | ||
#### loose | ||
Type: `Boolean`<br> | ||
Type: `boolean`<br> | ||
Default: `false` | ||
@@ -153,25 +190,50 @@ | ||
> **Important:** When `input` is a RegExp, the `loose` argument is ignored! | ||
```js | ||
const rgx = require('regexparam'); | ||
const { parse } = require('regexparam'); | ||
rgx('/users').pattern.test('/users/lukeed'); //=> false | ||
rgx('/users', true).pattern.test('/users/lukeed'); //=> true | ||
parse('/users').pattern.test('/users/lukeed'); //=> false | ||
parse('/users', true).pattern.test('/users/lukeed'); //=> true | ||
rgx('/users/:name').pattern.test('/users/lukeed/repos'); //=> false | ||
rgx('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true | ||
parse('/users/:name').pattern.test('/users/lukeed/repos'); //=> false | ||
parse('/users/:name', true).pattern.test('/users/lukeed/repos'); //=> true | ||
``` | ||
### regexparam(rgx) | ||
Returns: `Object` | ||
Returns a `{ keys, pattern }` object, where pattern is _identical_ to your `rgx` and `keys` is `false`, always. | ||
### regexparam.inject(pattern: string, values: object) | ||
Returns: `string` | ||
#### rgx | ||
Type: `RegExp` | ||
Returns a new string by replacing the `pattern` segments/parameters with their matching values. | ||
Your RegExp pattern. | ||
> **Important:** Named segments (eg, `/:name`) that _do not_ have a `values` match will be kept in the output. This is true _except for_ optional segments (eg, `/:name?`) and wildcard segments (eg, `/*`). | ||
> **Important:** This pattern is used _as is_! No parsing or interpreting is done on your behalf. | ||
#### pattern | ||
Type: `string` | ||
The route pattern that to receive injections. | ||
#### values | ||
Type: `Record<string, string>` | ||
The values to be injected. The keys within `values` must match the `pattern`'s segments in order to be replaced. | ||
> **Note:** To replace a wildcard segment (eg, `/*`), define a `values.wild` key. | ||
## Deno | ||
As of version `1.3.0`, you may use `regexparam` with Deno. These options are all valid: | ||
```ts | ||
// The official Deno registry: | ||
import regexparam from 'https://deno.land/x/regexparam/src/index.js'; | ||
// Third-party CDNs with ESM support: | ||
import regexparam from 'https://cdn.skypack.dev/regexparam'; | ||
import regexparam from 'https://esm.sh/regexparam'; | ||
``` | ||
> **Note:** All registries support versioned URLs, if desired. <br>The above examples always resolve to the latest published version. | ||
## Related | ||
@@ -178,0 +240,0 @@ |
13801
88
238