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

View File

@@ -49,7 +49,8 @@ ol.source.XYZ = function(xyzOptions) {
} else if (goog.isDef(xyzOptions.urls)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls);
} 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({

View File

@@ -20,28 +20,15 @@ ol.TileUrlFunctionType;
* @return {ol.TileUrlFunctionType} Tile URL function.
*/
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 function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return template.replace('{z}', tileCoord.z)
.replace('{x}', tileCoord.x)
.replace('{y}', tileCoord.y);
}
return ol.TileUrlFunction.createFromTemplates(templates);
} else {
return function(tileCoord) {
if (goog.isNull(tileCoord)) {
return undefined;
} else {
return template.replace('{z}', tileCoord.z)
.replace('{x}', tileCoord.x)
.replace('{y}', tileCoord.y);
}
};
}
};
};

View File

@@ -7,7 +7,7 @@ describe('ol.TileUrlFunction', function() {
it('creates expected URLs', function() {
var template = 'http://tile-{1-3}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).toEqual([
expect(urls).to.eql([
'http://tile-1/{z}/{x}/{y}',
'http://tile-2/{z}/{x}/{y}',
'http://tile-3/{z}/{x}/{y}'
@@ -18,7 +18,7 @@ describe('ol.TileUrlFunction', function() {
it('creates expected URLs', function() {
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).toEqual([
expect(urls).to.eql([
'http://tile-c/{z}/{x}/{y}',
'http://tile-d/{z}/{x}/{y}',
'http://tile-e/{z}/{x}/{y}'
@@ -33,31 +33,23 @@ describe('ol.TileUrlFunction', function() {
expect(tileUrl(new ol.TileCoord(3, 2, 1))).to.eql('3/2/1');
expect(tileUrl(null)).to.be(undefined);
});
describe('with number range', function() {
it('creates expected URL', function() {
var template = 'http://tile-{1-3}/{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-1/3/2/1');
tileCoord.hash = function() { return 2; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1');
tileCoord.hash = function() { return 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('createFromTemplates', function() {
it('creates expected URL', function() {
var templates = [
'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);
tileCoord.hash = function() { return 3; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-1/3/2/1');
tileCoord.hash = function() { return 2; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-3/3/2/1');
tileCoord.hash = function() { return 1; };
expect(tileUrlFunction(tileCoord)).to.eql('http://tile-2/3/2/1');
});
});