ql.io-uri-template
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -141,2 +141,18 @@ module.exports = (function(){ | ||
} | ||
function select(path, obj) { | ||
var splits = !path ? [] : path.split('.'); | ||
var curr = obj; | ||
for(var i = 0; i < splits.length; i++) { | ||
if(curr[splits[i]]) { | ||
curr = curr[splits[i]]; | ||
if(i < splits.length - 1 && curr.constructor === Array && curr.length > 0) { | ||
curr = curr[0]; | ||
} | ||
} | ||
else { | ||
return null; | ||
} | ||
} | ||
return curr; | ||
} | ||
function _append(str, val, encode) { | ||
@@ -154,78 +170,89 @@ var j; | ||
} | ||
function _format(str, values, defaults) { | ||
function _format(str, values, defaults, stream) { | ||
values = values || {}; | ||
defaults = defaults || {}; | ||
var i, j, val, split = false, arr, subset; | ||
for(i = 0; i < o.length; i++) { | ||
if(o[i].constructor === String) { | ||
str = _append(str, o[i], false); | ||
var i, j, val, split = false, arr, subset, key; | ||
stream = stream || o; | ||
var ele; | ||
for(i = 0; i < stream.length; i++) { | ||
ele = stream[i]; | ||
if(ele.constructor === String) { | ||
str = _append(str, ele, false); | ||
} | ||
else { | ||
val = values[o[i].variable] || defaults[o[i].variable]; | ||
if(val) { | ||
if(val.constructor == Array) { | ||
// But is the token multivalued? | ||
if(val.length === 1) { | ||
str = _append(str, val, true); | ||
} | ||
else if(o[i].multivalued) { | ||
if(o[i].max) { | ||
if(val.length <= o[i].max) { | ||
// Append as usual | ||
str = _append(str, val, true); | ||
} | ||
else { | ||
// Split the values into multiple and append each | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
} | ||
if(ele.variable.constructor == Array) { | ||
// Case of nested token - only single valued for now | ||
key = _format('', values, defaults, ele.variable); | ||
val = select(key, values) || select(key, defaults); | ||
str = str + val; | ||
} | ||
else { | ||
val = select(ele.variable, values) || select(ele.variable, defaults); | ||
if(val) { | ||
if(val.constructor == Array) { | ||
// But is the token multivalued? | ||
if(val.length === 1) { | ||
str = _append(str, val, true); | ||
} | ||
else if(ele.multivalued) { | ||
if(ele.max) { | ||
if(val.length <= ele.max) { | ||
// Append as usual | ||
str = _append(str, val, true); | ||
} | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
subset = []; | ||
var start = 0, end = o[i].max; | ||
for(j = 0; j < val.length/o[i].max; j++) { | ||
subset = val.slice(start, end); | ||
arr.push(_append(str, subset, true)); | ||
start += o[i].max; | ||
end += o[i].max; | ||
// Split the values into multiple and append each | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
} | ||
} | ||
str = arr; | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
subset = []; | ||
var start = 0, end = ele.max; | ||
for(j = 0; j < val.length/ele.max; j++) { | ||
subset = val.slice(start, end); | ||
arr.push(_append(str, subset, true)); | ||
start += ele.max; | ||
end += ele.max; | ||
} | ||
str = arr; | ||
} | ||
} | ||
} | ||
} | ||
else { | ||
str = _append(str, val, true); | ||
} | ||
} | ||
else { | ||
// Split if not already split. If already split, error | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
else { | ||
str = _append(str, val, true); | ||
} | ||
} | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
for(j = 0; j < val.length; j++) { | ||
arr.push(_append(str, val[j], true)); | ||
// Split if not already split. If already split, error | ||
if(split) { | ||
throw { | ||
error: 'Template can not have multiple single-valued params with multiple values' | ||
} | ||
} | ||
str = arr; | ||
else { | ||
split = true; | ||
// Split and continue. | ||
arr = []; | ||
for(j = 0; j < val.length; j++) { | ||
arr.push(_append(str, val[j], true)); | ||
} | ||
str = arr; | ||
} | ||
} | ||
} | ||
else { | ||
str = _append(str, val, true); | ||
} | ||
} | ||
else { | ||
str = _append(str, val, true); | ||
else if(ele.required) { | ||
throw { | ||
error: 'Token ' + ele.variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
} | ||
else if(o[i].required) { | ||
throw { | ||
error: 'Token ' + o[i].variable + ' not specified. Processed ' + str | ||
} | ||
} | ||
} | ||
@@ -237,3 +264,2 @@ } | ||
format: function(values, defaults) { | ||
var str = '', i, j, val, split = false, arr; | ||
return _format('', values, defaults); | ||
@@ -279,6 +305,6 @@ }, | ||
if (input.substr(pos).match(/^[^^ "'<>`{|}]/) !== null) { | ||
var result0 = input.charAt(pos); | ||
var result2 = input.charAt(pos); | ||
pos++; | ||
} else { | ||
var result0 = null; | ||
var result2 = null; | ||
if (reportMatchFailures) { | ||
@@ -288,2 +314,12 @@ matchFailed("[^^ \"'<>`{|}]"); | ||
} | ||
if (result2 !== null) { | ||
var result0 = result2; | ||
} else { | ||
var result1 = parse_expression(); | ||
if (result1 !== null) { | ||
var result0 = result1; | ||
} else { | ||
var result0 = null;; | ||
}; | ||
} | ||
@@ -585,5 +621,15 @@ | ||
? (function(l) { | ||
var r = ''; | ||
for(i = 0; i < l.length; i++) { r += l[i]; } | ||
return r; | ||
var o = []; | ||
o.push(l[0]); | ||
var current = 0; | ||
for(var i = 1; i < l.length; i++) { | ||
if(typeof l[i] === 'string' && typeof o[current] === 'string') { | ||
o[current] = o[current] + l[i]; | ||
} | ||
else { | ||
o.push(l[i]); | ||
current++; | ||
} | ||
} | ||
return (o.length === 1) ? o[0] : o; | ||
})(result1) | ||
@@ -785,2 +831,2 @@ : null; | ||
return result; | ||
})(); | ||
})(); |
@@ -5,3 +5,3 @@ { | ||
"name": "ql.io-uri-template", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -17,3 +17,3 @@ /* | ||
"use strict" | ||
'use strict' | ||
@@ -50,3 +50,3 @@ var uriTemplate = require(__dirname + '/../lib/uri-template'), | ||
'required': function(test) { | ||
var u = "http://www.subbu.org?p1={p1}&p2={^p2}&p3={p3}"; | ||
var u = 'http://www.subbu.org?p1={p1}&p2={^p2}&p3={p3}'; | ||
var p = uriTemplate.parse(u); | ||
@@ -130,3 +130,3 @@ var e = [ | ||
'multivalued-split': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={p2}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={p2}'; | ||
var template = uriTemplate.parse(str); | ||
@@ -145,3 +145,3 @@ var uri = template.format({ | ||
'multivalued-encode': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={|p2}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={|p2}'; | ||
var template = uriTemplate.parse(str); | ||
@@ -157,3 +157,3 @@ var uri = template.format({ | ||
'multivalued-multi-multi': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={p2}&p3={p3}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={p2}&p3={p3}'; | ||
var template = uriTemplate.parse(str); | ||
@@ -176,3 +176,3 @@ try { | ||
'multivalued-required': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={^|p2}&p3={^|p3}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={^|p2}&p3={^|p3}'; | ||
var p = uriTemplate.parse(str); | ||
@@ -202,3 +202,3 @@ var e = [ | ||
'multivalued-required-max': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={^|p2}&p3={^|p3}&p4={^20|p4}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={^|p2}&p3={^|p3}&p4={^20|p4}'; | ||
var p = uriTemplate.parse(str); | ||
@@ -235,3 +235,3 @@ var e = [ | ||
'multivalued-split-max': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={p2}&p3={2|p3}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={p2}&p3={2|p3}'; | ||
var template = uriTemplate.parse(str); | ||
@@ -251,3 +251,3 @@ var uri = template.format({ | ||
'multivalued-split-max-more': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={p2}&p3={2|p3}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={p2}&p3={2|p3}'; | ||
var template = uriTemplate.parse(str); | ||
@@ -268,3 +268,3 @@ var uri = template.format({ | ||
'multivalued-split-max-less': function(test) { | ||
var str = "http://www.subbu.org?p1={p1}&p2={p2}&p3={5|p3}"; | ||
var str = 'http://www.subbu.org?p1={p1}&p2={p2}&p3={5|p3}'; | ||
var template = uriTemplate.parse(str); | ||
@@ -281,3 +281,3 @@ var uri = template.format({ | ||
'encode': function(test) { | ||
var str = "http://www.foo.com?p1={p1}&p2={p2}&p3={5|p3}"; | ||
var str = 'http://www.foo.com?p1={p1}&p2={p2}&p3={5|p3}'; | ||
var template = uriTemplate.parse(str); | ||
@@ -294,10 +294,52 @@ var uri = template.format({ | ||
'merge': function(test) { | ||
var str = "http://www.foo.com?p1={p1}&p2={p2}&p3={#p3}"; | ||
var str = 'http://www.foo.com?p1={p1}&p2={p2}&p3={#p3}'; | ||
var template = uriTemplate.parse(str); | ||
test.equals(template.merge(), 'block'); | ||
str = "http://www.foo.com?p1={p1}&p2={p2}&p3={p3}"; | ||
str = 'http://www.foo.com?p1={p1}&p2={p2}&p3={p3}'; | ||
template = uriTemplate.parse(str); | ||
test.ok(template.merge(), 'field'); | ||
test.done(); | ||
}, | ||
'format-nested': function(test) { | ||
var u = 'http://www.subbu.org?p1={a.p1}&p2={^a.p2}&p3={a.p3}'; | ||
var p = uriTemplate.parse(u); | ||
var s = p.format({ | ||
a : { | ||
p1: 'v1', | ||
p2: 'v2', | ||
p3: 'v3' | ||
} | ||
}); | ||
test.equal(s, 'http://www.subbu.org?p1=v1&p2=v2&p3=v3'); | ||
test.done(); | ||
}, | ||
'format-no-values': function(test) { | ||
var u = 'http://www.subbu.org?p1={a.p1}&p2={a.p2}&p3={a.p3}'; | ||
var p = uriTemplate.parse(u); | ||
var s = p.format({}); | ||
test.equal(s, 'http://www.subbu.org?p1=&p2=&p3='); | ||
test.done(); | ||
}, | ||
'nested-tokens': function(test) { | ||
var u = 'http://www.foo.com?p1={p1}&p2={config.{ua}.apikey}'; | ||
var p = uriTemplate.parse(u); | ||
var util = require('util'); | ||
var s = p.format({ | ||
p1: 'v1', | ||
ua: 'safari', | ||
config: { | ||
safari: { | ||
apikey: '1234' | ||
} | ||
} | ||
}, true); | ||
test.equal(s, 'http://www.foo.com?p1=v1&p2=1234'); | ||
test.done(); | ||
} | ||
}; |
Sorry, the diff of this file is not supported yet
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
46466
1045