Commit 722a84ae authored by fxa's avatar fxa

initial commit

parent b5217ba3
[submodule "uritemplate-test"]
path = uritemplate-test
url = https://github.com/uri-templates/uritemplate-test.git
uritemplate-js URI Template JS
============== ===============
An javascript implementation of RFC6570 Uri Templates This is a javascript implementation of [RFC6570](http://tools.ietf.org/html/rfc6570) - URI Template,
\ No newline at end of file and can expand templates up to and including Level 4 in that specification.
It exposes a constructor function UriTemplate with the two methods:
* (static) parse(uriTemplateText)
* expand(variables)
Requirements
------------
You can use uri-template.js in any modern browsers (Tested even with IE8 in IE7-Mode), see file demo.html.
But you can also use it with node:
var
UriTemplate = require('./path/to/uritemplate.js'),
template,
expanded;
template = UriTemplate.parse('{?query*}';
template.expand({query: {firstParam: "firstValue", secondParam: "secondValue"}});
--> "?firstParam=firstValue&secondParam=secondValue"
Tests
-----
The tests are taken from https://github.com/uri-templates/uritemplate-test as a submodule.
Run the tests with
node test.js
Comming soon
------------
* npm support with package.json
* npm support for npm install
* A new method extract(uri), which tries to extract the variables from a given uri
License
-------
Copyright 2012 Franz Antesberger
MIT License, see http://mit-license.org/
<!DOCTYPE html>
<html>
<head>
<title>Demo of Usage</title>
<script type="text/javascript" src="src/uri-template.js"></script>
</head>
<body>
<div id="id"></div>
<script type="text/javascript">
(function () {
"use strict";
var
templateText = "{?query*}",
variables = {
query: {
firstParam: "firstValue",
secondParam: "secondValue"
}
};
document.getElementById('id').innerHTML =
"<p>When you have a template of the form</p><p><code>var templateText = \"" + templateText
+ "\";</code></p><p>and params of the form </p><p><code>var variables = " + JSON.stringify(variables)
+ ";</code></p><p>, you can use </p><p><code>UriTemplate.parse(templateText).expand(variables); </code></p><p>to produce </p><p><code>"
+ UriTemplate.parse(templateText).expand(variables)
+ "</code></p><p> Look at the source code of this page!</p>";
}());
</script>
</body>
</html>
\ No newline at end of file
This diff is collapsed.
(function () {
"use strict";
var
assert = require('assert'),
fs = require('fs'),
UriTemplate = require('./src/uri-template.js'),
numTestsPassed = 0;
function assertMatches(template, variables, expected, chapterName) {
var
uriTemplate,
actual,
index;
try {
uriTemplate = UriTemplate.parse(template);
}
catch (error) {
// if expected === false, the error was expected!
if (expected === false) {
return;
}
assert.fail('chapter "' + chapterName + '", template "' + template + '" threw error: ' + error);
return;
}
assert.ok(!!uriTemplate, 'uri template could not be parsed');
try {
actual = uriTemplate.expand(variables);
if (expected === false) {
assert.fail('chapter "' + chapterName + '", template "' + template + '" expected to fail, but returned \'' + actual + '\'!');
return;
}
}
catch (exception) {
if (expected === false) {
return;
}
assert.fail('chapter "' + chapterName + '", template "' + template + '" threw error: ' + exception);
return;
}
if (expected.constructor === Array) {
// actual must match at least one of the listed elements
for (index = 0; index < expected.length; index += 1) {
if (actual === expected[index]) {
return;
}
}
assert.fail('actual: ' + actual + ', expected: one of ' + JSON.stringify(expected) + ", " + 'chapter "' + chapterName + '", template "' + template + '"');
}
else {
assert.equal(actual, expected, 'actual: "' + actual + '", expected: "' + expected + '", template: "' + template + '"');
}
}
function runTestFile(filename) {
var
tests,
chapterName,
chapter,
variables,
index,
template,
expexted;
tests = JSON.parse(fs.readFileSync(filename));
for (chapterName in tests) {
if (tests.hasOwnProperty(chapterName)) {
chapter = tests[chapterName];
variables = chapter.variables;
for (index = 0; index < chapter.testcases.length; index += 1) {
template = chapter.testcases[index][0];
expexted = chapter.testcases[index][1];
assertMatches(template, variables, expexted, chapterName);
numTestsPassed += 1;
}
console.log(chapterName);
}
}
}
runTestFile('uritemplate-test/spec-examples.json');
runTestFile('uritemplate-test/extended-tests.json');
runTestFile('uritemplate-test/negative-tests.json');
console.log('passed all ' + numTestsPassed + ' tests!');
}());
\ No newline at end of file
Subproject commit 34fd46be98016e761897a3e30ec49cc65a695492
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment