UNPKG

2.85 kBMarkdownView Raw
1[1]: https://badgen.net/travis/litejs/uri-template-lite
2[2]: https://travis-ci.org/litejs/uri-template-lite
3[3]: https://badgen.net/coveralls/c/github/litejs/uri-template-lite
4[4]: https://coveralls.io/r/litejs/uri-template-lite
5[5]: https://badgen.net/packagephobia/install/uri-template-lite@19.11.0
6[6]: https://packagephobia.now.sh/result?p=uri-template-lite
7[7]: https://badgen.net/badge/icon/Buy%20Me%20A%20Tea/orange?icon=kofi&label
8[8]: https://www.buymeacoffee.com/lauriro
9
10[RFC 6570]: http://tools.ietf.org/html/rfc6570
11
12
13URI Template Lite – [![Build][1]][2] [![Coverage][3]][4] [![Size][5]][6] [![Buy Me A Tea][7]][8]
14=================
15
16URI Template [RFC 6570][] expansion and extraction.
17
18Usage
19-----
20
21```javascript
22// Call `expand` directly
23var data = {"domain":"example.com", "user":"fred", "query":"mycelium"}
24URI.expand("http://{domain}/~{user}/foo{?query,number}", data)
25// Returns http://example.com/~fred/foo?query=mycelium
26
27// ..or use `Template` constructor
28var data = {"domain":"example.com", "user":"fred", "query":"mycelium", "number": 3}
29var template = new URI.Template("http://{domain}/~{user}/foo{?query,number}")
30template.expand(data)
31// Returns http://example.com/~fred/foo?query=mycelium&number=3
32
33// Extract variables
34template.match("http://example.com/~fred/foo?query=mycelium&number=3")
35// Returns {"domain":"example.com", "user":"fred", "query":"mycelium", "number": "3"}
36
37template.match("http://other.com/?query=mycelium")
38// Returns null
39```
40
41
42Installation
43------------
44
45To use it in the browser, include uri-template.js in your site
46
47```html
48<script src=uri-template.js></script>
49```
50
51In node.js: `npm install uri-template-lite`
52
53```javascript
54var URI = require("uri-template-lite").URI
55```
56
57
58About error handling
59--------------------
60
61This implementation tries to do a best effort template expansion
62and leaves erroneous expressions in the returned URI
63instead of throwing errors.
64So for example, the incorrect expression
65{unclosed will return {unclosed as output.
66
67
68
69Browser Support
70---------------
71
72It should work IE6 and up but automated testing is currently broken.
73
74- For older browsers it requires `browser-upgrade-lite` package
75 or other ES5 polyfill.
76- FF3-4 `escape` should be patched to remove non-standard 2nd param,
77 `browser-upgrade-lite` does that.
78
79
80
81## External links
82
83[GitHub repo](https://github.com/litejs/uri-template-lite) |
84[npm package](https://npmjs.org/package/uri-template-lite) |
85URI Template [RFC 6570][] and
86[Tests](https://github.com/uri-templates/uritemplate-test) |
87[Travis CI](https://travis-ci.org/litejs/uri-template-lite) |
88[Coveralls coverage](https://coveralls.io/github/litejs/uri-template-lite) |
89[Buy Me A Tea][8]
90
91
92## Licence
93
94Copyright (c) 2014-2019 Lauri Rooden &lt;lauri@rooden.ee&gt;
95[The MIT License](http://lauri.rooden.ee/mit-license.txt)
96
97
98
99