Code Monkey home page Code Monkey logo

uri-js's Introduction

URI.js

URI.js is an RFC 3986 compliant, scheme extendable URI parsing/validating/resolving library for all JavaScript environments (browsers, Node.js, etc). It is also compliant with the IRI (RFC 3987) and IDNA (RFC 5890) specifications.

URI.js weighs in at only 2.3kb (gzipped, 7kb deflated). Need IRI support? It's only an extra 1.3kb (3.6kb gzipped, 9.8kb deflated).

API

Parsing & Validating

var components = URI.parse("uri://user:[email protected]:123/one/two.three?q1=a1&q2=a2#body");
//returns:
//{
//  scheme : "uri",
//  userinfo : "user:pass",
//  host : "example.com",
//  port : 123,
//  path : "/one/two.three",
//  query : "q1=a1&q2=a2",
//  fragment : "body"
//}

Serializing

URI.serialize({scheme : "http", host : "example.com", fragment : "footer"}) === "http://example.com/#footer"

Resolving

URI.resolve("uri://a/b/c/d?q", "../../g") === "uri://a/g"

Normalizing

URI.normalize("HTTP://ABC.com:80/%7Esmith/home.html") === "http://abc.com/~smith/home.html"

Comparison

URI.equal("example://a/b/c/%7Bfoo%7D", "eXAMPLE://a/./b/../b/%63/%7bfoo%7d") === true

IRI Support

//convert IRI to URI
URI.serialize(URI.parse("http://examplé.org/rosé")) === "http://xn--exampl-gva.org/ros%C3%A9"
//convert URI to IRI
URI.serialize(URI.parse("http://xn--exampl-gva.org/ros%C3%A9"), {iri:true}) === "http://examplé.org/rosé"

Options

All of the above functions can accept an additional options argument that is an object that can contain one or more of the following properties:

  • scheme (string)

    Indicates the scheme that the URI should be treated as, overriding the URI's normal scheme parsing behavior.

  • reference (string)

    If set to "suffix", it indicates that the URI is in the suffix format, and the validator will use the option's scheme property to determine the URI's scheme.

  • tolerant (boolean, false)

    If set to true, the parser will not report invalid URIs. It will also relax URI resolving rules.

  • absolutePath (boolean, false)

    If set to true, the serializer will not resolve a relative path component.

  • iri (boolean, false)

    If set to true, the serializer will unescape non-ASCII characters as per RFC 3987.

  • unicodeSupport (boolean, false)

    If set to true, the parser will unescape non-ASCII characters in the parsed output as per RFC 3987.

  • domainHost (boolean, false)

    If set to true, the library will treat the host component as a domain name, and convert IDNs (International Domain Names) as per RFC 5891.

Scheme Extendable

URI.js supports inserting custom scheme dependent processing rules. Currently, URI.js has built in support for the following schemes:

Note: The minified version of URI.js only comes with http/https support compiled in.

HTTP Support

URI.equal("HTTP://ABC.COM:80", "http://abc.com/") === true

Mailto Support

URI.parse("mailto:[email protected],[email protected]?subject=SUBSCRIBE&body=Sign%20me%20up!");
//returns:
//{
//	scheme : "mailto", 
//	to : ["[email protected]", "[email protected]"],
//	subject : "SUBSCRIBE",
//	body : "Sign me up!"
//}

URI.serialize({
	scheme : "mailto", 
	to : ["[email protected]"], 
	subject : "REMOVE", 
	body : "Please remove me",
	headers : {
		cc : "[email protected]"
	}
}) === "mailto:[email protected][email protected]&subject=REMOVE&body=Please%20remove%20me"

Usage

To load in a browser, use the following tag:

<script type="text/javascript" src="uri-js/dist/uri.min.js"></script>

If you need IRI support, use the following tag instead:

<script type="text/javascript" src="uri-js/dist/uri-iri.min.js"></script>

To load in a CommonJS (Node.js/io.js) environment, first install with npm by running on the command line:

npm install uri-js

Then, in your code, load it using:

var URI = require("uri-js");

Breaking changes from 1.x

The errors array on parsed components as now been changed to an error string.

License

Copyright 2011 Gary Court. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY GARY COURT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GARY COURT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the authors and should not be interpreted as representing official policies, either expressed or implied, of Gary Court.

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.