@iwpnd/valhalla-ts
Version:
A nodejs client and helper utilities for valhalla routing engine
195 lines • 5.37 kB
JavaScript
"use strict";
/* eslint-disable @typescript-eslint/no-unnecessary-type-parameters */
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.randomTrip = exports.randomSummary = exports.randomIsochrone = exports.randomIsochroneProperties = exports.randomPolygon = exports.randomRequestLocation = exports.randomPosition = exports.randomStatus = void 0;
const chance_1 = __importDefault(require("chance"));
const chance = new chance_1.default();
/**
*
* Create a status
* @params data? { Partial<StatusResponse>}
*
* returns {@link StatusResponse}
*/
const randomStatus = (extended = false) => ({
version: chance.string(),
tileset_last_modfied: chance.timestamp(),
...(extended && {
has_admin: chance.bool(),
has_tiles: chance.bool(),
has_timezones: chance.bool(),
has_live_traffic: chance.bool(),
bbox: [
chance.floating({ min: 0, max: 90 }),
chance.floating({ min: 0, max: 45 }),
chance.floating({ min: 90, max: 180 }),
chance.floating({ min: 45, max: 90 }),
],
}),
});
exports.randomStatus = randomStatus;
/**
*
* Create a random position of latitude and longitude
*
* @remarks
* Position is created between
* in a bounding box of[-180,-90,180,90]
*
* @params lat? - latitude
* @params lon? - longitude
*
* returns {@link Position}
*/
const randomPosition = (lat, lon) => ({
lat: lat ?? chance.floating({ min: -90, max: 90 }),
lon: lon ?? chance.floating({ min: -180, max: 180 }),
});
exports.randomPosition = randomPosition;
/**
*
* Create a random location
*
* @remarks
* Location is created between
* in a bounding box of[-180,-90,180,90]
*
* @params data? { Partial<RequestLocation>}
*
* returns {@link RequestPosition}
*/
const randomRequestLocation = (data) => ({
...(0, exports.randomPosition)(),
...data,
});
exports.randomRequestLocation = randomRequestLocation;
/**
*
* Create a random polygon
*
* @remarks
* polygon is created between
* in a bounding box of [13.1632, 52.4099, 13.6402, 52.6353]
*
* @params bbox {BBox}
*
* returns {@link Polygon}
*/
const randomPolygon = (bbox = [13.1632, 52.4099, 13.6402, 52.6353]) => {
const N = 5;
const [minLng, minLat, maxLng, maxLat] = bbox;
const points = [...Array(N).keys()].map(() => [
chance.floating({ min: minLng, max: maxLng }),
chance.floating({ min: minLat, max: maxLat }),
]);
let sw = [];
let ne = [];
points.forEach(([lng, lat]) => {
if (!sw.length) {
sw = [lng, lat];
}
if (!ne.length) {
ne = [lng, lat];
}
if (sw[0] > lng) {
sw[0] = lng;
}
if (sw[1] > lat) {
sw[1] = lat;
}
if (ne[0] < lng) {
ne[0] = lng;
}
if (ne[1] < lat) {
ne[1] = lat;
}
});
return {
type: 'Polygon',
coordinates: [[sw, [ne[0], sw[1]], ne, [sw[0], ne[1]], sw]],
};
};
exports.randomPolygon = randomPolygon;
/**
*
* Create random isochrone properties
*
* @return {@link IsochroneResponseProperties}
*/
const randomIsochroneProperties = () => ({
fill: chance.color(),
fillColor: chance.color(),
color: chance.color(),
fillOpacity: chance.floating({ min: 0, max: 1 }),
'fill-opacity': chance.floating({ min: 0, max: 1 }),
opacity: chance.floating({ min: 0, max: 1 }),
contour: chance.integer({ min: 5, max: 20 }),
metric: 'time',
});
exports.randomIsochroneProperties = randomIsochroneProperties;
/**
*
* Create random isochrone
*
* @params {@link Partial<@link Isochrone>}
*
* @return {@link Isochrone}
*/
const randomIsochrone = (data) => ({
type: 'Feature',
geometry: (0, exports.randomPolygon)(),
properties: (0, exports.randomIsochroneProperties)(),
...data,
});
exports.randomIsochrone = randomIsochrone;
/**
*
* Create random route summary
*
* @params {@link Partial<@link Summary>}
*
* @return {@link summary}
*/
const randomSummary = (data) => ({
has_time_restrictions: false,
min_lat: chance.floating({ min: -90, max: 90 }),
min_lon: chance.floating({ min: -180, max: 180 }),
max_lat: chance.floating({ min: -90, max: 90 }),
max_lon: chance.floating({ min: -180, max: 180 }),
time: chance.floating({ min: 100.0, max: 1000.0 }),
length: chance.floating({ min: 100.0, max: 1000.0 }),
cost: chance.floating({ min: 100.0, max: 1000.0 }),
...data,
});
exports.randomSummary = randomSummary;
/**
*
* Create random route
*
* @params {@link Partial<@link Trip>}
*
* @return {@link Trip}
*/
const randomTrip = (data) => ({
locations: [
{ ...(0, exports.randomPosition)(), original_index: 0 },
{ ...(0, exports.randomPosition)(), original_index: 1 },
],
legs: [
{
shape: 'adi`cBk|inXhXjL`QnHpMfFjm@hV',
summary: (0, exports.randomSummary)(),
},
],
summary: (0, exports.randomSummary)(),
status_message: chance.string(),
status: chance.integer(),
units: 'kilometers',
language: 'en-US',
...data,
});
exports.randomTrip = randomTrip;
//# sourceMappingURL=index.js.map