Replace multiple placeholder occurrences in templates

This commit is contained in:
Andreas Hocevar
2014-08-07 10:51:41 +02:00
parent 2230349b52
commit 08039e9894
+8 -4
View File
@@ -31,6 +31,10 @@ ol.TileCoordTransformType;
* @return {ol.TileUrlFunctionType} Tile URL function. * @return {ol.TileUrlFunctionType} Tile URL function.
*/ */
ol.TileUrlFunction.createFromTemplate = function(template) { ol.TileUrlFunction.createFromTemplate = function(template) {
var zRegEx = /\{z\}/g;
var xRegEx = /\{x\}/g;
var yRegEx = /\{y\}/g;
var dashYRegEx = /\{-y\}/g;
return ( return (
/** /**
* @param {ol.TileCoord} tileCoord Tile Coordinate. * @param {ol.TileCoord} tileCoord Tile Coordinate.
@@ -42,10 +46,10 @@ ol.TileUrlFunction.createFromTemplate = function(template) {
if (goog.isNull(tileCoord)) { if (goog.isNull(tileCoord)) {
return undefined; return undefined;
} else { } else {
return template.replace('{z}', tileCoord.z.toString()) return template.replace(zRegEx, tileCoord.z.toString())
.replace('{x}', tileCoord.x.toString()) .replace(xRegEx, tileCoord.x.toString())
.replace('{y}', tileCoord.y.toString()) .replace(yRegEx, tileCoord.y.toString())
.replace('{-y}', function() { .replace(dashYRegEx, function() {
var y = (1 << tileCoord.z) - tileCoord.y - 1; var y = (1 << tileCoord.z) - tileCoord.y - 1;
return y.toString(); return y.toString();
}); });