Projects | Contribute in GitHub

litejs

@version    0.1.11
@date       2015-02-09
@stability  2 - Unstable

URI Template – Build Coverage

URI Template RFC 6570 expansion and extraction.

Usage

// Call `expand` directly
var data = {"domain":"example.com", "user":"fred", "query":"mycelium"}
URI.expand("http://{domain}/~{user}/foo{?query,number}", data)
// Returns http://example.com/~fred/foo?query=mycelium

// ..or use `Template` constructor
var data = {"domain":"example.com", "user":"fred", "query":"mycelium", "number": 3}
var template = new URI.Template("http://{domain}/~{user}/foo{?query,number}")
template.expand(data)
// Returns http://example.com/~fred/foo?query=mycelium&number=3

// Extract variables
template.match("http://example.com/~fred/foo?query=mycelium&number=3")
// Returns {"domain":"example.com", "user":"fred", "query":"mycelium", "number": "3"}

template.match("http://other.com/?query=mycelium")
// Returns false

Installation

To use it in the browser, include uri-template-min.js in your site

<script src=uri-template-min.js></script>

In node.js: npm install uri-template-lite

var URI = require("uri-template-lite").URI

About error handling

This implementation tries to do a best effort template expansion and leaves erroneous expressions in the returned URI instead of throwing errors. So for example, the incorrect expression {unclosed will return {unclosed as output.

Browser Support

It should work IE6 and up but automated testing is currently broken.

Licence

Copyright (c) 2014-2015 Lauri Rooden <[email protected]>
The MIT License


Tags: uri, url, template, uri-template, rfc6570, litejs

Have an update or suggestion for this note? You can edit it and send me a pull request.

Fork me on GitHub

Since 2013 to 2015