Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including uri-template-lite with all npm packages installed. Try it out:

var uriTemplateLite = require("uri-template-lite")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

uri-template-lite v20.5.0

URI Template [RFC 6570] expansion and extraction

URI Template Lite – Build Coverage Size Buy Me A Tea

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 null

Installation

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

<script src=uri-template.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.

  • For older browsers it requires browser-upgrade-lite package or other ES5 polyfill.
  • FF3-4 escape should be patched to remove non-standard 2nd param, browser-upgrade-lite does that.

External links

GitHub repo | npm package | URI Template RFC 6570 and Tests | Travis CI | Coveralls coverage | Buy Me A Tea

Licence

Copyright (c) 2014-2019 Lauri Rooden <lauri@rooden.ee>
The MIT License

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free