Skip to content

Commit

Permalink
Refactor so that regexps aren't parsed on every tile.
Browse files Browse the repository at this point in the history
This parses the tile url template once instead of on every tile, then
just does a string substitution per tile.  It also handles character
rangers specified in reverse (e.g., `{c-a}` is the same as `{a-c}`).
  • Loading branch information
manthey committed Jun 23, 2017
1 parent 2b8d7f7 commit 212b1f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/tileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ module.exports = (function () {
* string. Replaces `{s}`, `{z}`, `{x}`, and `{y}`. These may be any case
* and may be prefixed with `$` (e.g., `${X}` is the same as `{x}`). The
* subdomain can be specifed by a string of characters, listed as a range,
* or as a comma separated list (e.g., `{s:abc}`, `{a-c}`, `{a,b,c}` are
* all equivalent.
* or as a comma-separated list (e.g., `{s:abc}`, `{a-c}`, `{a,b,c}` are
* all equivalent. The comma-separated list can have subdimains that are of
* any length; the string and range both use one-character subdomains.
*
* @param {string} base The tile format string
* @returns {function} A conversion function.
Expand All @@ -41,27 +42,35 @@ module.exports = (function () {
var xPattern = new RegExp(/\$?\{[xX]\}/),
yPattern = new RegExp(/\$?\{[yY]\}/),
zPattern = new RegExp(/\$?\{[zZ]\}/),
sPattern = new RegExp(/\$?\{(s|S|[sS]:.+|[^-{}]-[^-{}]|([^,{}]+,)+[^,{}]+)\}/);
return function (x, y, z, subdomains) {
var url = base
.replace(xPattern, x)
.replace(yPattern, y)
.replace(zPattern, z);
var sMatch = url.match(sPattern);
if (sMatch) {
if (sMatch[2]) {
subdomains = sMatch[1].split(',');
} else if (sMatch[1][1] === ':') {
subdomains = sMatch[1].substr(2).split('');
} else if (sMatch[1][1] === '-') {
subdomains = [];
for (var i = sMatch[1].charCodeAt(0); i <= sMatch[1].charCodeAt(2); i += 1) {
subdomains.push(String.fromCharCode(i));
}
sPattern = new RegExp(/\$?\{(s|S|[sS]:[^{}]+|[^-{}]-[^-{}]|([^,{}]+,)+[^,{}]+)\}/);
var url = base
.replace(sPattern, '{s}')
.replace(xPattern, '{x}')
.replace(yPattern, '{y}')
.replace(zPattern, '{z}');
var urlSubdomains;
var sMatch = base.match(sPattern);
if (sMatch) {
if (sMatch[2]) {
urlSubdomains = sMatch[1].split(',');
} else if (sMatch[1][1] === ':') {
urlSubdomains = sMatch[1].substr(2).split('');
} else if (sMatch[1][1] === '-') {
urlSubdomains = [];
var start = sMatch[1].charCodeAt(0),
end = sMatch[1].charCodeAt(2);
for (var i = Math.min(start, end); i <= Math.max(start, end); i += 1) {
urlSubdomains.push(String.fromCharCode(i));
}
url = url.replace(sPattern, m_getTileSubdomain(x, y, z, subdomains));
}
return url;
}

return function (x, y, z, subdomains) {
return url
.replace('{s}', m_getTileSubdomain(x, y, z, urlSubdomains || subdomains))
.replace('{x}', x)
.replace('{y}', y)
.replace('{z}', z);
};
}

Expand Down
1 change: 1 addition & 0 deletions tests/cases/tileLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,7 @@ describe('geo.tileLayer', function () {
's={s:abc}&x={x}&y={y}&z={z}': ['a', 'b', 'c'],
's={a,b,c}&x={x}&y={y}&z={z}': ['a', 'b', 'c'],
's={a-c}&x={x}&y={y}&z={z}': ['a', 'b', 'c'],
's={c-a}&x={x}&y={y}&z={z}': ['a', 'b', 'c'],
's=${s:1234}&x={x}&y={y}&z={z}': ['1', '2', '3', '4'],
's=${1,2,3,4}&x={x}&y={y}&z={z}': ['1', '2', '3', '4'],
's=${1-4}&x={x}&y={y}&z={z}': ['1', '2', '3', '4'],
Expand Down

0 comments on commit 212b1f8

Please sign in to comment.