Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.*/
node_modules/
coverage/
npm-debug.log
temp/
*.log
.DS_Store
node_modules
dist
7 changes: 0 additions & 7 deletions .npmignore

This file was deleted.

1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CHANGELOG.md
5 changes: 2 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"tabWidth": 4,
"tabWidth": 2,
"semi": false,
"singleQuote": true
}

}
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
language: node_js
cache: yarn
node_js:
- '8.0'
- '12'
script:
- npm run lint
- npm run build
- npm run test
- yarn lint
- yarn build
- yarn test
29 changes: 18 additions & 11 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
Copyright (c) 2015, Thomas Roch <>
MIT License

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
Copyright (c) 2015 Thomas Roch

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
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.
75 changes: 44 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
[![Build Status](https://travis-ci.org/troch/route-node.svg?branch=master)](https://travis-ci.org/troch/route-node)
[![Coverage Status](https://coveralls.io/repos/troch/route-node/badge.svg?branch=master)](https://coveralls.io/r/troch/route-node?branch=master)


# route-node

A package to create a tree (trie) of named routes, allowing you to build and match routes.
Expand All @@ -11,79 +10,93 @@ A package to create a tree (trie) of named routes, allowing you to build and mat
$ npm install route-node --save
```


## Creating your tree

To read about how to define paths, look at [path-parser README](https://www.npmjs.com/package/path-parser)

```javascript
import rootNode from 'route-node';
import rootNode from 'route-node'

// Create nodes
const usersNode = new RouteNode('users', '/users', [
new RouteNode('list', '/list'),
new RouteNode('view', '/view/:id')
]);
new RouteNode('list', '/list'),
new RouteNode('view', '/view/:id')
])

// You can also use plain objects
const ordersNode = new RouteNode('orders', '/orders', [
{name: 'pending', path: '/pending'},
{name: 'completed', path: '/completed'},
{name: 'view', path: '/view/:id'}
]);
{ name: 'pending', path: '/pending' },
{ name: 'completed', path: '/completed' },
{ name: 'view', path: '/view/:id' }
])

// Creating a top root node
const rootNode = new RouteNode('', '', [
ordersNode,
usersNode
]);
const rootNode = new RouteNode('', '', [ordersNode, usersNode])

// Add nodes programmatically
rootNode.add(new RouteNode('home', '/home'));
rootNode.add(new RouteNode('home', '/home'))
```

__`/` paths__
**`/` paths**

When using a deeply nested `/` path, it will automatically be matched when its parent is matched.

```js
const tree = new RouteNode('', '', [
new RouteNode('admin', '/admin', [
new RouteNode('home', '/'),
new RouteNode('users', '/users')
])
]);

tree.matchPath('/admin'); // => { name: 'admin.home', params: {} }
tree.buildPath('admin.home', {}, { trailingSlashMode: 'never' }); // => '/admin'
new RouteNode('admin', '/admin', [
new RouteNode('home', '/'),
new RouteNode('users', '/users')
])
])

tree.matchPath('/admin') // => { name: 'admin.home', params: {} }
tree.buildPath('admin.home', {}, { trailingSlashMode: 'never' }) // => '/admin'
```

__Callbacks__
**Options**

When adding routes (with contructor or `.add`), you can pass a callback which will be executed for each route added successfully to the tree.
```js
const node = new RouteNode('admin', '/admin', [], options)
```

Where options can contain:

- `onAdd`: a callback called when adding routes (with contructor or `.add`), you can pass a callback which will be executed for each route added successfully to the tree.
- `parent`: the node parent
- `finalSort`: to sort children (matching order) after having added all children routes (rather than on each add)
- `sort`: whether to sort on each add or not (default to true, overriden by `finalSort`)

## Building and matching routes

__node.buildPath(routeName: string, params?: object, options?: BuildOptions): string__
**node.buildPath(routeName: string, params?: object, options?: BuildOptions): string**

```javascript
rootNode.buildPath('users.view', {id: 1}) // => "/users/view/1"
rootNode.buildPath('users.view', { id: 1 }) // => "/users/view/1"
```

__Performance__
**Performance**

Node children need to be sorted for matching purposes. By default this operation happens after having added all routes.

__matchPath(path: string, options?: MatchOptions): RouteNodeState | null__
**matchPath(path: string, options?: MatchOptions): RouteNodeState | null**

```js
rootNode.matchPath('/users/view/1');
rootNode.matchPath('/users/view/1')
// => {name: "users.view", params: {id: "1"}}
```

## Options

Options available:

- `'urlParamsEncoding`, to specify how URL parameters are encoded and decoded:
- `'default'`: `encodeURIComponent` and `decodeURIComponent` are used but some characters to encode and decode URL parameters, but some characters are preserved when encoding (sub-delimiters:`+`,`:`,`'`,`!`,`,`,`;`,`*`).
- `'uriComponent'`: use `encodeURIComponent` and `decodeURIComponent`
for encoding and decoding URL parameters.
- `'uri'`: use `encodeURI` and `decodeURI for encoding amd decoding
URL parameters.
- `'none'`: no encoding or decoding is performed
- `'legacy'`: the approach for version 5.x and below (not recommended)
- `trailingSlashMode`:
- `'default'`: building follows path definitions
- `'never'`: when building, trailing slash is removed
Expand Down
Loading