From d6720095cc88d4a34e36e8521b4fc43bb4fd6461 Mon Sep 17 00:00:00 2001 From: Netfloex <38650595+Netfloex@users.noreply.github.com> Date: Wed, 7 Sep 2022 22:41:31 +0200 Subject: [PATCH] Add some more usage --- src/breng/types/planner.d.ts | 4 +- src/index.ts | 87 +++++++++++++++++++++++++++++++++--- 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/src/breng/types/planner.d.ts b/src/breng/types/planner.d.ts index 2508c17..db606b2 100644 --- a/src/breng/types/planner.d.ts +++ b/src/breng/types/planner.d.ts @@ -104,7 +104,7 @@ interface Leg { duration: number endTime: number fare?: LegFare - from: From2 + from: LegStartOrEndpoint generalizedCost: number interlineWithPreviousLeg?: boolean legGeometry: LegGeometry @@ -115,7 +115,7 @@ interface Leg { route: string startTime: number steps: Step[] - to: To + to: LegStartOrEndpoint transitLeg: boolean walkingBike?: boolean agencyId?: string diff --git a/src/index.ts b/src/index.ts index fb15769..b791aae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,18 @@ import { planner } from "@breng/endpoints/planner" import { search } from "@breng/endpoints/search" import chalk from "chalk" import { HTTPError } from "got-cjs" -import { DateTime } from "luxon" +import { DateTime, Duration } from "luxon" + +const concatString = (...array: Array): string => + array.filter(Boolean).join(" ").replace(/\n\s*/g, "\n") + +const cD: chalk.ChalkFunction = (...optionalText: unknown[]) => { + const text = optionalText.filter(Boolean) + + if (text.length) return chalk.dim(...text) + + return "" +} const main = async (): Promise => { const amsterdam = await search("Amsterdam") @@ -20,14 +31,78 @@ const main = async (): Promise => { chalk`{underline ${amsterdam.result.transit[0].name} {bold ->} ${rotterdam.result.transit[0].name}}`, ) + // console.log(inspect(plan.itineraries[0], true, 10, true)) plan.itineraries.forEach((itinerary) => { + const formatTime = (ms: number | undefined): string => + ms + ? DateTime.fromMillis(ms).toLocaleString( + DateTime.TIME_24_SIMPLE, + ) + : "~~:~~" + + console.log("\n\n") + console.log( - chalk`${DateTime.fromMillis(itinerary.startTime).toLocaleString( - DateTime.TIME_24_SIMPLE, - )} {bold ->} ${DateTime.fromMillis( - itinerary.endTime, - ).toLocaleString(DateTime.TIME_24_SIMPLE)}`, + concatString( + formatTime(itinerary.startTime), + chalk.bold("->"), + formatTime(itinerary.endTime), + ), ) + + console.log( + concatString( + "\n", + "Walkdistance: ", + Math.round(itinerary.walkDistance) + "m", + "\n", + "Reduction:", + "€" + itinerary.fare.fare.reduction.cents / 100, + "\n", + "Regular:", + "€" + itinerary.fare.fare.regular.cents / 100, + "\n", + "Duration:", + Duration.fromMillis(itinerary.duration * 1000) + .shiftTo("minutes") + .toHuman({ maximumFractionDigits: 0 }), + "\n", + ), + ) + itinerary.legs.forEach((leg) => { + // console.log(leg) + console.log( + concatString( + cD(formatTime(leg.from.departure)), + "-", + cD(formatTime(leg.from.arrival)), + cD(leg.mode), + "from", + cD(leg.from.name), + leg.from.platformCode && + `(Platform ${cD(leg.from.platformCode)})`, + "to", + cD(leg.to.name), + leg.to.platformCode && + `(Platform ${cD(leg.to.platformCode)})`, + ), + ) + if (leg.intermediateStops) { + console.log("Itermediate stops:") + + leg.intermediateStops.forEach((stop) => { + console.log( + concatString( + "\t", + cD(formatTime(stop.arrival)), + "-", + cD(formatTime(stop.departure)), + cD(stop.name), + ), + ) + }) + } + }) }) }