route-recognizer
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -103,3 +103,3 @@ import map from './route-recognizer/dsl'; | ||
// segment, indicating whether it should be decoded during recognition. | ||
function parse(route, names, specificity, shouldDecodes) { | ||
function parse(route, names, types, shouldDecodes) { | ||
// normalize route as not starting with a "/". Recognition will | ||
@@ -112,20 +112,2 @@ // also normalize. | ||
// A routes has specificity determined by the order that its different segments | ||
// appear in. This system mirrors how the magnitude of numbers written as strings | ||
// works. | ||
// Consider a number written as: "abc". An example would be "200". Any other number written | ||
// "xyz" will be smaller than "abc" so long as `a > x`. For instance, "199" is smaller | ||
// then "200", even though "y" and "z" (which are both 9) are larger than "0" (the value | ||
// of (`b` and `c`). This is because the leading symbol, "2", is larger than the other | ||
// leading symbol, "1". | ||
// The rule is that symbols to the left carry more weight than symbols to the right | ||
// when a number is written out as a string. In the above strings, the leading digit | ||
// represents how many 100's are in the number, and it carries more weight than the middle | ||
// number which represents how many 10's are in the number. | ||
// This system of number magnitude works well for route specificity, too. A route written as | ||
// `a/b/c` will be more specific than `x/y/z` as long as `a` is more specific than | ||
// `x`, irrespective of the other parts. | ||
// Because of this similarity, we assign each type of segment a number value written as a | ||
// string. We can find the specificity of compound routes by concatenating these strings | ||
// together, from left to right. | ||
for (var i=0; i<segments.length; i++) { | ||
@@ -138,3 +120,3 @@ var segment = segments[i], match; | ||
shouldDecodes.push(true); | ||
specificity.val += '3'; | ||
types.dynamics++; | ||
} else if (match = segment.match(/^\*([^\/]+)$/)) { | ||
@@ -144,9 +126,8 @@ results[i] = new StarSegment(match[1]); | ||
shouldDecodes.push(false); | ||
specificity.val += '1'; | ||
types.stars++; | ||
} else if(segment === "") { | ||
results[i] = new EpsilonSegment(); | ||
specificity.val += '2'; | ||
} else { | ||
results[i] = new StaticSegment(segment); | ||
specificity.val += '4'; | ||
types.statics++; | ||
} | ||
@@ -248,6 +229,25 @@ } | ||
// Sort the routes by specificity | ||
// This is a somewhat naive strategy, but should work in a lot of cases | ||
// A better strategy would properly resolve /posts/:id/new and /posts/edit/:id. | ||
// | ||
// This strategy generally prefers more static and less dynamic matching. | ||
// Specifically, it | ||
// | ||
// * prefers fewer stars to more, then | ||
// * prefers using stars for less of the match to more, then | ||
// * prefers fewer dynamic segments to more, then | ||
// * prefers more static segments to more | ||
function sortSolutions(states) { | ||
return states.sort(function(a, b) { | ||
return (b.specificity.val < a.specificity.val) ? -1 : (b.specificity.val === a.specificity.val) ? 0 : 1; | ||
if (a.types.stars !== b.types.stars) { return a.types.stars - b.types.stars; } | ||
if (a.types.stars) { | ||
if (a.types.statics !== b.types.statics) { return b.types.statics - a.types.statics; } | ||
if (a.types.dynamics !== b.types.dynamics) { return b.types.dynamics - a.types.dynamics; } | ||
} | ||
if (a.types.dynamics !== b.types.dynamics) { return a.types.dynamics - b.types.dynamics; } | ||
if (a.types.statics !== b.types.statics) { return b.types.statics - a.types.statics; } | ||
return 0; | ||
}); | ||
@@ -340,3 +340,3 @@ } | ||
var currentState = this.rootState, regex = "^", | ||
specificity = { val: '' }, | ||
types = { statics: 0, dynamics: 0, stars: 0 }, | ||
handlers = new Array(routes.length), allSegments = [], name; | ||
@@ -349,3 +349,3 @@ | ||
var segments = parse(route.path, names, specificity, shouldDecodes); | ||
var segments = parse(route.path, names, types, shouldDecodes); | ||
@@ -380,3 +380,3 @@ allSegments = allSegments.concat(segments); | ||
currentState.regex = new RegExp(regex + "$"); | ||
currentState.specificity = specificity; | ||
currentState.types = types; | ||
@@ -557,3 +557,3 @@ if (name = options && options.as) { | ||
RouteRecognizer.VERSION = '0.2.0'; | ||
RouteRecognizer.VERSION = '0.2.2'; | ||
@@ -560,0 +560,0 @@ // Set to false to opt-out of encoding and decoding path segments. |
@@ -284,3 +284,3 @@ (function() { | ||
// segment, indicating whether it should be decoded during recognition. | ||
function $$route$recognizer$$parse(route, names, specificity, shouldDecodes) { | ||
function $$route$recognizer$$parse(route, names, types, shouldDecodes) { | ||
// normalize route as not starting with a "/". Recognition will | ||
@@ -293,20 +293,2 @@ // also normalize. | ||
// A routes has specificity determined by the order that its different segments | ||
// appear in. This system mirrors how the magnitude of numbers written as strings | ||
// works. | ||
// Consider a number written as: "abc". An example would be "200". Any other number written | ||
// "xyz" will be smaller than "abc" so long as `a > x`. For instance, "199" is smaller | ||
// then "200", even though "y" and "z" (which are both 9) are larger than "0" (the value | ||
// of (`b` and `c`). This is because the leading symbol, "2", is larger than the other | ||
// leading symbol, "1". | ||
// The rule is that symbols to the left carry more weight than symbols to the right | ||
// when a number is written out as a string. In the above strings, the leading digit | ||
// represents how many 100's are in the number, and it carries more weight than the middle | ||
// number which represents how many 10's are in the number. | ||
// This system of number magnitude works well for route specificity, too. A route written as | ||
// `a/b/c` will be more specific than `x/y/z` as long as `a` is more specific than | ||
// `x`, irrespective of the other parts. | ||
// Because of this similarity, we assign each type of segment a number value written as a | ||
// string. We can find the specificity of compound routes by concatenating these strings | ||
// together, from left to right. | ||
for (var i=0; i<segments.length; i++) { | ||
@@ -319,3 +301,3 @@ var segment = segments[i], match; | ||
shouldDecodes.push(true); | ||
specificity.val += '3'; | ||
types.dynamics++; | ||
} else if (match = segment.match(/^\*([^\/]+)$/)) { | ||
@@ -325,9 +307,8 @@ results[i] = new $$route$recognizer$$StarSegment(match[1]); | ||
shouldDecodes.push(false); | ||
specificity.val += '1'; | ||
types.stars++; | ||
} else if(segment === "") { | ||
results[i] = new $$route$recognizer$$EpsilonSegment(); | ||
specificity.val += '2'; | ||
} else { | ||
results[i] = new $$route$recognizer$$StaticSegment(segment); | ||
specificity.val += '4'; | ||
types.statics++; | ||
} | ||
@@ -429,6 +410,25 @@ } | ||
// Sort the routes by specificity | ||
// This is a somewhat naive strategy, but should work in a lot of cases | ||
// A better strategy would properly resolve /posts/:id/new and /posts/edit/:id. | ||
// | ||
// This strategy generally prefers more static and less dynamic matching. | ||
// Specifically, it | ||
// | ||
// * prefers fewer stars to more, then | ||
// * prefers using stars for less of the match to more, then | ||
// * prefers fewer dynamic segments to more, then | ||
// * prefers more static segments to more | ||
function $$route$recognizer$$sortSolutions(states) { | ||
return states.sort(function(a, b) { | ||
return (b.specificity.val < a.specificity.val) ? -1 : (b.specificity.val === a.specificity.val) ? 0 : 1; | ||
if (a.types.stars !== b.types.stars) { return a.types.stars - b.types.stars; } | ||
if (a.types.stars) { | ||
if (a.types.statics !== b.types.statics) { return b.types.statics - a.types.statics; } | ||
if (a.types.dynamics !== b.types.dynamics) { return b.types.dynamics - a.types.dynamics; } | ||
} | ||
if (a.types.dynamics !== b.types.dynamics) { return a.types.dynamics - b.types.dynamics; } | ||
if (a.types.statics !== b.types.statics) { return b.types.statics - a.types.statics; } | ||
return 0; | ||
}); | ||
@@ -521,3 +521,3 @@ } | ||
var currentState = this.rootState, regex = "^", | ||
specificity = { val: '' }, | ||
types = { statics: 0, dynamics: 0, stars: 0 }, | ||
handlers = new Array(routes.length), allSegments = [], name; | ||
@@ -530,3 +530,3 @@ | ||
var segments = $$route$recognizer$$parse(route.path, names, specificity, shouldDecodes); | ||
var segments = $$route$recognizer$$parse(route.path, names, types, shouldDecodes); | ||
@@ -561,3 +561,3 @@ allSegments = allSegments.concat(segments); | ||
currentState.regex = new RegExp(regex + "$"); | ||
currentState.specificity = specificity; | ||
currentState.types = types; | ||
@@ -738,3 +738,3 @@ if (name = options && options.as) { | ||
$$route$recognizer$$RouteRecognizer.VERSION = '0.2.0'; | ||
$$route$recognizer$$RouteRecognizer.VERSION = '0.2.2'; | ||
@@ -741,0 +741,0 @@ // Set to false to opt-out of encoding and decoding path segments. |
@@ -103,3 +103,3 @@ import map from './route-recognizer/dsl'; | ||
// segment, indicating whether it should be decoded during recognition. | ||
function parse(route, names, specificity, shouldDecodes) { | ||
function parse(route, names, types, shouldDecodes) { | ||
// normalize route as not starting with a "/". Recognition will | ||
@@ -112,20 +112,2 @@ // also normalize. | ||
// A routes has specificity determined by the order that its different segments | ||
// appear in. This system mirrors how the magnitude of numbers written as strings | ||
// works. | ||
// Consider a number written as: "abc". An example would be "200". Any other number written | ||
// "xyz" will be smaller than "abc" so long as `a > x`. For instance, "199" is smaller | ||
// then "200", even though "y" and "z" (which are both 9) are larger than "0" (the value | ||
// of (`b` and `c`). This is because the leading symbol, "2", is larger than the other | ||
// leading symbol, "1". | ||
// The rule is that symbols to the left carry more weight than symbols to the right | ||
// when a number is written out as a string. In the above strings, the leading digit | ||
// represents how many 100's are in the number, and it carries more weight than the middle | ||
// number which represents how many 10's are in the number. | ||
// This system of number magnitude works well for route specificity, too. A route written as | ||
// `a/b/c` will be more specific than `x/y/z` as long as `a` is more specific than | ||
// `x`, irrespective of the other parts. | ||
// Because of this similarity, we assign each type of segment a number value written as a | ||
// string. We can find the specificity of compound routes by concatenating these strings | ||
// together, from left to right. | ||
for (var i=0; i<segments.length; i++) { | ||
@@ -138,3 +120,3 @@ var segment = segments[i], match; | ||
shouldDecodes.push(true); | ||
specificity.val += '3'; | ||
types.dynamics++; | ||
} else if (match = segment.match(/^\*([^\/]+)$/)) { | ||
@@ -144,9 +126,8 @@ results[i] = new StarSegment(match[1]); | ||
shouldDecodes.push(false); | ||
specificity.val += '1'; | ||
types.stars++; | ||
} else if(segment === "") { | ||
results[i] = new EpsilonSegment(); | ||
specificity.val += '2'; | ||
} else { | ||
results[i] = new StaticSegment(segment); | ||
specificity.val += '4'; | ||
types.statics++; | ||
} | ||
@@ -248,6 +229,25 @@ } | ||
// Sort the routes by specificity | ||
// This is a somewhat naive strategy, but should work in a lot of cases | ||
// A better strategy would properly resolve /posts/:id/new and /posts/edit/:id. | ||
// | ||
// This strategy generally prefers more static and less dynamic matching. | ||
// Specifically, it | ||
// | ||
// * prefers fewer stars to more, then | ||
// * prefers using stars for less of the match to more, then | ||
// * prefers fewer dynamic segments to more, then | ||
// * prefers more static segments to more | ||
function sortSolutions(states) { | ||
return states.sort(function(a, b) { | ||
return (b.specificity.val < a.specificity.val) ? -1 : (b.specificity.val === a.specificity.val) ? 0 : 1; | ||
if (a.types.stars !== b.types.stars) { return a.types.stars - b.types.stars; } | ||
if (a.types.stars) { | ||
if (a.types.statics !== b.types.statics) { return b.types.statics - a.types.statics; } | ||
if (a.types.dynamics !== b.types.dynamics) { return b.types.dynamics - a.types.dynamics; } | ||
} | ||
if (a.types.dynamics !== b.types.dynamics) { return a.types.dynamics - b.types.dynamics; } | ||
if (a.types.statics !== b.types.statics) { return b.types.statics - a.types.statics; } | ||
return 0; | ||
}); | ||
@@ -340,3 +340,3 @@ } | ||
var currentState = this.rootState, regex = "^", | ||
specificity = { val: '' }, | ||
types = { statics: 0, dynamics: 0, stars: 0 }, | ||
handlers = new Array(routes.length), allSegments = [], name; | ||
@@ -349,3 +349,3 @@ | ||
var segments = parse(route.path, names, specificity, shouldDecodes); | ||
var segments = parse(route.path, names, types, shouldDecodes); | ||
@@ -380,3 +380,3 @@ allSegments = allSegments.concat(segments); | ||
currentState.regex = new RegExp(regex + "$"); | ||
currentState.specificity = specificity; | ||
currentState.types = types; | ||
@@ -383,0 +383,0 @@ if (name = options && options.as) { |
{ | ||
"name": "route-recognizer", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "A lightweight JavaScript library that matches paths against registered routes.", | ||
@@ -5,0 +5,0 @@ "author": "Yehuda Katz", |
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
163908
2032