Skip to content

Commit

Permalink
Add date in time StopArea and Vehicle class
Browse files Browse the repository at this point in the history
-> arrival_time & arrival_time now deprecated !
  • Loading branch information
Alexis06030631 committed Oct 26, 2023
1 parent 3b4ef1b commit 6caae07
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
17 changes: 5 additions & 12 deletions src/structures/StopArea.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}))
}
})
Expand Down
16 changes: 14 additions & 2 deletions src/structures/StopTime.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {StructuresManager} from "./StructuresManager";
import {hourNativiaToHour} from "../util";
import {dateWithDateAndHour, hourNativiaToHour} from "../util";
import {StopArea} from "./StopArea";
export class StopTime {
/**
Expand All @@ -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
*/
Expand All @@ -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
Expand Down
18 changes: 15 additions & 3 deletions src/structures/Vehicle.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
*/
Expand All @@ -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})

Expand All @@ -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))

}

Expand Down
9 changes: 9 additions & 0 deletions src/util/Converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 6caae07

Please sign in to comment.