-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #3045 parsing of provider for tileProvider
- Loading branch information
Showing
4 changed files
with
114 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2018, GeoSolutions Sas. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
const expect = require('expect'); | ||
|
||
const {getUrls, template} = require('../TileProviderUtils'); | ||
const url = "//stamen-tiles-{s}.a.ssl.fastly.net/{variant}/{z}/{x}/{y}.{ext}"; | ||
const urlReplaced = "//stamen-tiles-a.a.ssl.fastly.net/{variant}/{z}/{x}/{y}.{ext}"; | ||
|
||
const optArray = { | ||
subdomains: ['a', 'b', 'c', 'd'], | ||
variant: 'toner', // this can't be missing | ||
ext: 'png', // this can't be missing | ||
url | ||
}; | ||
describe('Test the TileProviderUtils', () => { | ||
it('test getUrls defaults', () => { | ||
let urls = getUrls(); | ||
expect(urls.length).toBe(3); | ||
urls.forEach((u) => { | ||
expect(u).toBe(""); | ||
}); | ||
}); | ||
it('test getUrls with String as subdomains', () => { | ||
const opt = { | ||
subdomains: 'abcd', | ||
variant: 'toner', // this can't be missing | ||
ext: 'png', // this can't be missing | ||
url | ||
}; | ||
let urls = getUrls(opt); | ||
expect(urls.length).toBe(opt.subdomains.length); | ||
urls.forEach((u, i) => { | ||
expect(u.indexOf("//stamen-tiles-" + opt.subdomains[i])).toBe(0); | ||
}); | ||
}); | ||
it('test getUrls with array of char as subdomains', () => { | ||
|
||
let urls = getUrls(optArray); | ||
expect(urls.length).toBe(optArray.subdomains.length); | ||
urls.forEach((u, i) => { | ||
expect(u.indexOf("//stamen-tiles-" + optArray.subdomains[i])).toBe(0); | ||
}); | ||
}); | ||
|
||
it('test template defaults', () => { | ||
const value = template(); | ||
expect(value).toBe(""); | ||
}); | ||
it('test template ', () => { | ||
const value = template(urlReplaced, optArray); | ||
expect(value).toBe("//stamen-tiles-a.a.ssl.fastly.net/" + optArray.variant + "/{z}/{x}/{y}." + optArray.ext); | ||
// expect().toBe(); | ||
}); | ||
|
||
|
||
}); |