gtfs-alerts-utils
Version:
Utilities to work with GTFSrt Alerts
201 lines (177 loc) • 4.54 kB
JavaScript
;
const { TranslatedString, Alert } = require('gtfs-realtime-bindings-transit').transit_realtime;
module.exports.getTextOfLang = (entity, language, key) => {
const texts = entity.alert[key];
if (!texts) {
return '';
}
for (const translation of texts.translation) {
if (translation.language === language) {
return translation.text;
}
}
return '';
};
module.exports.setTextOfLang = (entity, language, text, key) => {
let texts = entity.alert[key];
if (texts) {
for (const translation of texts.translation) {
if (translation.language === language) {
translation.text = text;
return;
}
}
texts.translation.push(TranslatedString.Translation.create({
language,
text,
}));
} else {
texts = TranslatedString.create();
texts.translation.push(TranslatedString.Translation.create({
language,
text,
}));
entity.alert[key] = texts;
}
};
module.exports.deleteTextOfLang = (entity, lang, key) => {
const texts = entity.alert[key];
if (texts) {
texts.translation = texts.translation.filter(translation => translation.language !== lang);
}
};
function getAllLangOfEntity(entity) {
const langs = new Set();
const alert = entity.alert;
if (alert) {
if (alert.headerText) {
for (const translation of alert.headerText.translation) {
langs.add(translation.language);
}
}
if (alert.descriptionText) {
for (const translation of alert.descriptionText.translation) {
langs.add(translation.language);
}
}
if (alert.url) {
for (const translation of alert.url.translation) {
langs.add(translation.language);
}
}
}
return langs;
}
module.exports.getAllLangOfEntity = getAllLangOfEntity;
module.exports.getAllLang = (gtfsrt) => {
let langs = new Set();
for (const entity of gtfsrt.entity) {
const entityLangs = getAllLangOfEntity(entity);
langs = new Set([...langs, ...entityLangs]); // Union of set
}
return langs;
};
module.exports.getLocalizedEffect = (effect, language) => {
if (!language) {
throw new Error('A language must be provided to localized effect.');
}
return localizedEffectByLanguageByEffect[effect][language] || localizedEffectByLanguageByEffect[effect].en;
};
module.exports.getLocalizedCause = (cause, language) => {
if (!language) {
throw new Error('A language must be provided to localized cause.');
}
return localizedCauseByLanguageByEffect[cause][language] || localizedCauseByLanguageByEffect[cause].en;
};
const localizedEffectByLanguageByEffect = {
[Alert.Effect.NO_SERVICE]: {
en: 'No Service',
fr: 'Interruption de service',
},
[Alert.Effect.REDUCED_SERVICE]: {
en: 'Reduced Service',
fr: 'Réduction de service',
},
[Alert.Effect.SIGNIFICANT_DELAYS]: {
en: 'Significant Delays',
fr: 'Retards importants',
},
[Alert.Effect.DETOUR]: {
en: 'Detour',
fr: 'Déviations',
},
[Alert.Effect.ADDITIONAL_SERVICE]: {
en: 'Additional Service',
fr: 'Service additionnel',
},
[Alert.Effect.MODIFIED_SERVICE]: {
en: 'Modified Service',
fr: 'Service modifié',
},
[Alert.Effect.OTHER_EFFECT]: {
en: '',
fr: '',
},
[Alert.Effect.UNKNOWN_EFFECT]: {
en: '',
fr: '',
},
[Alert.Effect.STOP_MOVED]: {
en: 'Stop Moved',
fr: 'Arrêt déplacé',
},
[Alert.Effect.ACCESSIBILITY_ISSUE]: {
en: 'Limited step-free accessibility',
fr: '',
},
};
const localizedCauseByLanguageByEffect = {
[Alert.Cause.UNKNOWN_CAUSE]: {
en: '',
fr: '',
},
[Alert.Cause.OTHER_CAUSE]: {
en: '',
fr: '',
},
[Alert.Cause.TECHNICAL_PROBLEM]: {
en: 'Technical Problem',
fr: 'Problème technique',
},
[Alert.Cause.STRIKE]: {
en: 'Strike',
fr: 'Grève',
},
[Alert.Cause.DEMONSTRATION]: {
en: 'Demonstration',
fr: 'Manifestation',
},
[Alert.Cause.ACCIDENT]: {
en: 'Accident',
fr: 'Accident',
},
[Alert.Cause.HOLIDAY]: {
en: 'Holiday',
fr: 'Jour ferié',
},
[Alert.Cause.WEATHER]: {
en: 'Weather',
fr: 'Problème météo',
},
[Alert.Cause.MAINTENANCE]: {
en: 'Maintenance',
fr: 'Maintenance',
},
[Alert.Cause.CONSTRUCTION]: {
en: 'Construction',
fr: 'Travaux',
},
[Alert.Cause.POLICE_ACTIVITY]: {
en: 'Police Activity',
fr: 'Intervention policière',
},
[Alert.Cause.MEDICAL_EMERGENCY]: {
en: 'Medical Emergency',
fr: 'Urgence médicale',
},
};