Move factorizable code in ol.TileUrlFunction.expandUrl

This commit is contained in:
Bruno Binet
2013-03-12 18:26:43 +01:00
parent c4ab34e4a7
commit 54dfca3419
3 changed files with 48 additions and 15 deletions

View File

@@ -66,23 +66,10 @@ ol.source.WMTS = function(wmtsOptions) {
goog.object.extend(kvpParams, context);
}
// TODO: factorize the code below so that it is usable by all sources
var urls = wmtsOptions.urls;
if (!goog.isDef(urls)) {
urls = [];
var url = wmtsOptions.url;
goog.asserts.assert(goog.isDef(url));
var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
if (match) {
var startCharCode = match[1].charCodeAt(0);
var stopCharCode = match[2].charCodeAt(0);
var charCode;
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
urls.push(url.replace(match[0], String.fromCharCode(charCode)));
}
} else {
urls.push(url);
}
goog.asserts.assert(goog.isDef(wmtsOptions.url));
urls = ol.TileUrlFunction.expandUrl(wmtsOptions.url);
}
/**

View File

@@ -120,3 +120,24 @@ ol.TileUrlFunction.withTileCoordTransform =
}
};
};
/**
* @param {string} url Url.
* @return {Array.<string>} Array of urls.
*/
ol.TileUrlFunction.expandUrl = function(url) {
var urls = [];
var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url);
if (match) {
var startCharCode = match[1].charCodeAt(0);
var stopCharCode = match[2].charCodeAt(0);
var charCode;
for (charCode = startCharCode; charCode <= stopCharCode; ++charCode) {
urls.push(url.replace(match[0], String.fromCharCode(charCode)));
}
} else {
urls.push(url);
}
return urls;
};

View File

@@ -2,6 +2,31 @@ goog.provide('ol.test.TileUrlFunction');
describe('ol.TileUrlFunction', function() {
describe('expandUrl', function() {
describe('with number range', function() {
it('creates expected URLs', function() {
var template = 'http://tile-{1-3}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).toEqual([
'http://tile-1/{z}/{x}/{y}',
'http://tile-2/{z}/{x}/{y}',
'http://tile-3/{z}/{x}/{y}'
]);
});
});
describe('with character range', function() {
it('creates expected URLs', function() {
var template = 'http://tile-{c-e}/{z}/{x}/{y}';
var urls = ol.TileUrlFunction.expandUrl(template);
expect(urls).toEqual([
'http://tile-c/{z}/{x}/{y}',
'http://tile-d/{z}/{x}/{y}',
'http://tile-e/{z}/{x}/{y}'
]);
});
});
});
describe('createFromTemplate', function() {
it('creates expected URL', function() {
var tileUrl = ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}');