UNPKG

routable

Version:

Route matching and testing, nothing more than that. As simple as it could be.

253 lines 10.8 kB
[ { "tags": [ { "type": "constructor", "string": "" }, { "type": "param", "types": [ "String", "RegExp" ], "name": "url", "description": "URL to match against." }, { "type": "api", "visibility": "private" } ], "description": { "full": "<p>Route is a simple representation of a HTTP route. It can be used to simply<br />check if a given URL matches a certain route.</p>", "summary": "<p>Route is a simple representation of a HTTP route. It can be used to simply<br />check if a given URL matches a certain route.</p>", "body": "" }, "isPrivate": true, "ignore": false, "code": "var Route = module.exports = function Route(url) {\n if (!(this instanceof Route)) return new Route(url);\n if (!url) throw new Error('Missing url argument');\n\n this.flags = ''; // RegExp flags.\n this._url = ''; // Backup of the real URL.\n this.params = []; // Param names from the URL\n this.parsers = {}; // Param parsers.\n this.pattern = ''; // RegExp body.\n this._compiled = 0; // Compiled version of xRegExp.\n\n // Set the URL of the route, it will be automatically parsed.\n this.url = url;\n};\n\nObject.defineProperty(Route.prototype, 'url', {\n enumerable: false", "ctx": { "type": "declaration", "name": "Route", "value": "module.exports = function Route(url) {", "string": "Route" } }, { "tags": [ { "type": "returns", "string": "{String}" }, { "type": "api", "visibility": "private" } ], "description": { "full": "<p>Returns the compiled version of a URL.</p>", "summary": "<p>Returns the compiled version of a URL.</p>", "body": "" }, "isPrivate": true, "ignore": false, "code": ", get: function get() {\n return this._url.toString();\n }" }, { "tags": [ { "type": "param", "types": [ "Mixed" ], "name": "uri", "description": "The URI that needs to be parsed." }, { "type": "api", "visibility": "private" } ], "description": { "full": "<p>Parse the URL.</p>", "summary": "<p>Parse the URL.</p>", "body": "" }, "isPrivate": true, "ignore": false, "code": ", set: function set(uri) {\n var xregexpre = /\\/\\^(.*)+\\/([sxngimy]+)?$/g\n , self = this\n , re;\n\n //\n // We're already a regular expression, no need to parse it further.\n //\n if (uri instanceof RegExp) {\n this._url = uri;\n this.pattern = uri.source;\n this.flags = '';\n\n if (uri.global) this.flags += 'g';\n if (uri.ignoreCase) this.flags += 'i';\n if (uri.multiline) this.flags += 'm';\n\n return this.compile();\n }\n\n //\n // Only strings and regular expressions are allowed.\n //\n if (typeof (uri) !== 'string') throw new TypeError('url must be a String');\n\n //\n // When we've received a string that starts with a `/^ .. /flags`, assume\n // that we've been given a valid xRegExp string.\n //\n if (re = xregexpre.exec(uri)) {\n this._url = uri;\n this.pattern = re[1];\n this.flags = re[2];\n\n return this.compile();\n }\n\n this._url = url.parse(uri).path;\n this.pattern = '^';\n this.flags = 'x';\n this.params = [];\n\n this._url.split('/').forEach(function forEach(fragment) {\n\n if (!fragment.length) return;\n\n var named = fragment.charAt(0) === ':'\n , optional = fragment.charAt(fragment.length - 1) === '?';\n\n self.pattern += optional ? '\\\\" }, { "tags": [ { "type": "api", "visibility": "private" } ], "description": { "full": "<p>: &#39;\\/+&#39;;</p><pre><code> if (named) {\n //\n // Previously was gratuitous, but better to just be standard\n // self.pattern += &#39;([a-zA-Z0-9-_~%!;@=+\\\\$\\\\*\\\\.]+)&#39;;\n //\n // See RFC3986, or this handy table:\n // http://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters\n //\n self.pattern += &#39;([a-zA-Z0-9-_~\\\\.%]+)&#39;;\n self.params.push(fragment.slice(1, optional ? -1 : undefined));\n } else {\n self.pattern += fragment;\n }\n\n if (optional) self.pattern += &#39;?&#39;;\n });\n\n if (this.pattern === &#39;^&#39;) this.pattern += &#39;\\\\/&#39;;\n this.pattern += &#39;$&#39;;\n\n return this.compile();\n}\n</code></pre><p>});</p><p>/**<br />Compile our dis-assembled source to a new xRegExp instance.</p>", "summary": "<p>: &#39;\\/+&#39;;</p>", "body": "<pre><code> if (named) {\n //\n // Previously was gratuitous, but better to just be standard\n // self.pattern += &#39;([a-zA-Z0-9-_~%!;@=+\\\\$\\\\*\\\\.]+)&#39;;\n //\n // See RFC3986, or this handy table:\n // http://en.wikipedia.org/wiki/Percent-encoding#Types_of_URI_characters\n //\n self.pattern += &#39;([a-zA-Z0-9-_~\\\\.%]+)&#39;;\n self.params.push(fragment.slice(1, optional ? -1 : undefined));\n } else {\n self.pattern += fragment;\n }\n\n if (optional) self.pattern += &#39;?&#39;;\n });\n\n if (this.pattern === &#39;^&#39;) this.pattern += &#39;\\\\/&#39;;\n this.pattern += &#39;$&#39;;\n\n return this.compile();\n}\n</code></pre><p>});</p><p>/**<br />Compile our dis-assembled source to a new xRegExp instance.</p>" }, "isPrivate": true, "ignore": false, "code": "Route.prototype.compile = function compile() {\n this.compiled = xRegExp(this.pattern, this.flags);\n\n return this;\n};", "ctx": { "type": "method", "constructor": "Route", "cons": "Route", "name": "compile", "string": "Route.prototype.compile()" } }, { "tags": [ { "type": "param", "types": [ "String" ], "name": "uri", "description": "The URI we want to test against this route." }, { "type": "returns", "string": "{Boolean}" }, { "type": "api", "visibility": "public" } ], "description": { "full": "<p>Check if URL matches the route.</p>", "summary": "<p>Check if URL matches the route.</p>", "body": "" }, "isPrivate": false, "ignore": false, "code": "Route.prototype.test = function test(uri) {\n return this.compiled.test(uri);\n};", "ctx": { "type": "method", "constructor": "Route", "cons": "Route", "name": "test", "string": "Route.prototype.test()" } }, { "tags": [ { "type": "param", "types": [ "Object" ], "name": "req", "description": "an http request object." }, { "type": "return", "types": [ "Object" ], "description": "parameters of the configured route -> URL." }, { "type": "throws", "types": [ "TypeError" ], "description": "on input error." }, { "type": "api", "visibility": "public" } ], "description": { "full": "<p>Whether or not the route matches the given request&#39;s URL.</p>", "summary": "<p>Whether or not the route matches the given request&#39;s URL.</p>", "body": "" }, "isPrivate": false, "ignore": false, "code": "Route.prototype.exec = function exec(uri) {\n var re = xRegExp(this.pattern, this.flags)\n , params = Object.create(null)\n , result = re.exec(uri)\n , i = 0;\n\n if (!result) return undefined;\n\n //\n // Extract the parameters from the URL.\n //\n if (this.params && this.params.length) {\n this.params.forEach(function parseParams(p) {\n if (++i < result.length) params[p] = result[i] ? decodeURIComponent(result[i]) : null;\n });\n } else if (this._url instanceof RegExp) {\n for (i = 0; i < result.length; i++) {\n if (i === 0) continue;\n\n params[(i - 1)] = result[i];\n }\n } else if (re.xregexp && re.xregexp.captureNames) {\n re.xregexp.captureNames.forEach(function each(key) {\n params[key] = result[key];\n });\n }\n\n //\n // Iterate over the parsers so they can transform the results if needed.\n //\n for (var param in params) {\n if (param in this.parsers) {\n params[param] = this.parsers[param](params[param], uri, param);\n }\n }\n\n return params;\n};", "ctx": { "type": "method", "constructor": "Route", "cons": "Route", "name": "exec", "string": "Route.prototype.exec()" } }, { "tags": [ { "type": "param", "types": [ "String" ], "name": "name", "description": "name of the param" }, { "type": "param", "types": [ "Function" ], "name": "fn", "description": "parser of the param" }, { "type": "api", "visibility": "public" } ], "description": { "full": "<p>Add a custom param parser for when we execute the route on a given URL.</p>", "summary": "<p>Add a custom param parser for when we execute the route on a given URL.</p>", "body": "" }, "isPrivate": false, "ignore": false, "code": "Route.prototype.param = function param(name, fn) {\n this.parsers[name] = fn;\n\n return this;\n};", "ctx": { "type": "method", "constructor": "Route", "cons": "Route", "name": "param", "string": "Route.prototype.param()" } }, { "tags": [ { "type": "return", "types": [ "String" ], "description": "in the form indicated above." }, { "type": "api", "visibility": "private" } ], "description": { "full": "<p>String representation.</p>", "summary": "<p>String representation.</p>", "body": "" }, "isPrivate": true, "ignore": false, "code": "Route.prototype.toString = function toString() {\n var str = this.url.toString()\n , opts = [];\n\n if (this.version) opts.push('version=' + this.version);\n if (opts.length) str += ' (' + opts.join(', ') + ')';\n\n return str;\n};\n\nRoute.extend = require('extendible');", "ctx": { "type": "method", "constructor": "Route", "cons": "Route", "name": "toString", "string": "Route.prototype.toString()" } } ]