From 6caae07d547b6e40599876581b9e7ddbc7cc8cd2 Mon Sep 17 00:00:00 2001 From: Alexis06030631 <61119747+Alexis06030631@users.noreply.github.com> Date: Thu, 26 Oct 2023 20:11:27 +0200 Subject: [PATCH] Add date in time StopArea and Vehicle class -> arrival_time & arrival_time now deprecated ! --- package.json | 2 +- src/structures/StopArea.ts | 17 +++++------------ src/structures/StopTime.ts | 16 ++++++++++++++-- src/structures/Vehicle.ts | 18 +++++++++++++++--- src/util/Converter.ts | 9 +++++++++ 5 files changed, 44 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 91d9ff77..5d7e179b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sncf.js", - "version": "2.4.2", + "version": "2.4.5", "description": "An easy to use javascript module to get all the information in real time about the trains ©SNCF (only in France)", "scripts": { "test": "nodemon test.js --watch test.js --watch dist", diff --git a/src/structures/StopArea.ts b/src/structures/StopArea.ts index 4b410683..7886dea7 100644 --- a/src/structures/StopArea.ts +++ b/src/structures/StopArea.ts @@ -149,21 +149,14 @@ export class StopArea{ if(request.error) { reject(request.error) }else { - resolve(request.vehicle_journeys - // @ts-ignore - .map(vehicle_journey => { - vehicle_journey.disruptions - // @ts-ignore - .forEach(disruption => { - if(request.disruptions - // @ts-ignore - .map(disruption => disruption.id).includes(disruption.id)) { + resolve(request.vehicle_journeys.map((vehicle_journey:any) => { + vehicle_journey.disruptions.forEach((disruption:any) => { + if(request.disruptions.map((disruption:any) => disruption.id).includes(disruption.id)) { vehicle_journey.disruptions[vehicle_journey.disruptions.indexOf(disruption)] = request.disruptions - // @ts-ignore - .find(disruption2 => disruption2.id === disruption.id) + .find((disruption2:any) => disruption2.id === disruption.id) } }) - return new Vehicle(this.client, vehicle_journey) + return new Vehicle(this.client, vehicle_journey, date) })) } }) diff --git a/src/structures/StopTime.ts b/src/structures/StopTime.ts index dac1d11b..8641b760 100644 --- a/src/structures/StopTime.ts +++ b/src/structures/StopTime.ts @@ -1,5 +1,5 @@ import {StructuresManager} from "./StructuresManager"; -import {hourNativiaToHour} from "../util"; +import {dateWithDateAndHour, hourNativiaToHour} from "../util"; import {StopArea} from "./StopArea"; export class StopTime { /** @@ -8,12 +8,22 @@ export class StopTime { headsign: string; /** * Arrival time + * @deprecated Use arrival_date instead */ arrival_time: string; /** * Departure time + * @deprecated Use departure_date instead */ departure_time: string; + /** + * Return the arrival date of the train + */ + arrival_date: Date; + /** + * Return the departure date of the train + */ + departure_date: Date; /** * Return boolean if the stop is skipped */ @@ -35,12 +45,14 @@ export class StopTime { */ client: any; - constructor(Client:any, data:any) { + constructor(Client:any, data:any, date:Date = new Date()) { Object.defineProperty(this, "client", {value: Client}) this.headsign = data.headsign this.arrival_time = hourNativiaToHour(data.arrival_time) this.departure_time = hourNativiaToHour(data.departure_time) + this.arrival_date = dateWithDateAndHour(hourNativiaToHour(data.arrival_time), date) + this.departure_date = dateWithDateAndHour(hourNativiaToHour(data.departure_time), date) this.skipped = data.skipped_stop this.drop_off = data.drop_off_allowed this.pick_up = data.pickup_allowed diff --git a/src/structures/Vehicle.ts b/src/structures/Vehicle.ts index 8be20793..04b3843e 100644 --- a/src/structures/Vehicle.ts +++ b/src/structures/Vehicle.ts @@ -1,5 +1,5 @@ import {StructuresManager} from "./StructuresManager"; -import {hourNativiaToHour} from "../util"; +import {dateWithDateAndHour, hourNativiaToHour} from "../util"; import {Calendar} from "./Calendar"; import {StopTime} from "./StopTime"; import {Disruption} from "./Disruption"; @@ -27,12 +27,22 @@ export class Vehicle { calendar: Calendar[]; /** * Return the departure time of the vehicle + * @deprecated Use arrival_date instead */ departure_time: string; /** * Return the arrival time of the vehicle + * @deprecated Use arrival_date instead */ arrival_time: string; + /** + * Return the departure date of the vehicle + */ + departure_date: Date; + /** + * Return the arrival date of the vehicle + */ + arrival_date: Date; /** * Return the steps of the vehicle */ @@ -46,7 +56,7 @@ export class Vehicle { */ client: any; - constructor(Client:any, data:any) { + constructor(Client:any, data:any, date:Date = new Date()) { Object.defineProperty(this, "client", {value: Client}) Object.defineProperty(this, "data", {value: data}) @@ -57,7 +67,9 @@ export class Vehicle { this.calendar = data.calendars.map((calendar:any) => new Calendar(this.client, calendar)) this.departure_time = this.get_departure_time this.arrival_time = this.get_arrival_time - this.stop_times = data.stop_times.sort((a:any, b:any) => Number(a.departure_time) - Number(b.departure_time)).map((stop_time:any) => new StopTime(this.client, stop_time)) + this.departure_date = dateWithDateAndHour(this.get_departure_time, date) + this.arrival_date = dateWithDateAndHour(this.get_arrival_time, date) + this.stop_times = data.stop_times.sort((a:any, b:any) => Number(a.departure_time) - Number(b.departure_time)).map((stop_time:any) => new StopTime(this.client, stop_time, date)) } diff --git a/src/util/Converter.ts b/src/util/Converter.ts index 66150b61..d7f6eeb8 100644 --- a/src/util/Converter.ts +++ b/src/util/Converter.ts @@ -43,4 +43,13 @@ export function hourNativiaToHour (data:string, PassError?:boolean): string { const minute = data.substring(2, 4); const second = data.substring(4, 6); return `${hour}:${minute}:${second}` +} + +export function dateWithDateAndHour(hour:string, date:Date): Date { + const hourDate:any = { + hours: Number(hour.substring(0, 2)), + minutes: Number(hour.substring(3, 5)), + seconds: Number(hour.substring(6, 8)) + } + return new Date(date.getFullYear(), date.getMonth(), date.getDate(), hourDate.hours, hourDate.minutes, hourDate.seconds) } \ No newline at end of file