Skip to content

Commit

Permalink
fix(csv2json,historical): dividends/csv with no data (fixes #658)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Aug 12, 2023
1 parent 662760d commit 13dcc64
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lib/csv2json.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ describe("csv2json", () => {
expect(obj[0].string).toBeType("string");
});

/*
actually, one line files are ok... headers and no data.
it("throws on weird file (no newlines)", () => {
const csv = "one line file";
expect(() => csv2json(csv)).toThrow(/No newlines/);
});
*/

it("handles nulls", () => {
const obj = csv2json("val\nnull")[0];
Expand Down
5 changes: 4 additions & 1 deletion src/lib/csv2json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ function convert(input: any) {

export default function csv2json(csv: string): Array<any> {
const lines = csv.split("\n");
if (lines.length === 1) throw new Error("No newlines in: " + csv);

// Actually we should handle this case, i.e. headers but no data.
// if (lines.length === 1)
// throw new Error("No newlines in: " + csv);

const headers = (lines.shift() as string).split(DELIMITER).map(camelize);
const out = new Array(lines.length);
Expand Down
14 changes: 14 additions & 0 deletions src/modules/historical.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,18 @@ describe("historical", () => {
).rejects.toThrow("SOME (but not all) null values");
});
});

it("handles events:dividends for stocks with no dividends (#658)", async () => {
const data = await yf.historical(
"TSLA", // No dividends at time of writing
{
period1: 0,
period2: "2023-08-12", // time of writing :)
events: "dividends",
interval: "1d",
},
{ devel: "historical-dividends-TSLA-no-dividends.json" }
);
// Enough to check that this doesn't throw.
});
});
2 changes: 2 additions & 0 deletions src/modules/historical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ export default function historical(
result: {
schemaKey,
transformWith(result: any) {
if (result.length === 0) return result;

const filteredResults = [];
const fieldCount = Object.keys(result[0]).length;

Expand Down
73 changes: 73 additions & 0 deletions tests/http/historical-dividends-TSLA-no-dividends.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"request": {
"url": "https://query2.finance.yahoo.com/v7/finance/download/TSLA?interval=1d&events=dividends&includeAdjustedClose=true&period1=0&period2=1691798400"
},
"response": {
"ok": true,
"status": 200,
"statusText": "OK",
"headers": {
"content-disposition": [
"attachment; filename=TSLA.csv"
],
"content-type": [
"text/csv;charset=utf-8"
],
"vary": [
"Origin"
],
"cache-control": [
"private, max-age=10, stale-while-revalidate=20"
],
"y-rid": [
"06jo791idekh9"
],
"x-yahoo-request-id": [
"06jo791idekh9"
],
"x-request-id": [
"e96cb916-7317-4b22-81af-105251e06cec"
],
"content-length": [
"14"
],
"x-envoy-upstream-service-time": [
"20"
],
"date": [
"Sat, 12 Aug 2023 09:34:33 GMT"
],
"server": [
"ATS"
],
"x-envoy-decorator-operation": [
"finance-chart-api--mtls-production-ir2.finance-k8s.svc.yahoo.local:4080/*"
],
"age": [
"0"
],
"strict-transport-security": [
"max-age=31536000"
],
"referrer-policy": [
"no-referrer-when-downgrade"
],
"x-frame-options": [
"SAMEORIGIN"
],
"connection": [
"close"
],
"expect-ct": [
"max-age=31536000, report-uri=\"http://csp.yahoo.com/beacon/csp?src=yahoocom-expect-ct-report-only\""
],
"x-xss-protection": [
"1; mode=block"
],
"x-content-type-options": [
"nosniff"
]
},
"body": "Date,Dividends"
}
}

0 comments on commit 13dcc64

Please sign in to comment.