# core-router

> Primitives for building your own router

Latest version **1.0.0** (published 2020-02-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install core-router
pnpm add core-router
yarn add core-router
bun add core-router
```

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2020-02-14 |
| First published | 2020-02-13 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=6.0.0 |
| Dependencies | 1 |
| Unpacked size | 116.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | David Tang |
| Maintainers | dvtng |
| Keywords | react, react-router, routing, url |

## Links

- npm: https://www.npmjs.com/package/core-router
- Repository: https://github.com/dvtng/core-router
- npm.io page: https://npm.io/package/core-router

## Dependencies (1)

- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.1.0

## 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

- 1.0.0 (latest) — 2020-02-14
- 0.1.2 — 2020-02-14
- 0.1.1 — 2020-02-14
- 0.1.0 — 2020-02-13

## README

# core-router

Primitives for building your own router.

## Matching URLs with `match()`

`match()` provides flexible rule-based URL matching.

You can:

1. Use a path-only rule:

```js
match({ path: "/account/:accountId" }, "/account/123");

result = {
  params: {
    accountId: "123";
  }
}
```

2. Combine path, search params, and a condition:

```js
match(
  {
    path: "/account/:accountId",
    search: {
      from: ":fromDate?",
      to: ":toDate?"
    },
    condition: ({ params }) => {
      return isDate(params.fromDate) && isDate(params.toDate);
    }
  },
  "/account/123?from=2020-01-01&to=2020-01-31"
);

result = {
  params: {
    accountId: "123",
    fromDate: "2020-01-01",
    toDate: "2020-01-31"
  }
};
```

3. Or fallback to completely custom logic:

```js
match(
  {
    condition: ({ url }) => {
      return url.hash === "#secret";
    }
  },
  "/you/must/have/the#secret"
);

result = {
  params: {}
};
```

Absolute URLs will only match if they belong to the same domain:

```js
// On https://foo.com:
match({ path: "/hello" }, "https://bar.com/hello");

result = null;
```

Params will be decoded for you:

```js
match({ search: { q: ":searchTerms" } }, "/?q=hello%20world");

result = {
  params: {
    searchTerms: "hello world"
  }
};
```

## Generating URLs with `toHref()`

Any rule that can be used for `match()` can also be used to generate a relative URL.

```js
toHref({ path: "/account/:accountId" }, { accountId: "123" });

result = "/account/123";
```

If required params are not supplied, an error will be thrown:

```js
toHref({ path: "/account/:accountId" }, {});
// Error!
```

Similarly, if the `condition` is not met, an error will alos be thrown:

```js
toHref(
  {
    condition: ({ url }) => url.hash === "#secret"
  },
  "/#incorrect"
);
// Error!
```

This ensures that for any rule, a URL generated by `toHref()` will also `match()`:

```js
const href = toHref(myRule, myParams);
const matchResult = match(myRule, href);
expect(matchResult).not.toBe(null);
expect(matchResult.params).toEqual(myParams);
```

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