db-vendo-client/parse/location.js

74 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

import {parse} from 'qs';
2019-02-05 19:07:19 +01:00
2024-12-07 16:16:31 +00:00
const POI = 'POI';
const STATION = 'ST';
const ADDRESS = 'ADR';
2017-11-11 22:35:41 +01:00
const leadingZeros = /^0+/;
const parseLocation = (ctx, l) => {
2024-12-18 00:08:52 +00:00
const {profile, common} = ctx;
2024-12-07 22:46:04 +00:00
if (!l) {
return null;
}
2024-12-21 23:04:05 +00:00
const lid = parse(l.id || l.locationId, {delimiter: '@'});
const res = {
type: 'location',
2024-12-21 23:04:05 +00:00
id: (l.extId || l.evaNr || lid.L || l.evaNumber || l.evaNo || '').replace(leadingZeros, '') || null,
};
2024-12-17 19:41:00 +00:00
const name = l.name || lid.O;
2024-12-21 23:04:05 +00:00
if (l.lat && l.lon || l.coordinates || l.position) {
res.latitude = l.lat || l.coordinates?.latitude || l.position?.latitude;
res.longitude = l.lon || l.coordinates?.longitude || l.position?.longitude;
} else if ('X' in lid && 'Y' in lid) {
res.latitude = lid.Y / 1000000;
res.longitude = lid.X / 1000000;
2017-11-11 22:35:41 +01:00
}
2025-01-03 10:57:24 +00:00
// addresses and pois might also have fake evaNr sometimes!
if (l.type === STATION || l.extId || l.evaNumber || l.evaNo || lid.A == '1') {
2024-12-18 00:08:52 +00:00
let stop = {
type: 'station',
id: res.id,
2024-12-17 19:41:00 +00:00
name: name,
};
2025-01-02 14:00:45 +00:00
if ('number' === typeof res.latitude) {
stop.location = res; // todo: remove `.id`
}
2024-12-07 16:16:31 +00:00
// TODO subStops
2024-12-07 16:16:31 +00:00
if ('products' in l) {
2024-12-21 15:26:49 +00:00
stop.products = profile.parseProducts(ctx, l.products);
}
2024-12-18 00:08:52 +00:00
if (common && common.locations && common.locations[stop.id]) {
delete stop.type;
stop = {
...common.locations[stop.id],
...stop,
};
}
2024-12-08 21:42:57 +00:00
// TODO isMeta
2024-12-18 00:08:52 +00:00
// TODO entrances, lines
return stop;
}
2024-12-17 19:41:00 +00:00
res.name = name;
2025-01-02 14:00:45 +00:00
if (l.type === ADDRESS || lid.A == '2') {
2024-12-17 19:41:00 +00:00
res.address = name;
}
2025-01-02 14:00:45 +00:00
if (l.type === POI || lid.A == '4') {
res.poi = true;
}
return res;
};
2017-11-11 22:35:41 +01:00
2022-05-07 16:17:37 +02:00
export {
2024-12-07 16:16:31 +00:00
parseLocation,
};