route-node
Advanced tools
Comparing version 0.0.1 to 0.0.2
63
index.js
@@ -27,3 +27,3 @@ 'use strict'; | ||
this.path = path; | ||
this.parser = path ? new _pathParser2['default'](path) : {}; | ||
this.parser = path ? new _pathParser2['default'](path) : null; | ||
this.children = []; | ||
@@ -75,4 +75,4 @@ | ||
}, { | ||
key: '_findRouteByName', | ||
value: function _findRouteByName(routeName) { | ||
key: 'getSegmentsByName', | ||
value: function getSegmentsByName(routeName) { | ||
var findSegmentByName = function findSegmentByName(name, routes) { | ||
@@ -98,19 +98,16 @@ var filteredRoutes = routes.filter(function (r) { | ||
return matched ? segments : []; | ||
return matched ? segments : false; | ||
} | ||
}, { | ||
key: 'matchPath', | ||
value: function matchPath(path) { | ||
var segments = []; | ||
var matchChildren = function matchChildren(node, pathSegment, matched) { | ||
key: 'getSegmentsMatchingPath', | ||
value: function getSegmentsMatchingPath(path) { | ||
var matchChildren = function matchChildren(nodes, pathSegment, segments) { | ||
var _loop = function (i) { | ||
var child = node.children[i]; | ||
var child = nodes[i]; | ||
// Partially match path | ||
var match = child.parser.partialMatch(pathSegment); | ||
if (match) { | ||
// Append name and extend params | ||
matched.name += (matched.name ? '.' : '') + child.name; | ||
Object.keys(match).forEach(function (p) { | ||
return matched.params[p] = match[p]; | ||
segments.push(child); | ||
Object.keys(match).forEach(function (param) { | ||
return segments.params[param] = match[param]; | ||
}); | ||
@@ -122,3 +119,3 @@ // Remove consumed segment from path | ||
return { | ||
v: matched | ||
v: segments | ||
}; | ||
@@ -134,3 +131,3 @@ } | ||
return { | ||
v: matchChildren(child, remainingPath, matched) | ||
v: matchChildren(child.children, remainingPath, segments) | ||
}; | ||
@@ -141,3 +138,3 @@ } | ||
// for (child of node.children) { | ||
for (var i in node.children) { | ||
for (var i in nodes) { | ||
var _ret = _loop(i); | ||
@@ -150,3 +147,7 @@ | ||
return matchChildren(this, path, { name: '', params: {} }); | ||
var startingNodes = this.parser ? [this] : this.children; | ||
var segments = []; | ||
segments.params = {}; | ||
return matchChildren(startingNodes, path, segments); | ||
} | ||
@@ -156,7 +157,7 @@ }, { | ||
value: function getPath(routeName) { | ||
var segments = this._findRouteByName(routeName); | ||
var segments = this.getSegmentsByName(routeName); | ||
return segments.map(function (segment) { | ||
return segments ? segments.map(function (segment) { | ||
return segment.path; | ||
}).join(''); | ||
}).join('') : false; | ||
} | ||
@@ -168,8 +169,22 @@ }, { | ||
var segments = this._findRouteByName(routeName); | ||
var segments = this.getSegmentsByName(routeName); | ||
return segments.map(function (segment) { | ||
return segments ? segments.map(function (segment) { | ||
return segment.parser.build(params); | ||
}).join(''); | ||
}).join('') : false; | ||
} | ||
}, { | ||
key: 'matchPath', | ||
value: function matchPath(path) { | ||
var segments = this.getSegmentsMatchingPath(path); | ||
if (!segments) return false; | ||
var name = segments.map(function (segment) { | ||
return segment.name; | ||
}).join('.'); | ||
var params = segments.params; | ||
return { name: name, params: params }; | ||
} | ||
}]); | ||
@@ -176,0 +191,0 @@ |
@@ -7,3 +7,3 @@ import Path from 'path-parser' | ||
this.path = path | ||
this.parser = path ? new Path(path) : {} | ||
this.parser = path ? new Path(path) : null | ||
this.children = [] | ||
@@ -43,3 +43,3 @@ | ||
_findRouteByName(routeName) { | ||
getSegmentsByName(routeName) { | ||
let findSegmentByName = (name, routes) => { | ||
@@ -63,18 +63,15 @@ let filteredRoutes = routes.filter(r => r.name === name) | ||
return matched ? segments : [] | ||
return matched ? segments : false | ||
} | ||
matchPath(path) { | ||
let segments = [] | ||
let matchChildren = (node, pathSegment, matched) => { | ||
getSegmentsMatchingPath(path) { | ||
let matchChildren = (nodes, pathSegment, segments) => { | ||
// for (child of node.children) { | ||
for (let i in node.children) { | ||
let child = node.children[i] | ||
for (let i in nodes) { | ||
let child = nodes[i] | ||
// Partially match path | ||
let match = child.parser.partialMatch(pathSegment) | ||
if (match) { | ||
// Append name and extend params | ||
matched.name += (matched.name ? '.' : '') + child.name | ||
Object.keys(match).forEach(p => matched.params[p] = match[p]) | ||
segments.push(child) | ||
Object.keys(match).forEach(param => segments.params[param] = match[param]) | ||
// Remove consumed segment from path | ||
@@ -84,3 +81,3 @@ let remainingPath = pathSegment.replace(child.parser.build(match), '') | ||
if (!remainingPath.length && !child.children.length) { | ||
return matched | ||
return segments | ||
} | ||
@@ -92,3 +89,3 @@ // If no children to match against but unmatched path left | ||
// Else: remaining path and children | ||
return matchChildren(child, remainingPath, matched); | ||
return matchChildren(child.children, remainingPath, segments); | ||
} | ||
@@ -99,16 +96,31 @@ } | ||
return matchChildren(this, path, {name: '', params: {}}) | ||
let startingNodes = this.parser ? [this] : this.children | ||
let segments = [] | ||
segments.params = {} | ||
return matchChildren(startingNodes, path, segments) | ||
} | ||
getPath(routeName) { | ||
let segments = this._findRouteByName(routeName) | ||
let segments = this.getSegmentsByName(routeName) | ||
return segments.map(segment => segment.path).join('') | ||
return segments ? segments.map(segment => segment.path).join('') : false | ||
} | ||
buildPath(routeName, params = {}) { | ||
let segments = this._findRouteByName(routeName) | ||
let segments = this.getSegmentsByName(routeName) | ||
return segments.map(segment => segment.parser.build(params)).join('') | ||
return segments ? segments.map(segment => segment.parser.build(params)).join('') : false | ||
} | ||
matchPath(path) { | ||
let segments = this.getSegmentsMatchingPath(path) | ||
if (!segments) return false | ||
let name = segments.map(segment => segment.name).join('.') | ||
let params = segments.params | ||
return {name, params} | ||
} | ||
} |
{ | ||
"name": "route-node", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "A package to create a tree of named routes#", | ||
@@ -8,2 +8,3 @@ "main": "index.js", | ||
"test": "./node_modules/.bin/_mocha", | ||
"test-cover": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha", | ||
"build": "./node_modules/.bin/babel -m common lib/RouteNode.js > index.js" | ||
@@ -29,7 +30,9 @@ }, | ||
"should": "^6.0.3", | ||
"mocha": "^2.2.5" | ||
"mocha": "^2.2.5", | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.16" | ||
}, | ||
"dependencies": { | ||
"path-parser": "~0.0.3" | ||
"path-parser": "~0.0.4" | ||
} | ||
} |
@@ -5,4 +5,6 @@ [![Build Status](https://travis-ci.org/troch/route-node.svg?branch=master)](https://travis-ci.org/troch/route-node) | ||
A package to create a tree (trie) of named routes. It is similar to [routington](https://www.npmjs.com/package/routington) except that nodes are not added by splitting path by segment ("/"). Instead the trie is built with the supplied nodes, meaning each node is a valid route. | ||
A package to create a tree (trie) of named routes. It is similar to [routington](https://www.npmjs.com/package/routington) except that nodes are not added by splitting path by segment ("/"). Instead the tree is built with the supplied nodes, meaning each node is a valid route. | ||
**This module is being used for developing a router, API is subject to change without notice** | ||
## Install | ||
@@ -14,2 +16,4 @@ | ||
Building your route tree: | ||
```javascript | ||
@@ -38,3 +42,8 @@ var RootNode = require('route-node'); | ||
rootNode.add(new RouteNode('home', '/home')); | ||
``` | ||
And then build paths, or match your paths against your tree: | ||
```javascript | ||
rootNode.getPath('users.view'); // => "/users/view/:id" | ||
@@ -41,0 +50,0 @@ rootNode.buildPath('users.view', {id: 1}); // => "/users/view/1" |
@@ -15,2 +15,10 @@ 'use strict'; | ||
it('should throw an error when RouteNode is not used as a constructor', function () { | ||
(function () { | ||
RouteNode('', '', [ | ||
{name: 'home'} | ||
]); | ||
}).should.throw(); | ||
}); | ||
it('should instanciate a RouteNode object from plain objects', function () { | ||
@@ -39,2 +47,10 @@ var node = new RouteNode('', '', [ | ||
it('should throw an error when trying to add a node which is not an instance of RouteNode or Object', function () { | ||
var rootNode = new RouteNode('', ''); | ||
(function () { | ||
rootNode.add('users'); | ||
}).should.throw(); | ||
}); | ||
it('should throw an error when trying to add a route to a node with an already existing alias or path', function () { | ||
@@ -72,2 +88,8 @@ var root = new RouteNode('', '', [ | ||
it('should find a nested route by name', function () { | ||
var node = getRoutes(); | ||
node.getPath('users.manage').should.be.false; | ||
}); | ||
it('should build the path of a nested route', function () { | ||
@@ -101,2 +123,12 @@ var node = getRoutes(); | ||
}); | ||
it('should work on a tree without a root node', function () { | ||
var usersNode = new RouteNode('users', '/users', [ | ||
new RouteNode('list', '/list'), | ||
new RouteNode('view', '/view/:id') | ||
]); | ||
usersNode.matchPath('/users/view/1').should.eql({name: 'users.view', params: {id: '1'}}); | ||
usersNode.matchPath('/users/list').should.eql({name: 'users.list', params: {}}); | ||
}) | ||
}); | ||
@@ -103,0 +135,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20046
386
59
5
Updatedpath-parser@~0.0.4