From be6db797562c85f1be798ac33652d630a872bc1b Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Thu, 10 Apr 2025 13:18:07 +0200 Subject: [PATCH] bugfixing --- src/lib/components/FinancialSection.svelte | 9 +- .../stocks/[tickerID]/financials/+page.svelte | 408 +----------------- .../financials/balance-sheet/+page.svelte | 398 +---------------- .../financials/cash-flow/+page.svelte | 394 +---------------- .../[tickerID]/financials/ratios/+page.svelte | 396 +---------------- 5 files changed, 20 insertions(+), 1585 deletions(-) diff --git a/src/lib/components/FinancialSection.svelte b/src/lib/components/FinancialSection.svelte index e26dbaa8..904d07df 100644 --- a/src/lib/components/FinancialSection.svelte +++ b/src/lib/components/FinancialSection.svelte @@ -18,7 +18,6 @@ import Download from "lucide-svelte/icons/download"; import { goto } from "$app/navigation"; - import SEO from "$lib/components/SEO.svelte"; export let data; export let title; @@ -110,6 +109,7 @@ const headers = properties.map((prop) => prop.label); rows.unshift(headers); + const fileTitle = title?.toLowerCase()?.trim()?.replace(/\s+/g, "_"); // Check the format to export if (format.toLowerCase() === "csv") { const csvContent = rows.map((row) => row.join(",")).join("\n"); @@ -121,7 +121,7 @@ a.href = url; a.download = $stockTicker.toLowerCase() + - `${$selectedTimePeriod === "annual" ? "_annual" : $selectedTimePeriod === "quarterly" ? "_quarter" : "_ttm"}_income_statement.csv`; + `${$selectedTimePeriod === "annual" ? "_annual" : $selectedTimePeriod === "quarterly" ? "_quarter" : "_ttm"}_${fileTitle}.csv`; document.body.appendChild(a); a.click(); document.body.removeChild(a); @@ -207,11 +207,6 @@ } - -
- import { - displayCompanyName, - stockTicker, - coolMode, - timeFrame, - selectedTimePeriod, - screenWidth, - } from "$lib/store"; - import { removeCompanyStrings } from "$lib/utils"; - import * as DropdownMenu from "$lib/components/shadcn/dropdown-menu/index.js"; - import { Button } from "$lib/components/shadcn/button/index.js"; - //import * as XLSX from 'xlsx'; - import FinancialTable from "$lib/components/FinancialTable.svelte"; - import FinancialChart from "$lib/components/FinancialChart.svelte"; - import TableMode from "lucide-svelte/icons/grid-2x2"; - import ChartMode from "lucide-svelte/icons/chart-column-increasing"; - import Download from "lucide-svelte/icons/download"; + import { displayCompanyName, stockTicker } from "$lib/store"; - import { goto } from "$app/navigation"; import SEO from "$lib/components/SEO.svelte"; + import FinancialSection from "$lib/components/FinancialSection.svelte"; export let data; - let switchDate = false; - let tableList = []; - let processedData = {}; - - let financialData = []; - let fullStatement = []; - const statementConfig = [ { propertyName: "revenue", @@ -120,176 +97,6 @@ label: "Depreciation & Amortization", }, ]; - - const fields = statementConfig.map((item) => ({ - label: item.label, - key: item.propertyName, - })); - function toggleMode() { - $coolMode = !$coolMode; - } - - const getCurrentYear = () => new Date()?.getFullYear(); - - const filterStatement = (fullStatement, timeFrame, switchDate) => { - const currentYear = getCurrentYear(); - - let filtered = [...(fullStatement || [])]; - - switch (timeFrame) { - case "5Y": - filtered = filtered.filter( - (item) => currentYear - parseInt(item?.fiscalYear) < 5, - ); - break; - case "10Y": - filtered = filtered.filter( - (item) => currentYear - parseInt(item?.fiscalYear) < 10, - ); - break; - } - - // Sort by date depending on switchDate - filtered.sort((a, b) => { - const dateA = new Date(a.date || a.fiscalDate || a.fiscalYear); - const dateB = new Date(b.date || b.fiscalDate || b.fiscalYear); - return switchDate - ? dateA - dateB // earliest to latest - : dateB - dateA; // latest to earliest - }); - - return filtered; - }; - - fullStatement = data?.getData; - - const exportFundamentalData = (format = "csv") => { - if (["Pro", "Plus"]?.includes(data?.user?.tier)) { - const data = fullStatement; - if (!data || data.length === 0) { - return; - } - - let properties = [ - { - key: $selectedTimePeriod === "annual" ? "fiscalYear" : "date", - label: $selectedTimePeriod === "annual" ? "Year" : "Quarter", - }, - ]; - - for (let i = 0; i < statementConfig?.length; i++) { - properties.push({ - key: statementConfig[i]?.propertyName, - label: statementConfig[i]?.label, - }); - } - - // Helper function to handle special cases - - // Create rows for CSV/Excel - let rows = data.map((item) => - properties?.map((property) => item[property?.key] || 0), - ); - - // Include headers - const headers = properties.map((prop) => prop.label); - rows.unshift(headers); - - // Check the format to export - if (format.toLowerCase() === "csv") { - const csvContent = rows.map((row) => row.join(",")).join("\n"); - const blob = new Blob([csvContent], { - type: "data:text/csv;charset=utf-8", - }); - const url = URL.createObjectURL(blob); - const a = document.createElement("a"); - a.href = url; - a.download = - $stockTicker.toLowerCase() + - `${$selectedTimePeriod === "annual" ? "_annual" : $selectedTimePeriod === "quarterly" ? "_quarter" : "_ttm"}_income_statement.csv`; - document.body.appendChild(a); - a.click(); - document.body.removeChild(a); - URL.revokeObjectURL(url); - } - } else { - goto("/pricing"); - } - }; - - // Pre-process all data once instead of in each component - function preprocessFinancialData() { - processedData = {}; - - // Precompute mapping from propertyName to config for quick lookup - const configMap = {}; - statementConfig.forEach((item) => { - if (item && item.propertyName) { - configMap[item.propertyName] = item; - } - }); - - // Precompute xList from income (reverse order) - const xList = []; - for (let i = financialData.length - 1; i >= 0; i--) { - const statement = financialData[i]; - const year = statement.fiscalYear.slice(-2); - const quarter = statement.period; - xList.push( - $selectedTimePeriod === "annual" - ? "FY" + year - : "FY" + year + " " + quarter, - ); - } - - // Process each field using precomputed config and xList - fields.forEach((field) => { - const statementKey = field.key; - const config = configMap[statementKey]; - if (!config) return; - - const valueList = []; - // Loop through financialData in reverse to match xList order - for (let i = financialData.length - 1; i >= 0; i--) { - const statement = financialData[i]; - const rawValue = Number(statement[config.propertyName]); - // Round to two decimals - const value = parseFloat(rawValue.toFixed(2)); - valueList.push(value); - } - - processedData[statementKey] = { - xList, // re-use the precomputed labels - valueList, - labelName: config.label, - }; - }); - - // Build tableList once for all charts and sort by date (newest first) - tableList = financialData?.map((statement) => ({ - date: statement.date, - // Add more properties if needed - })); - - tableList.sort((a, b) => new Date(b.date) - new Date(a.date)); - } - - $: { - if ($timeFrame || $selectedTimePeriod) { - if ($selectedTimePeriod === "annual") { - fullStatement = data?.getData?.annual; - } else if ($selectedTimePeriod === "quarterly") { - fullStatement = data?.getData?.quarter; - } else if ($selectedTimePeriod === "ttm") { - fullStatement = data?.getData?.ttm; - } else { - fullStatement = data?.getData?.annual; - } - - financialData = filterStatement(fullStatement, $timeFrame, switchDate); - preprocessFinancialData(); - } - } -
-
-
-
-
-
-

- {removeCompanyStrings($displayCompanyName)} Income Statement -

-
- -
- {#if financialData?.length > 0} -
- - Financials in {financialData?.at(0)?.reportedCurrency}. Fiscal - year is - {data?.getProfileData?.fiscalYearRange}. - -
- - - -
- - - - - - - Select time frame - - - - ($timeFrame = "5Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 5 years - - ($timeFrame = "10Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 10 years - - ($timeFrame = "MAX")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - Max - - - - -
- - -
-
- {#if $coolMode} -
- {#each fields as item, i} - - {/each} -
- {:else} -
- - - - - {#each financialData as item} - {#if $selectedTimePeriod === "annual"} - - {:else} - - {/if} - {/each} - - - - {#each financialData as item} - - {/each} - - - - - - -
Fiscal Year - {"FY" + " " + item?.fiscalYear} - - {item?.period + " " + item?.fiscalYear} -
Period Ending - {new Date(item?.date).toLocaleDateString("en-US", { - month: "short", - day: "numeric", - year: "numeric", - })} -
-
- {/if} - {/if} -
-
-
-
-
-
+ diff --git a/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte b/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte index 35e38a68..6aa9a0b0 100644 --- a/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte +++ b/src/routes/stocks/[tickerID]/financials/balance-sheet/+page.svelte @@ -1,28 +1,11 @@ -
-
-
-
-
-
-

- {removeCompanyStrings($displayCompanyName)} Income Statement -

- -
- -
- {#if financialData?.length > 0} -
- - Financials in {financialData?.at(0)?.reportedCurrency}. Fiscal - year is - {data?.getProfileData?.fiscalYearRange}. - -
- -
- - - - - - - Select time frame - - - - ($timeFrame = "5Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 5 years - - ($timeFrame = "10Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 10 years - - ($timeFrame = "MAX")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - Max - - - - -
- - -
-
- {#if $coolMode} -
- {#each fields as item, i} - - {/each} -
- {:else} -
- - - - - {#each financialData as item} - {#if $selectedTimePeriod === "annual"} - - {:else} - - {/if} - {/each} - - - - {#each financialData as item} - - {/each} - - - - - - -
Fiscal Year - {"FY" + " " + item?.fiscalYear} - - {item?.period + " " + item?.fiscalYear} -
Period Ending - {new Date(item?.date).toLocaleDateString("en-US", { - month: "short", - day: "numeric", - year: "numeric", - })} -
-
- {/if} - {/if} -
-
-
-
-
-
+ diff --git a/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte b/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte index 7ad8405f..4a2f4aa1 100644 --- a/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte +++ b/src/routes/stocks/[tickerID]/financials/cash-flow/+page.svelte @@ -1,28 +1,11 @@ -
-
-
-
-
-
-

- {removeCompanyStrings($displayCompanyName)} Income Statement -

- -
- -
- {#if financialData?.length > 0} -
- - Financials in {financialData?.at(0)?.reportedCurrency}. Fiscal - year is - {data?.getProfileData?.fiscalYearRange}. - -
- -
- - - - - - - Select time frame - - - - ($timeFrame = "5Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 5 years - - ($timeFrame = "10Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 10 years - - ($timeFrame = "MAX")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - Max - - - - -
- - -
-
- {#if $coolMode} -
- {#each fields as item, i} - - {/each} -
- {:else} -
- - - - - {#each financialData as item} - {#if $selectedTimePeriod === "annual"} - - {:else} - - {/if} - {/each} - - - - {#each financialData as item} - - {/each} - - - - - - -
Fiscal Year - {"FY" + " " + item?.fiscalYear} - - {item?.period + " " + item?.fiscalYear} -
Period Ending - {new Date(item?.date).toLocaleDateString("en-US", { - month: "short", - day: "numeric", - year: "numeric", - })} -
-
- {/if} - {/if} -
-
-
-
-
-
+ diff --git a/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte b/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte index 25b62d23..ad79a36f 100644 --- a/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte +++ b/src/routes/stocks/[tickerID]/financials/ratios/+page.svelte @@ -1,28 +1,11 @@ -
-
-
-
-
-
-

- {removeCompanyStrings($displayCompanyName)} Income Statement -

- -
- -
- {#if financialData?.length > 0} -
- - Financials in {financialData?.at(0)?.reportedCurrency}. Fiscal - year is - {data?.getProfileData?.fiscalYearRange}. - -
- -
- - - - - - - Select time frame - - - - ($timeFrame = "5Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 5 years - - ($timeFrame = "10Y")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - 10 years - - ($timeFrame = "MAX")} - class="cursor-pointer sm:hover:bg-gray-300 dark:sm:hover:bg-primary" - > - Max - - - - -
- - -
-
- {#if $coolMode} -
- {#each fields as item, i} - - {/each} -
- {:else} -
- - - - - {#each financialData as item} - {#if $selectedTimePeriod === "annual"} - - {:else} - - {/if} - {/each} - - - - {#each financialData as item} - - {/each} - - - - - - -
Fiscal Year - {"FY" + " " + item?.fiscalYear} - - {item?.period + " " + item?.fiscalYear} -
Period Ending - {new Date(item?.date).toLocaleDateString("en-US", { - month: "short", - day: "numeric", - year: "numeric", - })} -
-
- {/if} - {/if} -
-
-
-
-
-
+