uri-template-lite
Advanced tools
Comparing version 0.1.4 to 0.1.6
94
index.js
@@ -5,4 +5,4 @@ | ||
/* | ||
* @version 0.1.4 | ||
* @date 2014-05-03 | ||
* @version 0.1.6 | ||
* @date 2014-05-20 | ||
* @stability 2 - Unstable | ||
@@ -17,3 +17,2 @@ * @author Lauri Rooden <lauri@rooden.ee> | ||
var RESERVED = /[\]\[:\/?#@!$&()*+,;=']/g | ||
, RE = /\{([+#.\/;?&]?)((?:[\w%.]+(\*|:\d)?,?)+)\}/g | ||
, SEPARATORS = { | ||
@@ -23,2 +22,6 @@ '':",", '+':",", '#':"," //, ';':";" | ||
} | ||
, escapeRe = /[.*+?^=!:${}()|\[\]\/\\]/g | ||
, expandRe = /\{([+#.\/;?&]?)((?:[\w%.]+(\*|:\d)?,?)+)\}/g | ||
//, parseRe = /\{([+#.\/;?&]?)((?:[\w%.]+(\*|:\d)?,?)+)\}|.[^{]*?/g | ||
, parseRe = new RegExp(expandRe.source + "|.[^{]*?", "g") | ||
@@ -28,3 +31,11 @@ function encodeNormal(val) { | ||
} | ||
function decodeNormal(val) { | ||
return decodeURIComponent(val)//.replace(RESERVED, escape) | ||
} | ||
function escapeRegExp(string) { | ||
return string.replace(escapeRe, "\\$&") | ||
} | ||
function addNamed(name, val, sep) { | ||
@@ -39,4 +50,5 @@ return name + (val || sep == "&" ? "=" : "") + val; | ||
//** EXPAND | ||
function expand(template, data) { | ||
return template.replace(RE, function(_, op, vals) { | ||
return template.replace(expandRe, function(_, op, vals) { | ||
var sep = SEPARATORS[op] || op | ||
@@ -77,4 +89,78 @@ , enc = op && sep == "," ? encodeURI : encodeNormal | ||
URI.expand = expand | ||
//*/ | ||
function Template(template) { | ||
var self = this | ||
//if (!(self instanceof Template)) return new Template(template) | ||
//** PARSE | ||
self.init(self.template = template) | ||
//*/ | ||
//** EXPAND | ||
self.expand = expand.bind(self, template) | ||
//*/ | ||
} | ||
/* | ||
* sep = sep || '&'; | ||
* eq = eq || '='; | ||
*/ | ||
//** PARSE | ||
Template.prototype = { | ||
init: function(template) { | ||
var pos = 0 | ||
, fnStr = "" | ||
, lengths = {} | ||
, reStr = "^"+ template.replace(parseRe, function(_, op, key) { | ||
if (!key) return escapeRegExp(_) | ||
var separator = SEPARATORS[op] || op | ||
, dec = op && separator == "," ? decodeURI : decodeNormal | ||
, add = (separator == ";" || separator == "&") | ||
, reGroup = "(.+?)" | ||
fnStr += 'sep="'+separator+'";' | ||
var reGroup = key.split(",").map(function(name) { | ||
var len, exp | ||
name = name.replace(/(?:(\*)|:(\d+))$/, function(_, _exp, _len) { | ||
len = _len | ||
exp = _exp | ||
return "" | ||
}) | ||
var reGroup = "(.*?)" | ||
pos++ | ||
//console.log("KEY", arguments) | ||
if (len) { | ||
reGroup = "((?:%..|.){1,"+len+"})" | ||
lengths[name] = {pos:pos, len: len} | ||
} | ||
else if (len = lengths[name]) { | ||
reGroup = "(\\"+len.pos+".*?)" | ||
} | ||
fnStr += "t=(parts["+pos+"]||'').split('"+ separator +"');" | ||
fnStr += "out[\""+name+"\"]=t.length>1?t.map(decodeURIComponent):decodeURIComponent(t[0]);" | ||
return add ? | ||
separator == "&" ? | ||
escapeRegExp(name + "=") + reGroup | ||
: escapeRegExp(name) + "(?:="+reGroup+")?" | ||
: reGroup | ||
}).join(escapeRegExp(separator)) | ||
return (op!="+"?escapeRegExp(op):"")+reGroup | ||
}) + "$" | ||
this.re = new RegExp(reStr) | ||
this.fn = new Function("parts", "var t,sep,eq,out={};"+fnStr+";return out") | ||
}, | ||
match: function(uri) { | ||
var match = this.re.exec(uri) | ||
return match && this.fn(match) | ||
} | ||
} | ||
//*/ | ||
URI.Template = Template | ||
// `this` is `exports` in NodeJS and `window` in browser. | ||
}(this.URI || (this.URI = {})); | ||
{ | ||
"name": "uri-template-lite", | ||
"version": "0.1.4", | ||
"version": "0.1.6", | ||
"stability": 2, | ||
@@ -30,3 +30,13 @@ "license": "MIT", | ||
"buildman": { | ||
"uri-template-min.js": "index.js" | ||
"build/uri-template-min.js": "index.js", | ||
"build/uri-expand.js": { | ||
"devel": true, | ||
"input": "index.js", | ||
"toggle": "PARSE" | ||
}, | ||
"build/uri-parse.js": { | ||
"devel": true, | ||
"input": "index.js", | ||
"toggle": "EXPAND" | ||
} | ||
}, | ||
@@ -33,0 +43,0 @@ "testling": { |
@@ -7,8 +7,6 @@ [1]: https://secure.travis-ci.org/litejs/uri-template-lite.png | ||
[8]: https://ci.testling.com/litejs/uri-template-lite | ||
[rfc-6570]: http://tools.ietf.org/html/rfc6570 | ||
[npm-package]: https://npmjs.org/package/uri-template-lite | ||
@version 0.1.4 | ||
@date 2014-05-03 | ||
@version 0.1.6 | ||
@date 2014-05-20 | ||
@stability 2 - Unstable | ||
@@ -45,2 +43,4 @@ | ||
### Simple expand | ||
```javascript | ||
@@ -52,4 +52,16 @@ var data = {"domain":"example.com", "user":"fred", "query":"mycelium"} | ||
### Using Template constructor | ||
```javascript | ||
var template = new URI.Template("http://{domain}/~{user}/foo{?query,number}") | ||
var data = {"domain":"example.com", "user":"fred", "query":"mycelium", "number": 3} | ||
template.expand(data) | ||
// http://example.com/~fred/foo?query=mycelium&number=3 | ||
template.match("http://example.com/~fred/foo?query=mycelium&number=3") | ||
// {"domain":"example.com", "user":"fred", "query":"mycelium", "number": "3"} | ||
template.match("http://other.com/?query=mycelium") | ||
// false | ||
``` | ||
About error handling | ||
@@ -81,7 +93,7 @@ -------------------- | ||
- [npm-package][] | ||
- [rfc-6570][] | ||
- [Source-code on Github](https://github.com/litejs/uri-template-lite) | ||
- [Package on npm](https://npmjs.org/package/uri-template-lite) | ||
- [RFC 6570 - URI Template](http://tools.ietf.org/html/rfc6570) | ||
### Licence | ||
@@ -88,0 +100,0 @@ |
@@ -11,6 +11,44 @@ | ||
var test = require("testman") | ||
.describe("URI Template"); | ||
function includeTests(json) { | ||
test.asserts.hasVals = function(a, b, options) { | ||
var ok = a && true | ||
ok && Object.keys(a).forEach(function(key){ | ||
if ( | ||
key in b && | ||
a[key] != b[key] && | ||
""+a[key] != ""+b[key] && | ||
(typeof a[key] == "string" && b[key] && (""+b[key]).slice(0, a[key].length) != a[key]) | ||
) ok = false | ||
ok || console.log("test", key, ok) | ||
}) | ||
ok || console.log("\n\nCompare:\n" + JSON.stringify(a) + "\n" + JSON.stringify(b)) | ||
return this.ok(ok, options || "Expected: "+b+" Got: "+a ) | ||
} | ||
test = test.describe("URI Template Expand"); | ||
includeExpandTests(require("./uritemplate-test/spec-examples.json")) | ||
includeExpandTests(require("./longer-spec-examples.json")) | ||
includeExpandTests(require("./uritemplate-test/spec-examples-by-section.json")) | ||
includeExpandTests(require("./uritemplate-test/extended-tests.json")) | ||
//includeExpandTests("uritemplate-test/negative-tests.json") | ||
test = test.describe("URI Template Match"); | ||
includeMatchTests(require("./uritemplate-test/spec-examples.json")) | ||
includeMatchTests(require("./longer-spec-examples.json")) | ||
//includeMatchTests(require("./uritemplate-test/spec-examples-by-section.json")) | ||
//includeMatchTests(require("./uritemplate-test/extended-tests.json")) | ||
//includeMatchTests("uritemplate-test/negative-tests.json") | ||
test.done() | ||
function includeExpandTests(json) { | ||
for(var level in json) { | ||
@@ -34,9 +72,28 @@ var arr = json[level].testcases, len = arr.length, i = 0 | ||
includeTests(require("./uritemplate-test/spec-examples.json")) | ||
includeTests(require("./uritemplate-test/spec-examples-by-section.json")) | ||
includeTests(require("./uritemplate-test/extended-tests.json")) | ||
//includeTests("uritemplate-test/negative-tests.json") | ||
function includeMatchTests(json) { | ||
for(var level in json) { | ||
var arr = json[level].testcases, len = arr.length, i = 0 | ||
, args = json[level].variables | ||
test.done() | ||
test = test.it("should pass "+level) | ||
for (;i<len;i++) { | ||
if (Array.isArray(arr[i][1])) { | ||
//test = test.ok(function(){ | ||
// return arr[i][1].indexOf(res) != -1 | ||
//}) | ||
} else { | ||
var uri = new URI.Template(arr[i][0]) | ||
res = uri.match(arr[i][1]) | ||
var msg = "# re: " + uri.re + " (" + arr[i][0] + ") : " + arr[i][1] + " ->\n" + | ||
JSON.stringify(res) + "\n" + | ||
JSON.stringify(args) | ||
//console.log(msg, uri.keys) | ||
test = test.type(res, "object", msg) | ||
test.hasVals(res, args, msg) | ||
} | ||
} | ||
} | ||
} | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
75721
18
1528
103