Skip to content

Commit

Permalink
feat(quote): initial returnAs support
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Jun 1, 2021
1 parent f6f3e60 commit bf29824
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
11 changes: 11 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4719,6 +4719,9 @@
"$ref": "#/definitions/QuoteField"
},
"type": "array"
},
"returnAs": {
"$ref": "#/definitions/ResultType"
}
},
"type": "object"
Expand Down Expand Up @@ -5100,6 +5103,14 @@
],
"type": "string"
},
"ResultType": {
"enum": [
"array",
"object",
"map"
],
"type": "string"
},
"RevenueEstimate": {
"additionalProperties": false,
"properties": {
Expand Down
38 changes: 38 additions & 0 deletions src/modules/quote.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,42 @@ describe("quote", () => {
expect(result.symbol).toBe("TSLA");
expect(result.displayName).toBeDefined();
});

describe("returnAs", () => {
it("array", async () => {
const devel = "quote-AAPL-BABA.json";
const results = await yf.quote(
["AAPL", "BABA"],
{ returnAs: "array" },
{ devel }
);
expect(results.length).toBe(2);
expect(results[0].symbol).toBe("AAPL");
expect(results[1].symbol).toBe("BABA");
});

it("object", async () => {
const devel = "quote-AAPL-BABA.json";
const results = await yf.quote(
["AAPL", "BABA"],
{ returnAs: "object" },
{ devel }
);
expect(Object.keys(results).length).toBe(2);
expect(results.AAPL.symbol).toBe("AAPL");
expect(results.BABA.symbol).toBe("BABA");
});

it("map", async () => {
const devel = "quote-AAPL-BABA.json";
const results = await yf.quote(
["AAPL", "BABA"],
{ returnAs: "map" },
{ devel }
);
expect(results.size).toBe(2);
expect(results.get("AAPL").symbol).toBe("AAPL");
expect(results.get("BABA").symbol).toBe("BABA");
});
});
});
26 changes: 23 additions & 3 deletions src/modules/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ export type QuoteResponse = Quote[];

export type QuoteField = keyof Quote;

export type ResultType = "array" | "object" | "map";

export interface QuoteOptions {
fields?: QuoteField[];
returnAs?: ResultType;
}

const queryOptionsDefaults = {};
Expand Down Expand Up @@ -189,6 +192,7 @@ export default function quote(
moduleOptions?: ModuleOptions
): Promise<any> {
const symbols = typeof query === "string" ? query : query.join(",");
const returnAs = queryOptionsOverrides && queryOptionsOverrides.returnAs;

return this._moduleExec({
moduleName: "quote",
Expand All @@ -202,6 +206,7 @@ export default function quote(
transformWith(queryOptions: QuoteOptions) {
// Options validation ensures this is a string[]
if (queryOptions.fields) queryOptions.fields.join(",");
delete queryOptions.returnAs;
return queryOptions;
},
},
Expand All @@ -217,8 +222,23 @@ export default function quote(

moduleOptions,
}).then((results: Quote[]) => {
return typeof query === "string"
? (results[0] as Quote)
: (results as Quote[]);
if (returnAs) {
switch (returnAs) {
case "array":
return results as Quote[];
case "object":
const object = {} as any;
for (let result of results) object[result.symbol] = result;
return object; // TODO: type
case "map":
const map = new Map();
for (let result of results) map.set(result.symbol, result);
return map; // TODO: type
}
} else {
return typeof query === "string"
? (results[0] as Quote)
: (results as Quote[]);
}
});
}

0 comments on commit bf29824

Please sign in to comment.