slippy-tile
Advanced tools
Comparing version 1.2.3 to 1.2.4
# Changelog | ||
## 1.2.2 - 2016-10-21 | ||
## 1.2.4 - 2016-10-21 | ||
- Add `{s}` to switch cases as default `['a', 'b', 'c']` | ||
- Removed `js-yaml` dependency | ||
@@ -6,0 +7,0 @@ - Removed all `request` related dependencies |
34
index.js
"use strict"; | ||
const mercator = require('global-mercator'); | ||
const path = require('path'); | ||
const providers = require(path.join(__dirname, 'providers')); | ||
const providerLookup = require(path.join(__dirname, 'providers')); | ||
/** | ||
@@ -9,12 +9,14 @@ * Substitutes the given tile information [x,y,zoom] to the URL tile scheme. | ||
* @param {Tile} tile Tile [x, y, zoom] | ||
* @param {string} scheme Tile scheme URL or provider unique key | ||
* @param {string} url URL Tile scheme or provider unique key | ||
* @returns {string} | ||
* @example | ||
* import slippyTile from 'slippyTile' | ||
* slippyTile([10, 15, 8], 'osm') | ||
* //='https://c.tile.openstreetmap.org/8/10/15.png' | ||
*/ | ||
function slippyTile(tile, scheme) { | ||
function slippyTile(tile, url) { | ||
const [x, y, zoom] = tile; | ||
let url = providerLookup(scheme); | ||
const providerURL = provider(url); | ||
if (providerURL) { | ||
url = providerURL; | ||
} | ||
url = url.replace(/{(zoom|z)}/, String(zoom)); | ||
@@ -59,3 +61,2 @@ url = url.replace(/{x}/, String(x)); | ||
* @example | ||
* import { parseSwitch } from 'slippyTile' | ||
* parseSwitch('http://tile-{switch:a,b,c}.openstreetmap.fr/hot/{zoom}/{x}/{y}.png') | ||
@@ -65,2 +66,8 @@ * //='http://tile-b.openstreetmap.fr/hot/{zoom}/{x}/{y}.png' | ||
function parseSwitch(url) { | ||
// Default simple switch | ||
if (url.match(/{s}/i)) { | ||
const random = String(sample(['a', 'b', 'c'])); | ||
return url.replace(/{s}/, random); | ||
} | ||
// Custom switch | ||
const pattern = /{switch:([a-z,\d]*)}/i; | ||
@@ -81,16 +88,16 @@ const found = url.match(pattern); | ||
* @example | ||
* import { provider } from 'slippyTile' | ||
* provider('bing-imagery') | ||
* //='http://ecn.t{switch:0,1,2,3}.tiles.virtualearth.net/tiles/a{quadkey}.jpeg?g=5250' | ||
* provider('osm') | ||
* //='https://{s}.tile.openstreetmap.org/{zoom}/{x}/{y}.png' | ||
*/ | ||
function providerLookup(provider) { | ||
function provider(provider) { | ||
if (provider) { | ||
const key = provider.toLowerCase().trim(); | ||
if (providers[key] !== undefined) { | ||
return providers[key].url; | ||
if (providerLookup[key] !== undefined) { | ||
return providerLookup[key].url; | ||
} | ||
} | ||
return provider; | ||
} | ||
exports.providerLookup = providerLookup; | ||
exports.provider = provider; | ||
/** | ||
@@ -102,5 +109,4 @@ * Sample an item from a given list | ||
* @example | ||
* import { sample } from 'slippyTile' | ||
* sample(['a', 'b', 'c']) | ||
* //='a' | ||
* //='b' | ||
*/ | ||
@@ -107,0 +113,0 @@ function sample(collection) { |
35
index.ts
@@ -17,4 +17,9 @@ import * as mercator from 'global-mercator' | ||
const providers: Providers = require(path.join(__dirname, 'providers')) | ||
interface Options { | ||
url?: string | ||
provider?: string | ||
} | ||
const providerLookup: Providers = require(path.join(__dirname, 'providers')) | ||
/** | ||
@@ -29,12 +34,12 @@ * Tile [x, y, zoom] | ||
* @param {Tile} tile Tile [x, y, zoom] | ||
* @param {string} scheme Tile scheme URL or provider unique key | ||
* @param {string} url URL Tile scheme or provider unique key | ||
* @returns {string} | ||
* @example | ||
* import slippyTile from 'slippyTile' | ||
* slippyTile([10, 15, 8], 'osm') | ||
* //='https://c.tile.openstreetmap.org/8/10/15.png' | ||
*/ | ||
export function slippyTile(tile: Tile, scheme: string) { | ||
export function slippyTile(tile: Tile, url: string) { | ||
const [x, y, zoom] = tile | ||
let url = providerLookup(scheme) | ||
const providerURL = provider(url) | ||
if (providerURL) { url = providerURL } | ||
url = url.replace(/{(zoom|z)}/, String(zoom)) | ||
@@ -74,3 +79,2 @@ url = url.replace(/{x}/, String(x)) | ||
* @example | ||
* import { parseSwitch } from 'slippyTile' | ||
* parseSwitch('http://tile-{switch:a,b,c}.openstreetmap.fr/hot/{zoom}/{x}/{y}.png') | ||
@@ -80,2 +84,8 @@ * //='http://tile-b.openstreetmap.fr/hot/{zoom}/{x}/{y}.png' | ||
export function parseSwitch (url: string) { | ||
// Default simple switch | ||
if (url.match(/{s}/i)) { | ||
const random = String(sample(['a', 'b', 'c'])) | ||
return url.replace(/{s}/, random) | ||
} | ||
// Custom switch | ||
const pattern = /{switch:([a-z,\d]*)}/i | ||
@@ -96,14 +106,14 @@ const found = url.match(pattern) | ||
* @example | ||
* import { provider } from 'slippyTile' | ||
* provider('bing-imagery') | ||
* //='http://ecn.t{switch:0,1,2,3}.tiles.virtualearth.net/tiles/a{quadkey}.jpeg?g=5250' | ||
* provider('osm') | ||
* //='https://{s}.tile.openstreetmap.org/{zoom}/{x}/{y}.png' | ||
*/ | ||
export function providerLookup(provider: string) { | ||
export function provider(provider: string) { | ||
if (provider) { | ||
const key = provider.toLowerCase().trim() | ||
if (providers[key] !== undefined) { | ||
return providers[key].url | ||
if (providerLookup[key] !== undefined) { | ||
return providerLookup[key].url | ||
} | ||
} | ||
return provider | ||
} | ||
@@ -117,5 +127,4 @@ | ||
* @example | ||
* import { sample } from 'slippyTile' | ||
* sample(['a', 'b', 'c']) | ||
* //='a' | ||
* //='b' | ||
*/ | ||
@@ -122,0 +131,0 @@ export function sample(collection: Array<string | number>): string | number { |
{ | ||
"name": "slippy-tile", | ||
"version": "1.2.3", | ||
"version": "1.2.4", | ||
"description": "Helps convert Slippy Map url tile schemas", | ||
@@ -8,3 +8,2 @@ "main": "index.js", | ||
"scripts": { | ||
"prepublish": "npm run docs", | ||
"pretest": "tsc && js-yaml providers.yml > providers.json", | ||
@@ -11,0 +10,0 @@ "test": "npm run lint && nyc ava", |
@@ -12,3 +12,3 @@ { | ||
], | ||
"url": "https://{switch:a,b,c}.tiles.mapbox.com/v4/digitalglobe.nal0mpda/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZGlnaXRhbGdsb2JlIiwiYSI6ImNpbjZpOXFxNzAwZjF0cGx5Ymw5dTQ2djgifQ.YMSAznlaa8WNf5Ypy2hmhA", | ||
"url": "https://{s}.tiles.mapbox.com/v4/digitalglobe.nal0mpda/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZGlnaXRhbGdsb2JlIiwiYSI6ImNpbjZpOXFxNzAwZjF0cGx5Ymw5dTQ2djgifQ.YMSAznlaa8WNf5Ypy2hmhA", | ||
"description": "Tiles from DigitalGlobe", | ||
@@ -26,3 +26,3 @@ "attribution": "© DigitalGlobe, © OpenStreetMap, © Mapbox", | ||
], | ||
"url": "https://{switch:a,b,c}.tiles.mapbox.com/v4/digitalglobe.nal0g75k/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZGlnaXRhbGdsb2JlIiwiYSI6ImNpbjZpOXFxNzAwZjF0cGx5Ymw5dTQ2djgifQ.YMSAznlaa8WNf5Ypy2hmhA", | ||
"url": "https://{s}.tiles.mapbox.com/v4/digitalglobe.nal0g75k/{z}/{x}/{y}.png?access_token=pk.eyJ1IjoiZGlnaXRhbGdsb2JlIiwiYSI6ImNpbjZpOXFxNzAwZjF0cGx5Ymw5dTQ2djgifQ.YMSAznlaa8WNf5Ypy2hmhA", | ||
"description": "Tiles from DigitalGlobe", | ||
@@ -64,3 +64,3 @@ "attribution": "© DigitalGlobe", | ||
], | ||
"url": "https://{switch:a,b,c}.tile.openstreetmap.org/{zoom}/{x}/{y}.png", | ||
"url": "https://{s}.tile.openstreetmap.org/{zoom}/{x}/{y}.png", | ||
"description": "Tiles from OSM", | ||
@@ -77,3 +77,3 @@ "attribution": "Map data © OSM", | ||
], | ||
"url": "https://{switch:a,b,c}.tile.thunderforest.com/cycle/{zoom}/{x}/{y}.png", | ||
"url": "https://{s}.tile.thunderforest.com/cycle/{zoom}/{x}/{y}.png", | ||
"description": "Tiles from OSM", | ||
@@ -91,3 +91,3 @@ "attribution": "Map data © OSM", | ||
], | ||
"url": "https://tile-{switch:a,b,c}.openstreetmap.fr/hot/{zoom}/{x}/{y}.png", | ||
"url": "https://tile-{s}.openstreetmap.fr/hot/{zoom}/{x}/{y}.png", | ||
"description": "Tiles from OSM", | ||
@@ -104,3 +104,3 @@ "attribution": "Map data © OSM", | ||
], | ||
"url": "https://{switch:a,b,c}.tile.thunderforest.com/transport/{zoom}/{x}/{y}.png", | ||
"url": "https://{s}.tile.thunderforest.com/transport/{zoom}/{x}/{y}.png", | ||
"description": "Tiles from OSM", | ||
@@ -141,3 +141,3 @@ "attribution": "Map data © OSM", | ||
], | ||
"url": "https://{switch:a,b,c,d}.tiles.mapbox.com/v4/mapbox.streets/{zoom}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6IlpIdEpjOHcifQ.Cldl4wq_T5KOgxhLvbjE-w", | ||
"url": "https://{s,d}.tiles.mapbox.com/v4/mapbox.streets/{zoom}/{x}/{y}.png?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6IlpIdEpjOHcifQ.Cldl4wq_T5KOgxhLvbjE-w", | ||
"description": "Tiles from OSM", | ||
@@ -144,0 +144,0 @@ "attribution": "Map data © OSM", |
@@ -79,3 +79,3 @@ [![Build Status](https://travis-ci.org/DenisCarriere/slippy-tile.svg?branch=master)](https://travis-ci.org/DenisCarriere/slippy-tile) | ||
- `tile` **[Tile](https://en.wikipedia.org/wiki/Tiled_web_map)** Tile [x, y, zoom] | ||
- `scheme` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** Tile scheme URL or provider unique key | ||
- `url` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** URL Tile scheme or provider unique key | ||
@@ -85,3 +85,2 @@ **Examples** | ||
```javascript | ||
import slippyTile from 'slippyTile' | ||
slippyTile([10, 15, 8], 'osm') | ||
@@ -104,3 +103,2 @@ //='https://c.tile.openstreetmap.org/8/10/15.png' | ||
```javascript | ||
import { parseSwitch } from 'slippyTile' | ||
parseSwitch('http://tile-{switch:a,b,c}.openstreetmap.fr/hot/{zoom}/{x}/{y}.png') | ||
@@ -112,3 +110,3 @@ //='http://tile-b.openstreetmap.fr/hot/{zoom}/{x}/{y}.png' | ||
# providerLookup | ||
# provider | ||
@@ -124,5 +122,6 @@ Provider Lookup | ||
```javascript | ||
import { provider } from 'slippyTile' | ||
provider('bing-imagery') | ||
//='http://ecn.t{switch:0,1,2,3}.tiles.virtualearth.net/tiles/a{quadkey}.jpeg?g=5250' | ||
provider('osm') | ||
//='https://{s}.tile.openstreetmap.org/{zoom}/{x}/{y}.png' | ||
``` | ||
@@ -143,5 +142,4 @@ | ||
```javascript | ||
import { sample } from 'slippyTile' | ||
sample(['a', 'b', 'c']) | ||
//='a' | ||
//='b' | ||
``` | ||
@@ -152,4 +150,5 @@ | ||
## 1.2.2 - 2016-10-21 | ||
## 1.2.4 - 2016-10-21 | ||
- Add `{s}` to switch cases as default `['a', 'b', 'c']` | ||
- Removed `js-yaml` dependency | ||
@@ -156,0 +155,0 @@ - Removed all `request` related dependencies |
@@ -15,2 +15,7 @@ "use strict"; | ||
}); | ||
ava_1.default('providers complex switch', t => { | ||
const tile = [10, 15, 8]; | ||
const url = index_1.default(tile, 'bing-imagery'); | ||
t.true(!!url); | ||
}); | ||
//# sourceMappingURL=test.js.map |
@@ -17,1 +17,7 @@ import test from 'ava' | ||
}) | ||
test('providers complex switch', t => { | ||
const tile: Tile = [10, 15, 8] | ||
const url = slippyTile(tile, 'bing-imagery') | ||
t.true(!!url) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
201692
532
163