# @nichoth/routes

> Route matcher devised for shared rendering JavaScript applications

Latest version **4.0.7** (published 2024-03-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @nichoth/routes
pnpm add @nichoth/routes
yarn add @nichoth/routes
bun add @nichoth/routes
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.0.7 |
| Published | 2024-03-18 |
| First published | 2022-12-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 13.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | nichoth |
| Maintainers | nichoth |
| Keywords | router, routes, client-side, browser |

## Links

- npm: https://www.npmjs.com/package/@nichoth/routes
- Repository: https://github.com/nichoth/routes
- Homepage: https://github.com/nichoth/routes#readme
- Issues: https://github.com/nichoth/routes/issues
- npm.io page: https://npm.io/package/@nichoth/routes

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 4.0.7 (latest) — 2024-03-18
- 4.0.5 — 2024-03-07
- 4.0.4 — 2024-02-27
- 4.0.3 — 2024-02-14
- 4.0.1 — 2024-02-09
- 4.0.0 — 2024-02-09
- 3.0.7 — 2023-05-29
- 3.0.6 — 2023-01-27
- 3.0.5 — 2023-01-12
- 3.0.4 — 2022-12-25
- 3.0.3 — 2022-12-25
- 3.0.2 — 2022-12-25
- 3.0.1 — 2022-12-25
- 3.0.0 — 2022-12-25

## README

# routes
![tests](https://github.com/nichoth/routes/actions/workflows/nodejs.yml/badge.svg)
[![dependencies](https://img.shields.io/badge/dependencies-zero-brightgreen.svg?style=flat-square)](package.json)
[![types](https://img.shields.io/npm/types/msgpackr?style=flat-square)](README.md)
[![module](https://img.shields.io/badge/module-ESM%2FCJS-blue?style=flat-square)](README.md)
[![semantic versioning](https://img.shields.io/badge/semver-2.0.0-brightgreen?logo=semver&style=flat-square)](https://semver.org/)
[![license](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)

Route matcher devised for shared rendering JavaScript applications

## install
```sh
npm install -S @nichoth/routes
```

## ESM vs CJS
Featuring ESM or CJS versions via `package.json` `exports` field.

```js
// esm
import Router from '@nichoth/routes'
```

```js
// cjs
const Router = require('@nichoth/routes').default
```

## example
Get a router instance

```js
import Router from '@nichoth/routes'
var router = new Router()
```

### Add some routes
```js
router.addRoute('/articles', getArticles);
router.addRoute('/articles/:slug', getArticleBySlug);
router.addRoute('/articles/search/*', searchForArticles);

// can also chain the method calls
router
    .addRoute('/foo', () => {/* ... */})
    .addRoute('/bar', () => {/* ... */})
```

### Find a match

```js
const match = router.match('/articles');
// => RouteMatch
```

#### The match object
```ts
interface RouteMatch {
    params:Record<string, string>;  // <-- e.g. { slug: 'article-title' }
    splats:string[];
    route:string;
    next?:((...any)=>any)|null;
    action?:(...any)=>any;
    index?:number;
}
```

You'll get `null` back if no route matches the provided URL. Otherwise, the
route match will provide all the useful information you need inside an object.

Key               | Description
------------------|---------------------------------------------------------------------------------------
`action`          | The action passed to `addRoute` as a second argument. Using a function is recommended
`next`            | Fall through to the next route, or `null` if no other routes match
`route`           | The route passed to `addRoute` as the first argument
`params`          | An object containing the values for named parameters in the route
`splats`          | An object filled with the values for wildcard parameters

## License

MIT

## fork
This is a fork of [ruta3](https://www.npmjs.com/package/ruta3), just adding types.

---
_Source: https://npm.io/package/@nichoth/routes · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
