Skip to content

Commit

Permalink
fix(historical,chart): throw if period1===period2 (closes #407)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Feb 23, 2022
1 parent 1a05f59 commit e120e55
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/modules/chart.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,32 @@ describe("chart", () => {
);
});

it("throws if period1,period2 are the same", async () => {
await expect(
yf.chart("TSLA", { period1: "2022-02-22", period2: "2022-02-22" })
).rejects.toThrow(/cannot share the same value/);
});

describe("specific cases", () => {
/*
This test is now irrelevant since we no longer allow period1===period2
it("optional fields, empty arrays", async () => {
/*
* Same day period with 1h interval, this result has these aspects:
*
* - Missing fields: timestamp (because no quotes)
* - New fields: previousClose, scale, tradingPeriods
* - Pecularity: indicators.quote is [{}], indicators.adjclose missing
*/
*/ /*
await yf.chart(
"TSLA",
{ period1: "2021-11-23", period2: "2021-11-23", interval: "1h" },
{ devel: "chart-TSLA-2021-11-23-to-2021-11-23-interval-1h.json" }
);
});
*/

it("", async () => {
it("handles queries with tradingPeriod.regular", async () => {
// Had tradingPeriod.regular, probably a timezone thing
await yf.chart(
"TSLA",
Expand Down
7 changes: 7 additions & 0 deletions src/modules/chart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@ export default async function _chart(
);
}

if (queryOptions.period1 === queryOptions.period2) {
throw new Error(
"yahooFinance.chart() options `period1` and `period2` " +
"cannot share the same value."
);
}

// Don't pass this on to Yahoo
delete queryOptions.return;

Expand Down
6 changes: 6 additions & 0 deletions src/modules/historical.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe("historical", () => {
);
});

it("throws if period1,period2 are the same", async () => {
await expect(
yf.historical("TSLA", { period1: "2022-02-22", period2: "2022-02-22" })
).rejects.toThrow(/cannot share the same value/);
});

describe("transformWith", () => {
const yf = { _moduleExec: jest.fn(), historical };
// @ts-ignore: TODO
Expand Down
7 changes: 7 additions & 0 deletions src/modules/historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ export default function historical(
);
}

if (queryOptions.period1 === queryOptions.period2) {
throw new Error(
"yahooFinance.historical() options `period1` and `period2` " +
"cannot share the same value."
);
}

return queryOptions;
},
},
Expand Down

0 comments on commit e120e55

Please sign in to comment.