Git Product home page Git Product logo

uri.js's Introduction

URI.js


I always want to shoot myself in the head when looking at code like the following:

var url = "http://example.org/foo?bar=baz",
    separator = url.indexOf('?') > -1 ? '&' : '?';

url += separator + encodeURIComponent("foo") + "=" + encodeURIComponent("bar");

I still can't believe javascript - the f**ing backbone-language of the web - doesn't offer an API for mutating URLs. Browsers (Firefox) don't expose the Location object (the structure behind window.location). Yes, one could think of decomposed IDL attributes as a native URL management library. But it relies on the DOM element <a>, it's slow and doesn't offer any convenienve at all.

How about a nice, clean and simple API for mutating URIs:

var url = new URL("http://example.org/foo?bar=baz");
url.addQuery("foo", "bar");

URI.js is here to help with that.

API Example

// mutating URLs
URI("http://example.org/foo.html?hello=world")
    .username("rodneyrehm")
        // -> http://[email protected]/foo.html?hello=world
    .username("")
        // -> http://example.org/foo.html?hello=world
    .directory("bar")
        // -> http://example.org/bar/foo.html?hello=world
    .suffix("xml")
        // -> http://example.org/bar/foo.xml?hello=world
    .query("")
        // -> http://example.org/bar/foo.xml
    .tld("com")
        // -> http://example.com/bar/foo.xml
    .query({ foo: "bar", hello: ["world", "mars"] });
        // -> http://example.com/bar/foo.xml?foo=bar&hello=world&hello=mars

// cleaning things up
URI("?&foo=bar&&foo=bar&foo=baz&")
    .normalizeQuery();
        // -> ?foo=bar&foo=baz

// working with relative paths
URI("/foo/bar/baz.html")
    .relativeTo("/foo/bar/world.html");
        // -> ./baz.html

URI("/foo/bar/baz.html")
    .relativeTo("/foo/bar/sub/world.html")
        // -> ../baz.html
    .absoluteTo("/foo/bar/sub/world.html");
        // -> /foo/bar/baz.html

See the About Page and API Docs for more stuff.

npm

npm install URIjs

Server-side JS

var URI = require('URIjs');

URI("/foo/bar/baz.html")
    .relativeTo("/foo/bar/sub/world.html")
// -> ../baz.html

Minify

use Google Closure Compiler:

// ==ClosureCompiler==
// @compilation_level SIMPLE_OPTIMIZATIONS
// @output_file_name URI.min.js
// @code_url http://medialize.github.com/URI.js/src/IPv6.js
// @code_url http://medialize.github.com/URI.js/src/punycode.js
// @code_url http://medialize.github.com/URI.js/src/SecondLevelDomains.js
// @code_url http://medialize.github.com/URI.js/src/URI.js
// ==/ClosureCompiler==

Resources

Docs where you get more info on parsing and working with URLs

Discussion on Hacker News

HTML5 URL Draft

MozURLProperty

Alternatives

If you don't like URI.js, you may like one of these:

TODO

if you want to get involved, these are things you could help out with…

Authors

Contains Code From

License

URI.js is published under the MIT license and GPL v3.

Changelog

1.6.3 (June 24th 2012)

  • fixing .absoluteTo() to join two relative paths properly (Issue #29)
  • adding .clone() to copy an URI instance

1.6.2 (June 23rd 2012)

  • .directory() now returns empty string if there is no directory
  • fixing .absoluteTo() to join two relative paths properly (Issue #29)

1.6.1 (May 19th 2012)

  • fixing TypeError on domain() with dot-less hostnames (Issue #27)

1.6.0 (March 19th 2012)

  • adding URN (javascript:, mailto:, ...) support
  • adding .scheme() as alias of .protocol()
  • adding .userinfo() to comply with terminology of RFC 3986
  • adding jQuery Plugin src/jquery.URI.js
  • Fixing relative scheme URLs (Issue #19 byroot)

1.5.0 (February 19th 2012)

  • adding Second Level Domain (SLD) Support (Issue #17)

1.4.3 (January 28th 2012)

1.4.2 (January 25th 2012)

1.4.1 (January 21st 2012)

1.4.0 (January 12th 2012)

  • added URI.iso8859() and URI.unicode() to switch base charsets (Issue #10, mortenn)
  • added .iso8859() and .unicode() to convert an URI's escape encoding

1.3.1 (January 3rd 2011)

  • Updated Punycode.js to version 0.3.0
  • added edge-case tests ("jim")
  • fixed edge-cases in .protocol(), .port(), .subdomain(), .domain(), .tld(), .filename()
  • fixed parsing of hostname in .hostname()

1.3.0 (December 30th 2011)

  • added .subdomain() convenience accessor
  • improved internal deferred build handling
  • fixed thrown Error for URI("http://example.org").query(true) (Issue #6)
  • added examples for extending URI.js for fragment abuse, see src/URI.fragmentQuery.js and src/URI.fragmentURI.js (Issue #2)

1.2.0 (December 29th 2011)

  • added .equals() for URL comparison
  • proper encoding/decoding for .pathname(), .directory(), .filename() and .suffix() according to RFC 3986 3.3
  • escape spaces in query strings with + according to application/x-www-form-urlencoded
  • allow URI.buildQuery() to build duplicate key=value combinations
  • added URI(string, string) constructor to conform with the specification
  • added .readable() for humanly readable representation of encoded URIs
  • fixed bug where @ in pathname would be parsed as part of the authority

1.1.0 (December 28th 2011)

1.0.0 (December 27th 2011)

  • Initial URI.js

uri.js's People

Contributors

rodneyrehm avatar christianharms avatar fgribreau avatar hgezim avatar mutewinter avatar mark-rushakoff avatar mathiasbynens avatar mortenn avatar victorblomberg avatar

Stargazers

Chaithanya Maisagoni avatar

Watchers

James Cloos avatar Chaithanya Maisagoni avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.