Alien-Web-HalBrowser
view release on metacpan or search on metacpan
share/vendor/js/uritemplates.js view on Meta::CPAN
if (!key || key.length === 0) {
key = name;
}
return key + (key.length > 0 ? "=" : "") + val;
}
function addLabeled(name, key, val, noName) {
noName = noName || false;
if (noName) { name = ""; }
if (!key || key.length === 0) {
key = name;
}
return key + (key.length > 0 && val ? "=" : "") + val;
}
var simpleConf = {
prefix : "", joiner : ",", encode : encodeNormal, builder : addUnNamed
};
var reservedConf = {
prefix : "", joiner : ",", encode : encodeReserved, builder : addUnNamed
};
var fragmentConf = {
prefix : "#", joiner : ",", encode : encodeReserved, builder : addUnNamed
};
var pathParamConf = {
prefix : ";", joiner : ";", encode : encodeNormal, builder : addLabeled
};
var formParamConf = {
prefix : "?", joiner : "&", encode : encodeNormal, builder : addNamed
};
var formContinueConf = {
prefix : "&", joiner : "&", encode : encodeNormal, builder : addNamed
};
var pathHierarchyConf = {
prefix : "/", joiner : "/", encode : encodeNormal, builder : addUnNamed
};
var labelConf = {
prefix : ".", joiner : ".", encode : encodeNormal, builder : addUnNamed
};
function Expression(conf, vars ) {
extend(this, conf);
this.vars = vars;
}
Expression.build = function(ops, vars) {
var conf;
switch(ops) {
case '' : conf = simpleConf; break;
case '+' : conf = reservedConf; break;
case '#' : conf = fragmentConf; break;
case ';' : conf = pathParamConf; break;
case '?' : conf = formParamConf; break;
case '&' : conf = formContinueConf; break;
case '/' : conf = pathHierarchyConf; break;
case '.' : conf = labelConf; break;
default : throw "Unexpected operator: '"+ops+"'";
}
return new Expression(conf, vars);
};
Expression.prototype.expand = function(context) {
var joiner = this.prefix;
var nextjoiner = this.joiner;
var buildSegment = this.builder;
var res = "";
var i = 0, cnt = this.vars.length;
for (i = 0 ; i< cnt; i++) {
var varspec = this.vars[i];
varspec.addValues(context, this.encode, function(key, val, noName) {
var segm = buildSegment(varspec.name, key, val, noName);
if (segm !== null && segm !== undefined) {
res += joiner + segm;
joiner = nextjoiner;
}
});
}
return res;
};
var UNBOUND = {};
/**
* Helper class to help grow a string of (possibly encoded) parts until limit is reached
*/
function Buffer(limit) {
this.str = "";
if (limit === UNBOUND) {
this.appender = Buffer.UnboundAppend;
} else {
this.len = 0;
this.limit = limit;
this.appender = Buffer.BoundAppend;
}
}
Buffer.prototype.append = function(part, encoder) {
return this.appender(this, part, encoder);
};
Buffer.UnboundAppend = function(me, part, encoder) {
part = encoder ? encoder(part) : part;
me.str += part;
return me;
};
Buffer.BoundAppend = function(me, part, encoder) {
part = part.substring(0, me.limit - me.len);
me.len += part.length;
part = encoder ? encoder(part) : part;
me.str += part;
return me;
};
share/vendor/js/uritemplates.js view on Meta::CPAN
}
VarSpec.build = function(name, expl, part, nums) {
var valueHandler, valueModifier;
if (!!expl) { //interprete as boolean
valueHandler = explodeValueHandler;
} else {
valueHandler = simpleValueHandler;
}
if (!part) {
nums = UNBOUND;
}
return new VarSpec(name, valueHandler, nums);
};
VarSpec.prototype.addValues = function(context, encoder, adder) {
var val = context.get(this.name);
var valprops = valueProperties(val);
if (valprops.isUndef) { return; } // ignore empty values
this.valueHandler(this, val, valprops, encoder, adder);
};
//----------------------------------------------parsing logic
// How each varspec should look like
var VARSPEC_RE=/([^*:]*)((\*)|(:)([0-9]+))?/;
var match2varspec = function(m) {
var name = m[1];
var expl = m[3];
var part = m[4];
var nums = parseInt(m[5], 10);
return VarSpec.build(name, expl, part, nums);
};
// Splitting varspecs in list with:
var LISTSEP=",";
// How each template should look like
var TEMPL_RE=/(\{([+#.;?&\/])?(([^.*:,{}|@!=$()][^*:,{}$()]*)(\*|:([0-9]+))?(,([^.*:,{}][^*:,{}]*)(\*|:([0-9]+))?)*)\})/g;
// Note: reserved operators: |!@ are left out of the regexp in order to make those templates degrade into literals
// (as expected by the spec - see tests.html "reserved operators")
var match2expression = function(m) {
var expr = m[0];
var ops = m[2] || '';
var vars = m[3].split(LISTSEP);
var i = 0, len = vars.length;
for (i = 0; i<len; i++) {
var match;
if ( (match = vars[i].match(VARSPEC_RE)) === null) {
throw "unexpected parse error in varspec: " + vars[i];
}
vars[i] = match2varspec(match);
}
return Expression.build(ops, vars);
};
var pushLiteralSubstr = function(set, src, from, to) {
if (from < to) {
var literal = src.substr(from, to - from);
set.push(new Literal(literal));
}
};
var parse = function(str) {
var lastpos = 0;
var comp = [];
var match;
var pattern = TEMPL_RE;
pattern.lastIndex = 0; // just to be sure
while ((match = pattern.exec(str)) !== null) {
var newpos = match.index;
pushLiteralSubstr(comp, str, lastpos, newpos);
comp.push(match2expression(match));
lastpos = pattern.lastIndex;
}
pushLiteralSubstr(comp, str, lastpos, str.length);
return new UriTemplate(comp);
};
//-------------------------------------------comments and ideas
//TODO: consider building cache of previously parsed uris or even parsed expressions?
return parse;
}());
( run in 0.471 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )