Skip to content

Commit

Permalink
fix(chart): fix and improve non-Date handling code for periods
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Sep 16, 2024
1 parent 7d0deea commit f1106c2
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions src/modules/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ const ChartResultObjectSchema = Type.Object(
},
);

const ChartOptionsSchema = Type.Object(
export const ChartOptionsSchema = Type.Object(
{
period1: Type.Union([Type.Date(), Type.String(), YahooNumber]),
period2: Type.Optional(
Expand Down Expand Up @@ -329,12 +329,22 @@ export default async function chart(
const dates = ["period1", "period2"] as const;
for (const fieldName of dates) {
const value = queryOptions[fieldName];
if (value instanceof Date)
if (value instanceof Date) {
queryOptions[fieldName] = Math.floor(value.getTime() / 1000);
else typeof value === "string";
queryOptions[fieldName] = Math.floor(
new Date(value as string).getTime() / 1000,
);
} else if (typeof value === "string") {
const timestamp = new Date(value as string).getTime();

if (isNaN(timestamp))
throw new Error(
"yahooFinance.chart() option '" +
fieldName +
"' invalid date provided: '" +
value +
"'",
);

queryOptions[fieldName] = Math.floor(timestamp / 1000);
}
}

if (queryOptions.period1 === queryOptions.period2) {
Expand Down

0 comments on commit f1106c2

Please sign in to comment.