Node.js/Javascript RFC 6570 URI Template implementation that supports both URI expansion and extraction.
- PHP URI Template
A very simple usage
UriTemplate.expand('/{username}/profile',{'username': 'john'});>>'/john/profile'More examples (supports all expansion levels defined in RFC6570)
UriTemplate.expand('/search/{term:1}/{term}/{?q*,limit}',{term: 'john',q: ['a','b'],limit: 10,});>>'/search/j/john/?q=a,b&limit=10'Extract variables from URI.
UriTemplate.extract('/search/{term:1}/{term}/{?q*,limit}','/search/j/john/?q=a&q=b&limit=10');>>('term:1': 'j',term: 'john',q: ['a','b']limit: 10)You can also instantiate UriTemplate instance via new keyword. Its constructor accepts 2 optional params base uri and default params. Which is very useful when working with API endpoint.
varuri=newUriTemplate('https://api.twitter.com/{version}',{'version': 1.1});uri.expand('/statuses/show/{id}.json',{'id': '210462857140252672'});>>https://api.twitter.com/1.1/statuses/show/210462857140252672.jsonIn browsers:
bower install uri-template.js<scriptsrc="dist/uri-template.js"></script>
UriTemplate.expand(...)In Node.js
npm install uri-template.js
UriTemplate = require('uri-template.js');