Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
Add some more usage
Browse files Browse the repository at this point in the history
  • Loading branch information
Netfloex committed Sep 7, 2022
1 parent 0f4aa53 commit d672009
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/breng/types/planner.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ interface Leg {
duration: number
endTime: number
fare?: LegFare
from: From2
from: LegStartOrEndpoint
generalizedCost: number
interlineWithPreviousLeg?: boolean
legGeometry: LegGeometry
Expand All @@ -115,7 +115,7 @@ interface Leg {
route: string
startTime: number
steps: Step[]
to: To
to: LegStartOrEndpoint
transitLeg: boolean
walkingBike?: boolean
agencyId?: string
Expand Down
87 changes: 81 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 | number | undefined>): 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<void> => {
const amsterdam = await search("Amsterdam")
Expand All @@ -20,14 +31,78 @@ const main = async (): Promise<void> => {
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),
),
)
})
}
})
})
}

Expand Down

0 comments on commit d672009

Please sign in to comment.