0.0.4 • Published 7 years ago
sort-route-addresses-nodeps v0.0.4
An algorithm for ordering route addresses
Definition of a "route address"
For the purposes of this algorithm, a route address is a string containing:
- An optional HTTP verb (or
ALL) prefix, followed by a space, followed by: - 1 or more path components, where each path component is:
- A forward slash
/, followed by: - A colon
:followed by any combination of letters and numbers (a "param"), OR - A star
*(a "wildcard"), OR - Any combination of letters and numbers (a "static path component")
- A forward slash
In other words, any Sails-compatible route path.
Algorithm
Given an unordered list of route addresses:
- Find the maximum number of parts (
maxParts) in any address, by ignoring the optional verb prefix and initial forward slash and splitting each address on the remaining/characters. For each address, construct a string (
rank) by examining each part in the address from left to right and:- Adding a
1to the string for each static path component - Adding a
2to the string for each param component - Adding a
3to the string for each wildcard component - If the address being examined has fewer than
maxPartsparts, pad the string with0s if no wildcards were encountered, or4s if wildcards were encountered. - Add a
0to the end of the string if the address has a specific verb prefix (i.e. notALL), and a1if it has aALLprefix or no prefix. - Finally, prefix a
5at the beginning of the string if the path has no static components1.
- Adding a
Sort the addresses by their
rank, with lesser ranks coming before greater ones.
Example
RANK ADDRESS
---- -----
1000 GET /foo
1001 /foo
1101 /foo/bar
1111 /foo/bar/baz
1120 GET /foo/bar/:baz
1121 /foo/bar/:baz
1130 GET /foo/bar/*
1131 /foo/bar/*
1201 /foo/:bar
1211 /foo/:bar/baz
1221 /foo/:bar/:baz
1311 /foo/*/baz
1341 /foo/*
2101 /:foo/bar
2111 /:foo/bar/baz
2121 /:foo/bar/:baz
2211 /:foo/:bar/baz
3111 /*/bar/baz
3131 /*/baz/*
3141 /*/baz
52001 /:foo
52201 /:foo/:bar
52221 /:foo/:bar/:baz
52231 /:foo/:bar/*
52341 /:foo/*
53440 GET /*
53441 /*1: This ensures that addresses like /*/baz/* are ranked above ones like /:foo/:bar/:baz -- the static baz component implies that the author intends for */baz/* to handle request URLs of at least three components, one of which is baz; if /:foo/:bar/:baz/ were ranked higher in the list, it would swallow the subset of those URLs with exactly three components.