10
src/utils/newSanitizer.ts
Normal file
10
src/utils/newSanitizer.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
export function removeNewlineAndPTag(input: string): string {
|
||||
const regex = /[\n\r]|<\/?p[^>]*>/g;
|
||||
return input.replace(regex, function(match) {
|
||||
if (match === "\n" || match === "\r") {
|
||||
return "";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -20,18 +20,7 @@ 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, function(match) {
|
||||
if (match === "\n" || match === "\r") {
|
||||
return "";
|
||||
} else {
|
||||
return "";
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function getDomainFromEmail(mail: string) { // Needs testing
|
||||
function getDomainFromEmail(mail: string) { // Needs testing
|
||||
let split = mail.split('@');
|
||||
return split[1];
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ import type { StaffLdb, NrccMessage, TrainServices,
|
||||
ServiceLocation } from '@owlboard/ts-types';
|
||||
|
||||
import { tz } from 'moment-timezone';
|
||||
import { removeNewlineAndPTag } from '../../sanitizer.utils';
|
||||
import { removeNewlineAndPTag } from '../../newSanitizer';
|
||||
|
||||
/// I do not yet have a type defined for any of the input object
|
||||
export function transform(input: any): StaffLdb | null {
|
||||
@@ -14,9 +14,9 @@ export function transform(input: any): StaffLdb | null {
|
||||
locationName: data?.locationName || "Not Found",
|
||||
stationManagerCode: data?.stationManagerCode || "UK",
|
||||
nrccMessages: transformNrcc(data?.nrccMessages) || undefined,
|
||||
trainServices: transformTrainServices(data?.trainServices),
|
||||
busServices: transformTrainServices(data?.busServices),
|
||||
ferryServices: transformTrainServices(data?.ferryServices)
|
||||
trainServices: transformTrainServices(data?.trainServices) || undefined,
|
||||
busServices: transformTrainServices(data?.busServices) || undefined,
|
||||
ferryServices: transformTrainServices(data?.ferryServices) || undefined
|
||||
}
|
||||
return output
|
||||
} catch (err) {
|
||||
@@ -26,15 +26,18 @@ export function transform(input: any): StaffLdb | null {
|
||||
}
|
||||
|
||||
function transformDateTime(input: string): Date {
|
||||
console.debug(`staffStation.transformDateTime Input: ${input}`)
|
||||
return new Date(input)
|
||||
}
|
||||
|
||||
function transformNrcc(input: any): NrccMessage[] {
|
||||
console.debug(`staffStations.transformNrcc: Running`)
|
||||
let output: NrccMessage[] = []
|
||||
let messages = input
|
||||
if (!Array.isArray(input?.message)) {
|
||||
input.message = [input?.message]
|
||||
messages = [input?.message]
|
||||
}
|
||||
for (const item of input?.message) {
|
||||
for (const item of messages) {
|
||||
let message: NrccMessage = {
|
||||
severity: item?.severity,
|
||||
xhtmlMessage: removeNewlineAndPTag(item?.xhtmlMessage)
|
||||
@@ -45,37 +48,45 @@ function transformNrcc(input: any): NrccMessage[] {
|
||||
}
|
||||
|
||||
function transformTrainServices(input: any): TrainServices[] {
|
||||
let services: any = input.service
|
||||
console.debug(`staffStation.transformTrainServices running`)
|
||||
let services: any = input?.service
|
||||
let output: TrainServices[] = []
|
||||
if (services === undefined) {
|
||||
return output
|
||||
}
|
||||
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),
|
||||
for (const service of services) {
|
||||
const trainService: TrainServices = {
|
||||
rid: service?.rid,
|
||||
uid: service?.uid,
|
||||
trainid: service?.trainid,
|
||||
operatorCode: service?.operatorCode || 'UK',
|
||||
platform: service?.platform || '',
|
||||
platformIsHidden: service?.platformIsHidden || '',
|
||||
serviceIsSupressed: service?.serviceIsSupressed || '',
|
||||
origin: transformLocation(service?.origin),
|
||||
destination: transformLocation(service?.destination),
|
||||
isCancelled: service?.isCancelled || '',
|
||||
cancelReason: service?.cancelReason,
|
||||
delayReason: service?.delayReason,
|
||||
arrivalType: service?.arrivalType,
|
||||
departureType: service?.departureType,
|
||||
sta: transformUnspecifiedDateTime(service?.sta),
|
||||
eta: transformUnspecifiedDateTime(service?.eta),
|
||||
ata: transformUnspecifiedDateTime(service?.ata),
|
||||
std: transformUnspecifiedDateTime(service?.std),
|
||||
etd: transformUnspecifiedDateTime(service?.etd),
|
||||
atd: transformUnspecifiedDateTime(service?.atd),
|
||||
}
|
||||
output.push(trainService)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
function transformLocation(input: any): ServiceLocation[] {
|
||||
console.debug(`staffStation.transformLocation running`)
|
||||
let output: ServiceLocation[] = []
|
||||
if (!Array.isArray(input)) {
|
||||
input = [input]
|
||||
@@ -91,6 +102,7 @@ function transformLocation(input: any): ServiceLocation[] {
|
||||
}
|
||||
|
||||
function transformUnspecifiedDateTime(input: string): Date {
|
||||
console.debug(`staffStation.transformUnspecifiedDateTime running`)
|
||||
const date = tz(input, "Europe/London");
|
||||
return date.toDate()
|
||||
}
|
||||
Reference in New Issue
Block a user