uri-template-lite
Advanced tools
Comparing version 0.1.8 to 0.1.10
61
index.js
@@ -5,4 +5,4 @@ | ||
/* | ||
* @version 0.1.8 | ||
* @date 2014-05-21 | ||
* @version 0.1.10 | ||
* @date 2014-12-08 | ||
* @stability 2 - Unstable | ||
@@ -16,10 +16,12 @@ * @author Lauri Rooden <lauri@rooden.ee> | ||
!function(URI) { | ||
/** | ||
* URI Template | ||
* @see http://tools.ietf.org/html/rfc6570 | ||
*/ | ||
var RESERVED = /[\]\[:\/?#@!$&()*+,;=']/g | ||
, SEPARATORS = { | ||
'':",", '+':",", '#':"," //, ';':";" | ||
, '?':"&" //, '&':"&", '/':"/", '.':"." | ||
} | ||
, SEPARATORS = {"": ",", "+": ",", "#": ",", "?": "&"} | ||
, escapeRe = /[.*+?^=!:${}()|\[\]\/\\]/g | ||
, expandRe = /\{([+#.\/;?&]?)((?:[\w%.]+(\*|:\d)?,?)+)\}/g | ||
//, parseRe = /\{([+#.\/;?&]?)((?:[\w%.]+(\*|:\d)?,?)+)\}|.[^{]*?/g | ||
, parseRe = new RegExp(expandRe.source + "|.[^{]*?", "g") | ||
@@ -33,7 +35,11 @@ | ||
function addNamed(name, val, sep) { | ||
return name + (val || sep == "&" ? "=" : "") + val; | ||
return name + (val || sep == "&" ? "=" : "") + val | ||
} | ||
function typeofString(s) { | ||
return typeof s == "string" | ||
} | ||
function mapCleanJoin(arr, mapFn, joinStr) { | ||
arr = arr.map(mapFn).filter(function(s){return typeof s == "string"}) | ||
arr = arr.map(mapFn).filter(typeofString) | ||
return arr.length && arr.join(joinStr) | ||
@@ -49,3 +55,3 @@ } | ||
var exp = name != (name = name.split("*")[0]) | ||
, len = !exp && (len = name.split(":"), name=len[0], len[1]) | ||
, len = !exp && (len = name.split(":"), name = len[0], len[1]) | ||
, val = data[name] | ||
@@ -58,15 +64,12 @@ | ||
val = mapCleanJoin(val, enc, exp ? add ? sep + name + "=" : sep : "," ) | ||
} | ||
else { | ||
len = exp ? "=" : "," | ||
} else { | ||
val = mapCleanJoin(Object.keys(val), function(key) { | ||
return enc(key) + len + enc(val[key]) | ||
}, exp && (add || sep == "/") ? sep : "," ) | ||
return enc(key) + (exp ? "=" : ",") + enc(val[key]) | ||
}, exp && (add || sep == "/") ? sep : ",") | ||
if (exp) add = null | ||
} | ||
if (!val) return | ||
} else { | ||
val = enc(len ? val.slice(0, len) : val) | ||
} | ||
else { | ||
val = enc( len ? val.slice(0, len) : val ) | ||
} | ||
@@ -76,3 +79,3 @@ return add ? add(name, val, sep) : val | ||
return out ? (op!="+"?op+out:out) : out === "" && (op=="#"||op==".") ? op : "" | ||
return out || out === "" ? (op != "+" ? op + out : out) : "" | ||
} | ||
@@ -88,3 +91,3 @@ )} | ||
//** PARSE | ||
self.init(self.template = template) | ||
self.init(template) | ||
//*/ | ||
@@ -110,3 +113,3 @@ //** EXPAND | ||
, lengths = {} | ||
, reStr = "^"+ template.replace(parseRe, function(_, op, key) { | ||
, reStr = "^" + template.replace(parseRe, function(_, op, key) { | ||
if (!key) return escapeRegExp(_) | ||
@@ -128,22 +131,24 @@ | ||
if (len) { | ||
re = "((?:%..|.){1,"+len+"})" | ||
lengths[name] = {pos:pos, len: len} | ||
re = "((?:%..|.){1," + len + "})" | ||
lengths[name] = {pos: pos, len: len} | ||
} | ||
else if (len = lengths[name]) { | ||
re = "(\\"+len.pos+".*?)" | ||
re = "(\\" + len.pos + ".*?)" | ||
} | ||
//TODO: decodeURIComponent throws an Error on invalid input, add try-catch | ||
fnStr += "t=($["+pos+"]||'').split('"+ separator +"').map(decodeURIComponent);" | ||
fnStr += "o[\""+name+"\"]=t.length>1?t:t[0];" | ||
fnStr += "o[\"" + name + "\"]=t.length>1?t:t[0];" | ||
return add ? | ||
separator == "&" ? | ||
escapeRegExp(name + "=") + re | ||
: escapeRegExp(name) + "(?:="+re+")?" | ||
: escapeRegExp(name) + "(?:=" + re + ")?" | ||
: re | ||
}).join(escapeRegExp(separator)) | ||
return (op!="+"?escapeRegExp(op):"")+reGroup | ||
return (op != "+" ? escapeRegExp(op) : "") + reGroup | ||
}) + "$" | ||
this.template = template | ||
this.re = new RegExp(reStr) | ||
this.fn = new Function("$", "var t,o={};"+fnStr+"return o") | ||
this.fn = new Function("$", "var t,o={};" + fnStr + "return o") | ||
}, | ||
@@ -150,0 +155,0 @@ match: function(uri) { |
{ | ||
"name": "uri-template-lite", | ||
"version": "0.1.8", | ||
"version": "0.1.10", | ||
"stability": 2, | ||
"license": "MIT", | ||
"author": "Lauri Rooden <lauri@rooden.ee>", | ||
"description": "URI Template [RFC 6570] implementation in less than 1kb", | ||
"description": "URI Template [RFC 6570] implementation", | ||
"keywords": [ | ||
"uri", | ||
"template", | ||
"rfc6570", | ||
"litejs" | ||
@@ -30,4 +31,4 @@ ], | ||
"buildman": { | ||
"build/uri-template-min.js": "index.js", | ||
"build/uri-expand.js": { | ||
"dist/uri-template-min.js": "index.js", | ||
"dist/uri-expand.js": { | ||
"devel": true, | ||
@@ -37,3 +38,3 @@ "input": "index.js", | ||
}, | ||
"build/uri-parse.js": { | ||
"dist/uri-parse.js": { | ||
"devel": true, | ||
@@ -43,28 +44,3 @@ "input": "index.js", | ||
} | ||
}, | ||
"testling": { | ||
"files": "tests/run.js", | ||
"browsers": [ | ||
"ie/6..10", | ||
"ff/3", | ||
"ff/4", | ||
"ff/5", | ||
"ff/20", | ||
"ff/25", | ||
"chrome/4", | ||
"chrome/10", | ||
"chrome/20", | ||
"chrome/30", | ||
"chrome/31", | ||
"opera/10", | ||
"opera/11", | ||
"opera/11.6", | ||
"opera/12", | ||
"opera/16", | ||
"safari/4..6", | ||
"android-browser/4.2", | ||
"iphone/6", | ||
"ipad/6" | ||
] | ||
} | ||
} |
@@ -5,8 +5,7 @@ [1]: https://secure.travis-ci.org/litejs/uri-template-lite.png | ||
[4]: https://coveralls.io/r/litejs/uri-template-lite | ||
[7]: https://ci.testling.com/litejs/uri-template-lite.png | ||
[8]: https://ci.testling.com/litejs/uri-template-lite | ||
[RFC 6570]: http://tools.ietf.org/html/rfc6570 | ||
@version 0.1.8 | ||
@date 2014-05-21 | ||
@version 0.1.10 | ||
@date 2014-12-08 | ||
@stability 2 - Unstable | ||
@@ -19,3 +18,3 @@ | ||
URI Template [RFC 6570][rfc-6570] implementation in less than 1kb. | ||
URI Template [RFC 6570][] implementation in JavaScript. | ||
@@ -69,6 +68,6 @@ | ||
This implementation tries to do a best effort template expansion | ||
and leaves erroneous expressions in the returned URI | ||
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 | ||
So for example, the incorrect expression | ||
{unclosed will return {unclosed as output. | ||
@@ -81,7 +80,7 @@ | ||
[![browser support][7]][8] | ||
It should work IE6 and up but automated testing is currently broken. | ||
- For older browsers it requires `browser-upgrade-lite` package | ||
- 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, | ||
- FF3-4 `escape` should be patched to remove non-standard 2nd param, | ||
`browser-upgrade-lite` does that. | ||
@@ -96,3 +95,3 @@ | ||
- [Package on npm](https://npmjs.org/package/uri-template-lite) | ||
- [RFC 6570 - URI Template](http://tools.ietf.org/html/rfc6570) | ||
- URI Template [RFC 6570][] | ||
@@ -99,0 +98,0 @@ |
@@ -6,2 +6,3 @@ | ||
global.escape = mod.escape || global.escape | ||
global.JSON = global.JSON || mod.JSON | ||
@@ -32,7 +33,6 @@ | ||
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") | ||
includeExpandTests(require("./custom-examples.json")) | ||
@@ -44,9 +44,9 @@ | ||
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") | ||
includeMatchTests(require("./custom-examples.json")) | ||
test.done() | ||
@@ -53,0 +53,0 @@ |
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
19
64293
1308
102