a simple URI class
Updated on 2007-08-27 to work with Prototype 1.6 release candidate
what ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var uri = $U("ftp://billy:bobby@ftp.somewhere.com/path/to/something"); uri.scheme // => "ftp" uri.user // => "billy" uri.host // => "ftp.somewhere.com" uri.path // => "/path/to/something" uri == $U(uri) // => true uri.user = 'robert'; uri.path = '/other/place'; uri.scheme = 'http'; uri.toString(); // => "http://robert:bobby@ftp.somewhere.com/other/place" |
source
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
/* Copyright (c) 2007 Samuel Lebeau this piece of software is largely inspired by http://download.dojotoolkit.org/release-0.4.2/dojo-0.4.2-ajax/src/uri/Uri.js Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var URI = Class.create({ initialize: function(uri) { var r = uri.match(this.constructor.uriPattern); if (!r) throw this.constructor._invalidURIError(uri); this._uri = uri; this.scheme = r[2] || (r[1] ? '' : null); this.authority = r[4] || (r[3] ? '' : null); this.path = r[5]; this.query = r[7] || (r[6] ? '' : null); this.fragment = r[9] || (r[8] ? '' : null); if (this.authority) { r = this.authority.match(this.constructor.authorityPattern); this.user = r[3] || null; this.password = r[4] || null; this.host = r[5]; this.port = parseInt(r[7]) || null; } }, toString: function() { return (this.scheme ? this.scheme + '://' : '') + (this.user ? this.user + (this.password ? ':' + this.password : '') + '@' : '' ) + (this.host ? this.host + (this.port ? ':' + this.port : '') : '') + this.path + (this.query ? '?' + this.query : '') + (this.fragment ? '#' + this.fragment : ''); } }); Object.extend(URI, { parse: function(uri) { return new this(uri); }, from: function(object) { return (object.constructor == URI) ? object : URI.parse(object.toString()); }, uriPattern: new RegExp("^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$"), authorityPattern: new RegExp("^(((?:([^:]+):)?([^@]+))@)?([^:]*)(:([0-9]+))?$"), _invalidURIError: function(uri) { this.name = 'InvalidURIError'; this.message = 'Invalid URI : ' + uri + ''; } }); var $U = URI.from; |
1 comment
Comments
-
Hey, this is useful. I might pick this up in Jester, especially when I begin chugging along on remote site support. I just released Jester with JSON support over on thoughtbot's blog, by the way.