Remove expandUrl duplicated code from createFromTemplate

and make use of expandUrl function instead.
This commit is contained in:
Bruno Binet
2013-03-13 11:01:39 +01:00
parent 54dfca3419
commit 875ead58ab
3 changed files with 29 additions and 49 deletions
+2 -1
View File
@@ -49,7 +49,8 @@ ol.source.XYZ = function(xyzOptions) {
} else if (goog.isDef(xyzOptions.urls)) { } else if (goog.isDef(xyzOptions.urls)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls); tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls);
} else if (goog.isDef(xyzOptions.url)) { } else if (goog.isDef(xyzOptions.url)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplate(xyzOptions.url); tileUrlFunction = ol.TileUrlFunction.createFromTemplates(
ol.TileUrlFunction.expandUrl(xyzOptions.url));
} }
var tileGrid = new ol.tilegrid.XYZ({ var tileGrid = new ol.tilegrid.XYZ({
-13
View File
@@ -20,18 +20,6 @@ ol.TileUrlFunctionType;
* @return {ol.TileUrlFunctionType} Tile URL function. * @return {ol.TileUrlFunctionType} Tile URL function.
*/ */
ol.TileUrlFunction.createFromTemplate = function(template) { ol.TileUrlFunction.createFromTemplate = function(template) {
var match =
/\{(\d)-(\d)\}/.exec(template) || /\{([a-z])-([a-z])\}/.exec(template);
if (match) {
var templates = [];
var startCharCode = match[1].charCodeAt(0);
var stopCharCode = match[2].charCodeAt(0);
var charCode;
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
templates.push(template.replace(match[0], String.fromCharCode(charCode)));
}
return ol.TileUrlFunction.createFromTemplates(templates);
} else {
return function(tileCoord) { return function(tileCoord) {
if (goog.isNull(tileCoord)) { if (goog.isNull(tileCoord)) {
return undefined; return undefined;
@@ -41,7 +29,6 @@ ol.TileUrlFunction.createFromTemplate = function(template) {
.replace('{y}', tileCoord.y); .replace('{y}', tileCoord.y);
} }
}; };
}
}; };
+11 -19
View File
@@ -7,7 +7,7 @@ describe('ol.TileUrlFunction', function() {
it('creates expected URLs', function() { it('creates expected URLs', function() {
var template = 'http://tile-{1-3}/{z}/{x}/{y}'; var template = 'http://tile-{1-3}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template); var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).toEqual([ expect(urls).to.eql([
'http://tile-1/{z}/{x}/{y}', 'http://tile-1/{z}/{x}/{y}',
'http://tile-2/{z}/{x}/{y}', 'http://tile-2/{z}/{x}/{y}',
'http://tile-3/{z}/{x}/{y}' 'http://tile-3/{z}/{x}/{y}'
@@ -18,7 +18,7 @@ describe('ol.TileUrlFunction', function() {
it('creates expected URLs', function() { it('creates expected URLs', function() {
var template = 'http://tile-{c-e}/{z}/{x}/{y}'; var template = 'http://tile-{c-e}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template); var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).toEqual([ expect(urls).to.eql([
'http://tile-c/{z}/{x}/{y}', 'http://tile-c/{z}/{x}/{y}',
'http://tile-d/{z}/{x}/{y}', 'http://tile-d/{z}/{x}/{y}',
'http://tile-e/{z}/{x}/{y}' 'http://tile-e/{z}/{x}/{y}'
@@ -33,10 +33,16 @@ describe('ol.TileUrlFunction', function() {
expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1'); expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1');
expect(tileUrl(null)).to.be(undefined); expect(tileUrl(null)).to.be(undefined);
}); });
describe('with number range', function() { });
describe('createFromTemplates', function() {
it('creates expected URL', function() { it('creates expected URL', function() {
var template = 'http://tile-{1-3}/{z}/{x}/{y}'; var templates = [
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template); 'http://tile-1/{z}/{x}/{y}',
'http://tile-2/{z}/{x}/{y}',
'http://tile-3/{z}/{x}/{y}'
];
var tileUrlFunction = ol.TileUrlFunction.createFromTemplates(templates);
var tileCoord = new ol.TileCoord(3, 2, 1); var tileCoord = new ol.TileCoord(3, 2, 1);
tileCoord.hash = function() { return 3; }; tileCoord.hash = function() { return 3; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1'); expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1');
@@ -46,20 +52,6 @@ describe('ol.TileUrlFunction', function() {
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1'); expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1');
}); });
}); });
describe('with character range', function() {
it('creates expected URL', function() {
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(template);
var tileCoord = new ol.TileCoord(3, 2, 1);
tileCoord.hash = function() { return 3; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-c/3/2/1');
tileCoord.hash = function() { return 2; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-e/3/2/1');
tileCoord.hash = function() { return 1; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-d/3/2/1');
});
});
});
describe('withTileCoordTransform', function() { describe('withTileCoordTransform', function() {
it('creates expected URL', function() { it('creates expected URL', function() {