https://github.com/funkia/rudolph
A pure and functional router using classic FRP. Written in TypeScript.
https://github.com/funkia/rudolph
Last synced: about 1 year ago
JSON representation
A pure and functional router using classic FRP. Written in TypeScript.
- Host: GitHub
- URL: https://github.com/funkia/rudolph
- Owner: funkia
- Created: 2017-03-18T09:21:19.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2021-09-20T12:05:45.000Z (over 4 years ago)
- Last Synced: 2025-03-27T18:50:57.666Z (about 1 year ago)
- Language: TypeScript
- Size: 592 KB
- Stars: 20
- Watchers: 3
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README

# Rudolph
A pure and functional router using classic FRP. Written in TypeScript.
Experimental.
[](https://travis-ci.org/funkia/rudolph)
[](https://codecov.io/gh/funkia/rudolph)
## Install
```
npm install --save @funkia/rudolph @funkia/hareactive
```
## API
### Router
```ts
type Router = {
prefixPath: string;
path: Behavior;
useHash: boolean;
};
```
### createRouter
Takes a configuration Object describing how to handle the routing:
* `useHash: boolean` - whether to use hash-routing
* `path: Behavior` - defaults to `locationHashB` or `locationB`
It errors if `useHash = true` but hash-routing is unsupported in that browser, or if there is no support for the history API.
The returned Router object is identical to its input, augmented with `prefixPath: ""`, which is used to nest routers.
Usage:
```ts
const router = createRouter({
useHash: false
});
runComponent("#mount", main({ router }));
```
### navigate
```ts
navigate(router: Router, pathStream: Stream): Now>
```
`navigate` takes a stream of paths. Whenever the stream has an occurence, it is navigated to.
Usage:
```ts
const navs: Stream = userIds
.map(prefix("/user/"))
.combine(on.homeClicks.mapTo("/"));
start(navigate(props.router, navs));
```
### routePath
`routePath(routes: Routes, router: Router): Behavior`
Takes a description of the routes and a router, and returns a behavior with the result of parsing the router's location according to the routes' pattern.
The first parameter, `routes: Routes`, is a description of the routes, in the form:
```ts
{"/route/:urlParam"; (restUrl, params) => result}
```
Usage:
```ts
E.section(
routePath(
{
"/user/:userId": (_subrouter, { userId }) => user(userId),
"/": () => home,
"*": () => notFound,
},
props.router
)
)
```
### Routes
```ts
type Routes = Record>
```
Example:
```ts
{
"/user/:userId": (_subrouter, { userId }) => user(userId),
"/": () => home,
"*": () => notFound,
}
```
### RouteHandler
```ts
type RouteHandler = (
router: Router,
params: Record
) => A;
```
### locationHashB
`locationHashB: Behavior` represents the current values of the URL hash.
### locationB
`locationHashB: Behavior` represents the current values of the URL pathname.
### navigateHashIO
`navigateHashIO: (path: string) => IO` is an `IO` effect that updates the URL hash to the supplied argument.
### navigateIO
`navigateIO: (path: string) => IO` is an `IO` effect that updates the URL pathname to the supplied argument.
### warnNavigation
Takes a behavior of a boolean, if true the user will have to confirm before unloading page.