~pepe/trolley

809b1f0ff6c34495fc1e0d308b6ccf7ef246318b — Josef Pospíšil 4 years ago df8a91f
Add documentation and license
3 files changed, 68 insertions(+), 3 deletions(-)

A LICENSE
M README.md
D router.janet
A LICENSE => LICENSE +19 -0
@@ 0,0 1,19 @@
Copyright (c) 2019 Josef Pospíšil

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

M README.md => README.md +49 -3
@@ 1,6 1,52 @@
# Trolley 

General router for Janet programming language. It is based on idea of path and
action to which it resolves.
General router for Janet programming language. The main idea is the path and
action to which it resolves with params as defined in routes.

@todo add more documentation
## Routes

We define routes as a struct with keys which contain path and params definition and
values which contain actions to which the path resolves.
Example: 

```
{"/" root
 "/home" home
 "/people/:id" people}
```

## Compiling routes 

You can compile routes' configuration from the previous part to routing table where
keys contain PEG for matching route and values contains action it resolves.
This function is mostly for internal or advanced usage of this lib.

Example:

```
(import trolley)
(troley/compile-routes 
  {"/" root
   "/home" home
   "/people/:id" people})
=> @{<core/peg 0x559E29164210> :root <core/peg 0x559E29159180> :home}
```

Id of `core/peg` differs on your machine.

## Router

This function creates routing function from routes definition, which then can be
used to match the path and returns action and table with parameters.

Example:

```
(import trolley)
(def router 
  (trolley/router {"/" root "/home" home "/people/:id" people})) 
(router "/people/3")
=> (people @{:id "3"})
```

Creation of the `router` function is the main usage pattern for this library.

D router.janet => router.janet +0 -0