Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[historical] throw error if given a string for period{1,2} that is invalid input for new Date() #460

Merged
merged 3 commits into from
May 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ throw if they don't match. If you're returning a fake result, name
your file `.fake.json`.

Occasionally we want to skip caching and only return live results, e.g.
to check if our validation pasts at different times of the day (when
to check if our validation passes at different times of the day (when
different markets are open):

```bash
Expand Down
16 changes: 12 additions & 4 deletions src/modules/historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,18 @@ export default function historical(
const value = queryOptions[fieldName];
if (value instanceof Date)
queryOptions[fieldName] = Math.floor(value.getTime() / 1000);
else if (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) || timestamp <= 0)
glaucoheitor marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
"yahooFinance.historical() option '" +
fieldName +
"' invalid date provided."
);
glaucoheitor marked this conversation as resolved.
Show resolved Hide resolved

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

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