Skip to content

Commit

Permalink
Merge pull request #11 from cdimoulis/make_class
Browse files Browse the repository at this point in the history
Make class INTO Master
  • Loading branch information
cdimoulis committed Sep 29, 2017
2 parents 3ddff5b + e652a03 commit feb5c1f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ You can remove all routes:
router.clearAll();
```

## Extending Router

You can extend the router using the class extend syntax available in node >6.11

```js
const SimpleRouter = require('simple-routes');

class MyRouter extends SimpleRouter {
// override functions as you wish
...
};
```


[npm]: https://img.shields.io/npm/v/simple-routes.svg
[npm-url]: https://npmjs.com/package/simple-routes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simple-routes",
"version": "0.1.2",
"version": "0.2.0",
"description": "A simple route matcher",
"keywords": [
"router",
Expand Down
65 changes: 34 additions & 31 deletions src/router.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
const match = require('minimatch');
const columnify = require('columnify');

const Router = function(opts) {
// Currently no options are used.
opts = opts || {};
let options;

let _routes = [];
let _actions = [];
let _routes = [];
let _actions = [];

class Router {

constructor(opts) {
// Currently no options are used.
this.options = opts || {};
}

// Add a route to the routes
// route: (string) the route to match. Uses minimatch for route matching
// action: (function) the action (funtion callback) to perform. This function
// will be returned on a successful match.
this.addRoute = (route, action) => {
addRoute(route, action) {
if (route && (typeof route) === 'string') {
if (action && (typeof action) === 'function') {
// Detect any query strings and warn that they are ignored
Expand All @@ -35,7 +40,7 @@ const Router = function(opts) {

// Adds multiple routes and actions.
// list: array of arrays. Each array is a pair of [route, action].
this.addRoutes = (list) => {
addRoutes(list) {
if (list && Array.isArray(list)) {
list.forEach((obj) => {
this.addRoute(obj[0], obj[1]);
Expand All @@ -50,19 +55,17 @@ const Router = function(opts) {

// Show the action for the given route
// Returns the function or -1 if not found
this.getAction = (route) => {
getAction(route) {
// Separate potential query string
let rq = route.split('?');
if (rq.length > 1)
console.warn('Query strings are ignored in route matching');
// Index of path only
let index = _findMatch(rq[0]);
return _actions[index] || -1;
};

// Remove a route
// Returns the action or null if not found
this.removeRoute = (route) => {
removeRoute(route) {
// Separate potential query string
let rq = route.split('?');
// Index of path only
Expand All @@ -75,13 +78,13 @@ const Router = function(opts) {
};

// Clear all routes
this.clearAll = () => {
clearAll() {
_routes = [];
_actions = [];
}

// If has route
this.hasRoute = (route) => {
hasRoute(route) {
// Separate potential query string
let rq = route.split('?');
// Index of path only
Expand All @@ -90,7 +93,7 @@ const Router = function(opts) {
}

// Get a pretty listing of the routes
this.toString = () => {
toString() {
let str = '';
let data = [];
for (let i = 0; i < _routes.length; i++) {
Expand All @@ -111,32 +114,32 @@ const Router = function(opts) {
}

// Get all the routes
this.routes = () => {
routes() {
return _routes;
}

// Get all the actions
this.actions = () => {
actions() {
return _actions;
}
};

/***
* Private FUNCTIONS
***/

// Returns the index of a match or null
let _findMatch = (route) => {
let index;
for (let i = 0; i < _routes.length; i++) {
let possible = _routes[i];
if (match(route, possible)) {
index = i;
break;
}
/***
* Private FUNCTIONS
***/

// Returns the index of a match or null
let _findMatch = (route) => {
let index;
for (let i = 0; i < _routes.length; i++) {
let possible = _routes[i];
if (match(route, possible)) {
index = i;
break;
}
return index;
}

}
return index;
};

module.exports = Router;

0 comments on commit feb5c1f

Please sign in to comment.