uri

0.1.0 • Public • Published

URI Type for javascript

  • Supports all kinds of URI (URL, URN, any scheme).
  • Relative URI Resolution
  • All classes extend the native String implementation.
  • Pure ECMA-262 implementation of URI spec (RFC-3986)
  • Works Client or Server side, (V8 / node.js compatible).

Usage:

var u = new URI('http://user:pass@github.com/webr3/URI?query=string#this');

URI extends the native String implementation, so 'u' is a string, and you can call all the normal String methods like u.indexOf(), u.match(), u.substr() and so forth.

In addition, URI exposes the following URI specific methods for reading parts of a URI (each of which are strings themselves):

u.scheme(); -> 'http:' u.heirpart(); -> '//user:pass@github.com/webr3/URI' u.heirpart().authority(); -> 'user:pass@github.com' u.heirpart().authority().userinfo(); -> 'user:pass' u.heirpart().authority().host(); -> 'github.com' u.heirpart().path(); -> '/webr3/URI' u.querystring(); -> '?query=string' u.fragment(); -> '#this'

Further, methods are exposed to handle complicated URIs:

u = new URI('http://github.com/a/b/c/d/../.././egg#s/../x'); u.isAbsolute(); -> boolean u.defrag(); -> 'http://github.com/a/b/c/d/../.././egg' u.toAbsolute(); -> 'http://github.com/a/b/egg'

and to resolve relative URIs + URI References:

u = new URI('http://github.com/a/b/c/d'); u.resolveReference('../.././n?x=y'); -> 'http://github.com/a/n?x=y'

node.js usage:

require('./uris'); // require the file, and no, you don't need 'var uri =' var u = new URI('http://github.com/webr3/URI'); // nothing's different

running the tests:

URI.Test(); // be sure to include uristest first

Notes:

  • I may add case normalisation for segments which support it..
  • the file is called uris.js instead of uri.js because for some reason the line require('uri') makes v8/node throw a wobbly..
  • validation is out of scope, couldn't possibly implement validation for every scheme..

A few examples of some of the URIs supported:

Readme

Keywords

none

Package Sidebar

Install

npm i uri

Weekly Downloads

102

Version

0.1.0

License

none

Last publish

Collaborators

  • s3u