route-node
Advanced tools
Comparing version 0.1.5 to 0.2.0
@@ -79,9 +79,9 @@ define(['exports', 'module', 'path-parser'], function (exports, module, _pathParser) { | ||
} | ||
// Params | ||
// Params last | ||
if (aHasParams && !bHasParams) return 1; | ||
if (!aHasParams && bHasParams) return -1; | ||
// Spat params | ||
// Spat params last | ||
if (!a.parser.hasSpatParam && b.parser.hasSpatParam) return -1; | ||
if (!b.parser.hasSpatParam && a.parser.hasSpatParam) return 1; | ||
// Number of segments | ||
// Sort by number of segments descending | ||
var aSegments = (a.path.match(/\//g) || []).length; | ||
@@ -138,2 +138,4 @@ var bSegments = (b.path.match(/\//g) || []).length; | ||
value: function getSegmentsMatchingPath(path) { | ||
var trailingSlash = arguments[1] === undefined ? false : arguments[1]; | ||
var matchChildren = function matchChildren(nodes, pathSegment, segments) { | ||
@@ -144,2 +146,17 @@ var _loop = function (i) { | ||
var match = child.parser.partialMatch(pathSegment); | ||
var remainingPath = undefined; | ||
if (!match && trailingSlash) { | ||
// Try with optional trailing slash | ||
match = child.parser.match(pathSegment, true); | ||
remainingPath = ''; | ||
} else if (match) { | ||
// Remove consumed segment from path | ||
var consumedPath = child.parser.build(match); | ||
remainingPath = pathSegment.replace(consumedPath, ''); | ||
if (trailingSlash && remainingPath === '/' && !/\/$/.test(consumedPath)) { | ||
remainingPath = ''; | ||
} | ||
} | ||
if (match) { | ||
@@ -150,4 +167,2 @@ segments.push(child); | ||
}); | ||
// Remove consumed segment from path | ||
var remainingPath = pathSegment.replace(child.parser.build(match), ''); | ||
// If fully matched | ||
@@ -218,3 +233,3 @@ if (!remainingPath.length) { | ||
value: function getMatchPathFromSegments(segments) { | ||
if (!segments) return null; | ||
if (!segments || !segments.length) return null; | ||
@@ -231,3 +246,5 @@ var name = segments.map(function (segment) { | ||
value: function matchPath(path) { | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path)); | ||
var trailingSlash = arguments[1] === undefined ? false : arguments[1]; | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path, trailingSlash)); | ||
} | ||
@@ -234,0 +251,0 @@ }]); |
@@ -84,9 +84,9 @@ 'use strict'; | ||
} | ||
// Params | ||
// Params last | ||
if (aHasParams && !bHasParams) return 1; | ||
if (!aHasParams && bHasParams) return -1; | ||
// Spat params | ||
// Spat params last | ||
if (!a.parser.hasSpatParam && b.parser.hasSpatParam) return -1; | ||
if (!b.parser.hasSpatParam && a.parser.hasSpatParam) return 1; | ||
// Number of segments | ||
// Sort by number of segments descending | ||
var aSegments = (a.path.match(/\//g) || []).length; | ||
@@ -143,2 +143,4 @@ var bSegments = (b.path.match(/\//g) || []).length; | ||
value: function getSegmentsMatchingPath(path) { | ||
var trailingSlash = arguments[1] === undefined ? false : arguments[1]; | ||
var matchChildren = function matchChildren(nodes, pathSegment, segments) { | ||
@@ -149,2 +151,17 @@ var _loop = function (i) { | ||
var match = child.parser.partialMatch(pathSegment); | ||
var remainingPath = undefined; | ||
if (!match && trailingSlash) { | ||
// Try with optional trailing slash | ||
match = child.parser.match(pathSegment, true); | ||
remainingPath = ''; | ||
} else if (match) { | ||
// Remove consumed segment from path | ||
var consumedPath = child.parser.build(match); | ||
remainingPath = pathSegment.replace(consumedPath, ''); | ||
if (trailingSlash && remainingPath === '/' && !/\/$/.test(consumedPath)) { | ||
remainingPath = ''; | ||
} | ||
} | ||
if (match) { | ||
@@ -155,4 +172,2 @@ segments.push(child); | ||
}); | ||
// Remove consumed segment from path | ||
var remainingPath = pathSegment.replace(child.parser.build(match), ''); | ||
// If fully matched | ||
@@ -223,3 +238,3 @@ if (!remainingPath.length) { | ||
value: function getMatchPathFromSegments(segments) { | ||
if (!segments) return null; | ||
if (!segments || !segments.length) return null; | ||
@@ -236,3 +251,5 @@ var name = segments.map(function (segment) { | ||
value: function matchPath(path) { | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path)); | ||
var trailingSlash = arguments[1] === undefined ? false : arguments[1]; | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path, trailingSlash)); | ||
} | ||
@@ -239,0 +256,0 @@ }]); |
@@ -91,9 +91,9 @@ (function (global, factory) { | ||
} | ||
// Params | ||
// Params last | ||
if (aHasParams && !bHasParams) return 1; | ||
if (!aHasParams && bHasParams) return -1; | ||
// Spat params | ||
// Spat params last | ||
if (!a.parser.hasSpatParam && b.parser.hasSpatParam) return -1; | ||
if (!b.parser.hasSpatParam && a.parser.hasSpatParam) return 1; | ||
// Number of segments | ||
// Sort by number of segments descending | ||
var aSegments = (a.path.match(/\//g) || []).length; | ||
@@ -150,2 +150,4 @@ var bSegments = (b.path.match(/\//g) || []).length; | ||
value: function getSegmentsMatchingPath(path) { | ||
var trailingSlash = arguments[1] === undefined ? false : arguments[1]; | ||
var matchChildren = function matchChildren(nodes, pathSegment, segments) { | ||
@@ -156,2 +158,17 @@ var _loop = function (i) { | ||
var match = child.parser.partialMatch(pathSegment); | ||
var remainingPath = undefined; | ||
if (!match && trailingSlash) { | ||
// Try with optional trailing slash | ||
match = child.parser.match(pathSegment, true); | ||
remainingPath = ''; | ||
} else if (match) { | ||
// Remove consumed segment from path | ||
var consumedPath = child.parser.build(match); | ||
remainingPath = pathSegment.replace(consumedPath, ''); | ||
if (trailingSlash && remainingPath === '/' && !/\/$/.test(consumedPath)) { | ||
remainingPath = ''; | ||
} | ||
} | ||
if (match) { | ||
@@ -162,4 +179,2 @@ segments.push(child); | ||
}); | ||
// Remove consumed segment from path | ||
var remainingPath = pathSegment.replace(child.parser.build(match), ''); | ||
// If fully matched | ||
@@ -230,3 +245,3 @@ if (!remainingPath.length) { | ||
value: function getMatchPathFromSegments(segments) { | ||
if (!segments) return null; | ||
if (!segments || !segments.length) return null; | ||
@@ -243,3 +258,5 @@ var name = segments.map(function (segment) { | ||
value: function matchPath(path) { | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path)); | ||
var trailingSlash = arguments[1] === undefined ? false : arguments[1]; | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path, trailingSlash)); | ||
} | ||
@@ -246,0 +263,0 @@ }]); |
@@ -106,3 +106,3 @@ import Path from 'path-parser/modules/Path' | ||
getSegmentsMatchingPath(path) { | ||
getSegmentsMatchingPath(path, trailingSlash = false) { | ||
let matchChildren = (nodes, pathSegment, segments) => { | ||
@@ -114,7 +114,20 @@ // for (child of node.children) { | ||
let match = child.parser.partialMatch(pathSegment) | ||
let remainingPath | ||
if (!match && trailingSlash) { | ||
// Try with optional trailing slash | ||
match = child.parser.match(pathSegment, true) | ||
remainingPath = '' | ||
} else if (match) { | ||
// Remove consumed segment from path | ||
let consumedPath = child.parser.build(match) | ||
remainingPath = pathSegment.replace(consumedPath, '') | ||
if (trailingSlash && remainingPath === '/' && !/\/$/.test(consumedPath)) { | ||
remainingPath = '' | ||
} | ||
} | ||
if (match) { | ||
segments.push(child) | ||
Object.keys(match).forEach(param => segments.params[param] = match[param]) | ||
// Remove consumed segment from path | ||
let remainingPath = pathSegment.replace(child.parser.build(match), '') | ||
// If fully matched | ||
@@ -159,3 +172,3 @@ if (!remainingPath.length) { | ||
getMatchPathFromSegments(segments) { | ||
if (!segments) return null | ||
if (!segments || !segments.length) return null | ||
@@ -168,5 +181,5 @@ let name = segments.map(segment => segment.name).join('.') | ||
matchPath(path) { | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path)) | ||
matchPath(path, trailingSlash = false) { | ||
return this.getMatchPathFromSegments(this.getSegmentsMatchingPath(path, trailingSlash)) | ||
} | ||
} |
{ | ||
"name": "route-node", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "A package to create a tree of named routes#", | ||
@@ -35,4 +35,4 @@ "main": "dist/commonjs/route-node.js", | ||
"dependencies": { | ||
"path-parser": "~0.1.1" | ||
"path-parser": "~0.2.1" | ||
} | ||
} |
@@ -68,2 +68,10 @@ [![npm version](https://badge.fury.io/js/route-node.svg)](http://badge.fury.io/js/route-node) | ||
Trailing slash can be optional: | ||
```javascript | ||
rootNode.matchPath('/users/view/1'); // => {name: "users.view", params: {id: "1"}} | ||
rootNode.matchPath('/users/view/1/'); // => null | ||
rootNode.matchPath('/users/view/1/', true); // => {name: "users.view", params: {id: "1"}} | ||
``` | ||
## Related packages | ||
@@ -70,0 +78,0 @@ |
@@ -168,9 +168,26 @@ 'use strict'; | ||
}); | ||
it('should match paths with optional trailing slashes', function () { | ||
var rootNode = getRoutes(); | ||
should.not.exists(rootNode.matchPath('/users/list/')); | ||
rootNode.matchPath('/users/list', true).should.eql({name: 'users.list', params: {}}); | ||
rootNode.matchPath('/users/list').should.eql({name: 'users.list', params: {}}); | ||
rootNode.matchPath('/users/list/', true).should.eql({name: 'users.list', params: {}}); | ||
should.not.exists(rootNode.matchPath('/users/list//', true)); | ||
var rootNode = getRoutes(true); | ||
should.not.exists(rootNode.matchPath('/users/list')); | ||
rootNode.matchPath('/users/list', true).should.eql({name: 'users.list', params: {}}); | ||
rootNode.matchPath('/users/list/', true).should.eql({name: 'users.list', params: {}}); | ||
rootNode.matchPath('/users/list/').should.eql({name: 'users.list', params: {}}); | ||
should.not.exists(rootNode.matchPath('/users/list//', true)); | ||
}); | ||
}); | ||
function getRoutes() { | ||
function getRoutes(trailingSlash) { | ||
var suffix = trailingSlash ? '/' : ''; | ||
var usersNode = new RouteNode('users', '/users', [ | ||
new RouteNode('list', '/list'), | ||
new RouteNode('view', '/view/:id') | ||
new RouteNode('list', '/list' + suffix), | ||
new RouteNode('view', '/view/:id' + suffix) | ||
]); | ||
@@ -177,0 +194,0 @@ |
53382
1033
83
+ Addedpath-parser@0.2.4(transitive)
- Removedpath-parser@0.1.1(transitive)
Updatedpath-parser@~0.2.1