-
Notifications
You must be signed in to change notification settings - Fork 383
/
TileProviderUtils.js
43 lines (37 loc) · 1.14 KB
/
TileProviderUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const { isArray } = require('lodash');
function template(str = "", data = {}) {
return str.replace(/(?!(\{?[zyx]?\}))\{*([\w_]+)*\}/g, function() {
let st = arguments[0];
let key = arguments[1] ? arguments[1] : arguments[2];
let value = data[key];
if (value === undefined) {
throw new Error('No value provided for variable ' + st);
} else if (typeof value === 'function') {
value = value(data);
}
return value;
});
}
/**
* it will replace a wildcard with each subdomain
* @param opt options to use
* @return array of urls
*/
function getUrls(opt = {}) {
let url = opt.url || "";
let subdomains = opt.subdomains || "";
if (subdomains) {
// subdomains can be a string or an array of chars
if (typeof subdomains === "string") {
subdomains = subdomains.split("");
}
if (isArray(subdomains)) {
return subdomains.map( c => template(url.replace("{s}", c), opt));
}
}
return ['a', 'b', 'c'].map( c => template(url.replace("{s}", c), opt));
}
module.exports = {
getUrls,
template
};