From fe979c63b5700de0e77e5fe7ffd3c20e9fdc31b7 Mon Sep 17 00:00:00 2001 From: Mke Jongbloet Date: Wed, 5 Apr 2023 10:47:21 +0100 Subject: [PATCH] moving to src folder, addressing nullify coalesce feedback, addressing import feedback --- main.ts | 223 --------------------------------------- src/main.ts | 163 ++++++++++++++++++++++++++++ modal.ts => src/modal.ts | 0 styles.css | 8 -- tsconfig.json | 4 +- 5 files changed, 165 insertions(+), 233 deletions(-) delete mode 100644 main.ts create mode 100644 src/main.ts rename modal.ts => src/modal.ts (100%) delete mode 100644 styles.css diff --git a/main.ts b/main.ts deleted file mode 100644 index 69022dd..0000000 --- a/main.ts +++ /dev/null @@ -1,223 +0,0 @@ -import { Editor, Plugin, Notice } from "obsidian"; -import { InsertLinkModal } from "./modal"; - -const yahooStockAPI = require("yahoo-stock-api").default; -const yahoo = new yahooStockAPI(); -export default class StockInfoPlugin extends Plugin { - async onload() { - console.log("reloaded"); - - // Add command to Obsidian quick tasks - - this.addCommand({ - id: "insert-stock-info", - name: "Insert stock info", - editorCallback: (editor: Editor) => { - // get selected text - const selectedText = editor.getSelection(); - - // onSubmit of the form - const onSubmit = async (ticker: string) => { - // helper function: format a long number into millions or billions - const formatLongNumber = (n: number) => { - if (n < 1e9) return +(n / 1e6).toFixed(3) + "M"; - if (n >= 1e9 && n < 1e12) - return +(n / 1e9).toFixed(3) + "B"; - if (n >= 1e12) return +(n / 1e12).toFixed(1) + "T"; - }; - - // helper function: check if a given date = today - const isToday = (someDate: Date) => { - const today = new Date(); - return ( - someDate.getDate() == today.getDate() && - someDate.getMonth() == today.getMonth() && - someDate.getFullYear() == today.getFullYear() - ); - }; - - // create object to store stock return values - let stock: { [key: string]: any } = {}; - - // async function callHistoricalPrices(ticker: string) { - // try { - // const startDate: Date = new Date(); - // const endDate: Date = new Date(); - - // // begin async call for historical prices - // return yahoo.getHistoricalPrices({ - // startDate, - // endDate, - // symbol: ticker, - // frequency: "1d", - // }); - // } catch (e) { - // console.error("An error occurred:", e); - // new Notice( - // "Error: couldn't retrieve stock information", - // 15000 - // ); - // } - // } - - async function callCurrentPrices(ticker: string) { - try { - return yahoo.getSymbol({ - symbol: ticker, - }); - } catch (e) { - console.error("An error occurred:", e); - new Notice( - "Error: couldn't retrieve stock information", - 15000 - ); - } - } - - // await the results of both async calls - const currentStockInfo = await callCurrentPrices(ticker); - - //console.log(historicStockInfo); - console.log(currentStockInfo); - - if (currentStockInfo.error) { - console.error( - "Error occurred:", - currentStockInfo.message - ); - new Notice( - "Error: couldn't retrieve stock information", - 15000 - ); - } else { - // Name of the stock - stock["name"] = currentStockInfo.name - ? currentStockInfo.name - : null; - - // Currency of the stock - stock["currency"] = currentStockInfo.currency - ? currentStockInfo.currency - : null; - - // Bid price of the stock - if (currentStockInfo.response.bid) - stock["bid"] = currentStockInfo.response.bid.value - ? currentStockInfo.response.bid.value - : null; - - // Ask price of the stock - if (currentStockInfo.response.ask) - stock["ask"] = currentStockInfo.response.ask.value - ? currentStockInfo.response.ask.value - : null; - - // Market cap of the stock - stock["marketCap"] = currentStockInfo.response.marketCap - ? currentStockInfo.response.marketCap - : null; - - // Previous close price of the stock - stock["previousClose"] = currentStockInfo.response - .previousClose - ? currentStockInfo.response.previousClose - : null; - - // Volume of shares for the stock - stock["volume"] = currentStockInfo.response.volume - ? currentStockInfo.response.volume - : null; - - // 52 week high - stock["fiftytwo_high"] = currentStockInfo.response - .fiftyTwoWeekRange.high - ? currentStockInfo.response.fiftyTwoWeekRange.high - : null; - - // 52 week low - stock["fiftytwo_low"] = currentStockInfo.response - .fiftyTwoWeekRange.low - ? currentStockInfo.response.fiftyTwoWeekRange.low - : null; - - // Day range high - stock["dayRange_high"] = currentStockInfo.response - .dayRange.high - ? currentStockInfo.response.dayRange.high - : null; - - // Day range low - stock["dayRange_low"] = currentStockInfo.response - .dayRange.low - ? currentStockInfo.response.dayRange.low - : null; - - // Date and time of provided information - stock["updated"] = currentStockInfo.response.updated - ? currentStockInfo.response.updated - : null; - - // BUILD OUTPUT - var output = "> [!info]- " + ticker + " "; - if (stock.bid && stock.ask) - output += - "(Bid: " + - stock.bid + - ", Ask: " + - stock.ask + - ", Spread: " + - ( - ((stock.ask - stock.bid) / stock.ask) * - 100 - ).toFixed(3) + - "%)"; - - if (stock.name) output += "\n> **Name:** " + stock.name; - // if (stock.dayChange) - // output += - // " (" + - // stock.dayChange + - // " / " + - // stock.dayChangePercent + - // "%)"; - if (stock.currency) - output += "\n> **Currency:** " + stock.currency; - if (stock.volume) - output += - "\n> **Volume:** " + - stock.volume.toLocaleString("en-US"); - if (stock.currency && stock.marketCap) - output += - "\n> **Market cap:** " + - formatLongNumber(stock.marketCap); - if (stock.dayRange_low && stock.dayRange_high) - output += - "\n> **Day range:** " + - stock.dayRange_low + - " – " + - stock.dayRange_high; - if (stock.fiftytwo_low && stock.fiftytwo_high) - output += - "\n> **52W range:** " + - stock.fiftytwo_low + - " – " + - stock.fiftytwo_high; - if (stock.updated) - output += - "\n>\n>*" + - new Date(stock.updated) + - "*"; - - editor.replaceSelection(`${output}` + "\n\n"); - - for (var r in stock) delete stock[r]; - for (var r in currentStockInfo) - delete currentStockInfo[r]; - } - }; - - new InsertLinkModal(this.app, selectedText, onSubmit).open(); - }, - }); - } -} diff --git a/src/main.ts b/src/main.ts new file mode 100644 index 0000000..c44c873 --- /dev/null +++ b/src/main.ts @@ -0,0 +1,163 @@ +import { Editor, Plugin, Notice } from "obsidian"; +import { InsertLinkModal } from "./modal"; +import yahooStockAPI from "yahoo-stock-api"; +import { getSymbolResponse } from "yahoo-stock-api/dist/types/getSymbol"; + +const yahoo = new yahooStockAPI(); +export default class StockInfoPlugin extends Plugin { + async onload() { + console.log("reloaded"); + + // Add command to Obsidian quick tasks + + this.addCommand({ + id: "insert-stock-info", + name: "Insert stock info", + editorCallback: (editor: Editor) => { + // get selected text + const selectedText = editor.getSelection(); + + // onSubmit of the form + const onSubmit = async (ticker: string) => { + // helper function: format a long number into millions or billions + const formatLongNumber = (n: number) => { + if (n < 1e9) return +(n / 1e6).toFixed(3) + "M"; + if (n >= 1e9 && n < 1e12) + return +(n / 1e9).toFixed(3) + "B"; + if (n >= 1e12) return +(n / 1e12).toFixed(1) + "T"; + }; + + const logError = (msg: string) => { + console.error("Error occurred:", msg); + new Notice( + "Error: couldn't retrieve stock information", + 15000 + ); + }; + + async function callCurrentPrices(ticker: string) { + try { + return yahoo.getSymbol({ + symbol: ticker, + }); + } catch (e) { + console.error("An error occurred:", e); + new Notice( + "Error: couldn't retrieve stock information", + 15000 + ); + } + } + + // await the results of both async calls + const currentStockInfo = await callCurrentPrices(ticker); + + console.log(currentStockInfo); + + // create object to store stock return values + const stock: { [key: string]: any } = {}; + + if (currentStockInfo) { + // check the API sent back something + if (!currentStockInfo.error) { + // if the API didn't send back an error + stock["name"] = currentStockInfo.name ?? null; // Name of the stock + stock["currency"] = + currentStockInfo.currency ?? null; // Currency of the stock + stock["bid"] = + currentStockInfo.response.bid.value ?? null; // Bid price of the stock + stock["ask"] = + currentStockInfo.response.ask.value ?? null; // Ask price of the stock + stock["marketCap"] = + currentStockInfo.response.marketCap ?? null; // Market cap of the stock + stock["previousClose"] = + currentStockInfo.response.previousClose ?? null; // Previous close price of the stock + stock["volume"] = + currentStockInfo.response.volume ?? null; // Volume of shares for the stock + stock["fiftytwo_high"] = + currentStockInfo.response.fiftyTwoWeekRange + .high ?? null; // 52 week high + stock["fiftytwo_low"] = + currentStockInfo.response.fiftyTwoWeekRange + .low ?? null; // 52 week low + stock["dayRange_high"] = + currentStockInfo.response.dayRange.high ?? null; // Day range high + stock["dayRange_low"] = + currentStockInfo.response.dayRange.low ?? null; // Day range low + stock["updated"] = + currentStockInfo.response.updated ?? null; // Date and time of provided information + + // BUILD OUTPUT + let output = "> [!info]- " + ticker + " "; + if (stock.bid && stock.ask) + output += + "(Bid: " + + stock.bid + + ", Ask: " + + stock.ask + + ", Spread: " + + ( + ((stock.ask - stock.bid) / stock.ask) * + 100 + ).toFixed(3) + + "%)"; + else if (stock.previousClose) + output += + "(Previous close: " + + stock.previousClose + + ")"; + + if (stock.name) + output += "\n> **Name:** " + stock.name; + if (stock.currency) + output += "\n> **Currency:** " + stock.currency; + if (stock.volume) + output += + "\n> **Volume:** " + + stock.volume.toLocaleString("en-US"); + if (stock.currency && stock.marketCap) + output += + "\n> **Market cap:** " + + formatLongNumber(stock.marketCap); + if (stock.bid && stock.ask && stock.previousClose) + output += + "\n> **Previous close:** " + + stock.previousClose; + if (stock.dayRange_low && stock.dayRange_high) + output += + "\n> **Day range:** " + + stock.dayRange_low + + " – " + + stock.dayRange_high; + if (stock.fiftytwo_low && stock.fiftytwo_high) + output += + "\n> **52W range:** " + + stock.fiftytwo_low + + " – " + + stock.fiftytwo_high; + if (stock.updated) + output += + "\n>\n>*" + + new Date(stock.updated) + + "*"; + + editor.replaceSelection(`${output}` + "\n\n"); + + for (const r in stock) delete stock[r]; + for (const r in currentStockInfo) + delete currentStockInfo[r]; + } else { + // API call returned an error + logError(currentStockInfo.message as string); + } + } else { + // API call returned null + logError("API did not respond"); + } + }; + + new InsertLinkModal(this.app, selectedText, onSubmit).open(); + }, + }); + } +} diff --git a/modal.ts b/src/modal.ts similarity index 100% rename from modal.ts rename to src/modal.ts diff --git a/styles.css b/styles.css deleted file mode 100644 index 71cc60f..0000000 --- a/styles.css +++ /dev/null @@ -1,8 +0,0 @@ -/* - -This CSS file will be included with your plugin, and -available in the app when your plugin is enabled. - -If your plugin does not need CSS, delete this file. - -*/ diff --git a/tsconfig.json b/tsconfig.json index 2d6fbdf..e6524f7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "baseUrl": ".", + "baseUrl": "./src/", "inlineSourceMap": true, "inlineSources": true, "module": "ESNext", @@ -19,6 +19,6 @@ ] }, "include": [ - "**/*.ts" + "src/*.ts" ] }