Working on staffStation.utils and added tests
Signed-off-by: Fred Boniface <fred@fjla.uk>
This commit is contained in:
@@ -20,6 +20,17 @@ function cleanNrcc(input: string) { // Remove newlines and then <p> tags from in
|
||||
return cleanInput;
|
||||
}
|
||||
|
||||
export function removeNewlineAndPTag(input: string): string {
|
||||
const regex = /[\n\r]|<\/?p[^>]*>/g;
|
||||
return input.replace(regex, (match) => {
|
||||
if (match === "\n" || match === "\r") {
|
||||
return "";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
async function getDomainFromEmail(mail: string) { // Needs testing
|
||||
let split = mail.split('@');
|
||||
return split[1];
|
||||
|
||||
@@ -1,10 +1,32 @@
|
||||
import type { StaffLdb, NrccMessage, TrainServices,
|
||||
ServiceLocation } from '@owlboard/ts-types'
|
||||
ServiceLocation } from '@owlboard/ts-types';
|
||||
|
||||
import { tz } from 'moment-timezone';
|
||||
import { removeNewlineAndPTag } from '../../sanitizer.utils';
|
||||
|
||||
/// I do not yet have a type defined for any of the input object
|
||||
export function transform(input: Object): StaffLdb {
|
||||
export function transform(input: any): StaffLdb | null {
|
||||
const data = input.GetBoardResult
|
||||
let output: StaffLdb
|
||||
return output
|
||||
try {
|
||||
output = {
|
||||
generatedAt: transformDateTime(data?.generatedAt) || new Date(),
|
||||
locationName: data?.locationName || "Not Found",
|
||||
stationManagerCode: data?.stationManagerCode || "UK",
|
||||
nrccMessages: transformNrcc(data?.nrccMessages),
|
||||
trainServices: transformTrainServices(data?.trainServices),
|
||||
busServices: transformTrainServices(data?.busServices),
|
||||
ferryServices: transformTrainServices(data?.ferryServices)
|
||||
}
|
||||
return output
|
||||
} catch (err) {
|
||||
console.log('Unable to parse data, assuming no data: ' + err)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
function transformDateTime(input: string): Date {
|
||||
return new Date(input)
|
||||
}
|
||||
|
||||
function transformNrcc(input: any): NrccMessage[] {
|
||||
@@ -15,15 +37,41 @@ function transformNrcc(input: any): NrccMessage[] {
|
||||
for (const item of input?.message) {
|
||||
let message: NrccMessage = {
|
||||
severity: item?.severity,
|
||||
xhtmlMessage: item?.xhtmlMessage
|
||||
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage)
|
||||
}
|
||||
output.push(message)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
function transformTrainServices(input: Object): TrainServices[] {
|
||||
function transformTrainServices(input: any): TrainServices[] {
|
||||
let services: any = input.service
|
||||
let output: TrainServices[] = []
|
||||
if (!Array.isArray(input.service)) {
|
||||
services = [input.service]
|
||||
}
|
||||
const trainServices: TrainServices = {
|
||||
rid: services?.rid,
|
||||
uid: services?.uid,
|
||||
trainid: services?.trainid,
|
||||
operatorCode: services?.operatorCode || 'UK',
|
||||
platform: services?.platform || '',
|
||||
platformIsHidden: services?.platformIsHidden || '',
|
||||
serviceIsSupressed: services?.serviceIsSupressed || '',
|
||||
origin: transformLocation(services?.origin),
|
||||
destination: transformLocation(services?.destination),
|
||||
isCancelled: services?.isCancelled || '',
|
||||
cancelReason: services?.cancelReason,
|
||||
delayReason: services?.delayReason,
|
||||
arrivalType: services?.arrivalType,
|
||||
departureType: services?.departureType,
|
||||
sta: transformUnspecifiedDateTime(services?.sta),
|
||||
eta: transformUnspecifiedDateTime(services?.eta),
|
||||
ata: transformUnspecifiedDateTime(services?.ata),
|
||||
std: transformUnspecifiedDateTime(services?.std),
|
||||
etd: transformUnspecifiedDateTime(services?.etd),
|
||||
atd: transformUnspecifiedDateTime(services?.atd),
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
@@ -40,4 +88,9 @@ function transformLocation(input: any): ServiceLocation[] {
|
||||
output.push(location)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
function transformUnspecifiedDateTime(input: string): Date {
|
||||
const date = tz(input, "Europe/London");
|
||||
return date.toDate()
|
||||
}
|
||||
Reference in New Issue
Block a user