From 2591f61fd34da51b79cd1c18558463d27c95c535 Mon Sep 17 00:00:00 2001 From: Frederic Junod Date: Mon, 5 Sep 2016 11:03:03 +0200 Subject: [PATCH] Allow more than one digit range in ol.TileUrlFunction.expandUrl To be able to have number range like `{9-13}` --- src/ol/tileurlfunction.js | 16 +++++++++++++--- test/spec/ol/tileurlfunction.test.js | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/ol/tileurlfunction.js b/src/ol/tileurlfunction.js index 0989c5eb79..3e5dfdc2ca 100644 --- a/src/ol/tileurlfunction.js +++ b/src/ol/tileurlfunction.js @@ -107,16 +107,26 @@ ol.TileUrlFunction.nullTileUrlFunction = function(tileCoord, pixelRatio, project */ ol.TileUrlFunction.expandUrl = function(url) { var urls = []; - var match = /\{(\d)-(\d)\}/.exec(url) || /\{([a-z])-([a-z])\}/.exec(url); + var match = /\{([a-z])-([a-z])\}/.exec(url); if (match) { + // char range 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; } + match = match = /\{(\d+)-(\d+)\}/.exec(url); + if (match) { + // number range + var stop = parseInt(match[2], 10); + for (var i = parseInt(match[1], 10); i <= stop; i++) { + urls.push(url.replace(match[0], i.toString())); + } + return urls; + } + urls.push(url); return urls; }; diff --git a/test/spec/ol/tileurlfunction.test.js b/test/spec/ol/tileurlfunction.test.js index 698ae0bcf4..fcd920a35b 100644 --- a/test/spec/ol/tileurlfunction.test.js +++ b/test/spec/ol/tileurlfunction.test.js @@ -18,6 +18,15 @@ describe('ol.TileUrlFunction', function() { 'http://tile-3/{z}/{x}/{y}' ]); }); + it('creates expected URLs', function() { + var template = 'http://tile-{9-11}/{z}/{x}/{y}'; + var urls = ol.TileUrlFunction.expandUrl(template); + expect(urls).to.eql([ + 'http://tile-9/{z}/{x}/{y}', + 'http://tile-10/{z}/{x}/{y}', + 'http://tile-11/{z}/{x}/{y}' + ]); + }); }); describe('with character range', function() { it('creates expected URLs', function() { @@ -30,6 +39,15 @@ describe('ol.TileUrlFunction', function() { ]); }); }); + describe('without range', function() { + it('creates expected URLs', function() { + var template = 'http://tiles.example.com/{z}/{x}/{y}'; + var urls = ol.TileUrlFunction.expandUrl(template); + expect(urls).to.eql([ + 'http://tiles.example.com/{z}/{x}/{y}' + ]); + }); + }); }); describe('createFromTemplate', function() {