# mapleTree

> Simple routing library for node.js

Latest version **0.5.1** (published 2013-10-31) · MIT license · 0 weekly downloads

## Install

```sh
npm install mapleTree
pnpm add mapleTree
yarn add mapleTree
bun add mapleTree
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.5.1 |
| Published | 2013-10-31 |
| First published | 2012-05-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.6.11 |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Saam Barati |
| Maintainers | saamyjoon |
| Keywords | router, routing, route |

## Links

- npm: https://www.npmjs.com/package/mapleTree
- Repository: https://github.com/saambarati/mapleTree
- Issues: https://github.com/saambarati/mapleTree/issues
- npm.io page: https://npm.io/package/mapleTree

## Recent versions

- 0.5.1 (latest) — 2013-10-31
- 0.5.0-1 — 2012-12-04
- 0.5.0 — 2012-12-02
- 0.4.1 — 2012-10-07
- 0.4.0 — 2012-09-15
- 0.3.3 — 2012-08-29
- 0.3.2 — 2012-08-29
- 0.3.0 — 2012-08-28
- 0.2.2 — 2012-07-31
- 0.2.1 — 2012-07-01
- 0.2.0 — 2012-06-29
- 0.1.12 — 2012-06-21
- 0.1.11 — 2012-06-14
- 0.1.10 — 2012-06-13
- 0.1.9 — 2012-06-13
- … 3 more at https://npm.io/package/mapleTree/versions

## README

mapleTree
=========

mapleTree is a small, recursive router for Node.js. It works by creating a routing tree and searching for full and partial matches.
mapleTree is designed to be minimal. It is written with the intention that other libraries will be built on top of it or extend its functionality.

####Install
    npm install mapleTree
#### From Source
    git clone git://github.com/saambarati/mapleTree.git
    cd mapleTree
    npm link

API
---

### Simple Routing
     var mapleTree = require('mapleTree')
       , router = new mapleTree.RouteTree()

#### Normal route

     router.define('/foo/bar', function () {
       console.log('foo/bar route')
     })

#### Colon Agruments

     router.define('/hello/:foo', function () {
       console.log('hello/:foo')
     })
     m = router.match('/hello/world')
     //m.perfect === true
     //m.params.foo === 'world'
     //router.match('/hello/world/foo').perfect === false

     router.define('/files/:file.:format', function () { //note, the period is interpreted literally
       console.log('file callback')
       console.log('filename =>' + this.params.file + '.'+ this.params.format)
     })
     m = router.match('/files/home.html')
     //m.perfect === true
     //m.params.file === 'home'
     //m.params.format === 'html'

#### router.match

     /*
      *  the matcher object  contains a few important properties. It is what is returned from a router.match() call
      *  matcher.cbs = {Array}                           //collection of callbacks, the best match at zero index
      *  matcher.fn = {function}                         //placeholder for best matching function. The best depends on 'fifo' being true or false. (see below)
      *  matcher.perfect = {boolean} default => false    //true if matched exact path. false if it only matched partially
      *  matcher.extras = {Array}                        //match a regexp capture group that isn't part of params. i.e when using wildcard
      *  matcher.params = {Object}                       //collection of colon args
      *  matcher.next {function}                         //invoke next matching function if one exists
     */

     var match = router.match('/foo/bar')
     match.fn()                            //prints 'foo/bar route'

     match = router.match('/hello/world')
     match.fn()                            //prints 'hello/:foo'
     console.log(match.params.foo)         //prints 'world'

     match = router.match('/files/index.html')
     match.fn()  //prints 'filename => index.html'

#### wildcard routes

    router.define('/files/*')

    router.match('/files')           //matcher.perfect === false
    router.match('/files/home.html') //matcher.perfect === true

### Partial Matches -- *first in first out / first in last out*

     router = new mapleTree.RouteTree({'fifo' : false })

     router.define('/hello', function () {
       console.log('/hello')
       this.next()
     })
     router.define('/hello/world', function () {
       console.log('/hello/world')
       this.next()
     })
     router.define('/hello/world/foo', function () {
       console.log('/hello/world/foo')
       this.next()
     })

     var match = router.match('/hello/world/foo')
     match.fn()
     /* PRINTS =>
      *  /hello/world/foo
      *  /hello/world
      *  /hello
     */

     router.fifo = true  //first match is invoked first now
     //or when creating the router you can pass an options obj  => new maple.RouteTree({'fifo' : true})
     match = router.match('/hello/world/foo')
     match.fn()
     /* PRINTS =>
      *  /hello
      *  /hello/world
      *  /hello/world/foo
     */




### URL Pattern Matching (or other patterns)
The  `pattern` API works similarly to the `define` API. You pass `mapleTree.pattern` a patterned URL to match against, and it returns a function that when passed a string as a parameter
will return a `boolean` indicating whether it matches the parameter or not.

    var mapleTree = require('mapleTree')
    var match = mapleTree.pattern('/test/:var') //returns a function
    console.log(match('/test/yes')) // true
    console.log(match('/test'))     // false
    console.log(match('/test/'))    // false

    var match = mapleTree.pattern('wildcard/*')
    console.log(match('/wildcard'))                      // false
    console.log(match('/wildcard/some/extended/route/')) // true


### Tidbits

Routing using `match.next` will not match against the root (`/`) route. I figure if you need a function to run against
the root, it is better served being run outside of the router, considering you want it to run every time.
I could be wrong about this stance though, and am interested in listening to arguments defending the contrary.
Maybe if enough people want the functionality I can add it as an option when instantiating the router similar to `fifo`.

#### Trailing slashes at the end of routes *are* significant.

    tree.define('/hello')

is not the same as:

    tree.define('/hello/')

if you want to match both `'/hello'` and `'/hello/'`
define your route this way:

    tree.define('/hello/?')

because it makes the trailing slash optional.

###### Licensed under The MIT License

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