From 465e7385defcb6b5520487cf631e5f9470c26885 Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Tue, 13 Aug 2024 14:54:08 +0200 Subject: [PATCH] bugfixing: show last X years correctly for quarter results --- .../stats/balance-sheet/+page.svelte | 24 ++++++++++++------- .../[tickerID]/stats/cash-flow/+page.svelte | 23 ++++++++++++------ .../[tickerID]/stats/income/+page.svelte | 24 ++++++++++++------- .../[tickerID]/stats/ratios/+page.svelte | 20 +++++++++++----- 4 files changed, 62 insertions(+), 29 deletions(-) diff --git a/src/routes/stocks/[tickerID]/stats/balance-sheet/+page.svelte b/src/routes/stocks/[tickerID]/stats/balance-sheet/+page.svelte index 8b2c2548..6f92a008 100644 --- a/src/routes/stocks/[tickerID]/stats/balance-sheet/+page.svelte +++ b/src/routes/stocks/[tickerID]/stats/balance-sheet/+page.svelte @@ -379,11 +379,23 @@ const exportData = (format = 'csv') => { fullStatement = data?.getBalanceSheetStatement; timeFrame = '10Y'; - balanceSheet = fullStatement?.slice(0,10); displayStatement = 'cashAndCashEquivalents'; - - + const getCurrentYear = () => new Date()?.getFullYear(); + +const filterStatement = (fullStatement, timeFrame) => { + const currentYear = getCurrentYear(); + + switch(timeFrame) { + case '5Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 5); + case '10Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 10); + default: + return fullStatement; + } +}; + $: { if (timeFrame || displayStatement || filterRule) { @@ -395,11 +407,7 @@ $: { fullStatement = data?.getBalanceSheetStatement; } - balanceSheet = timeFrame === '5Y' - ? (fullStatement?.slice(0, 5)) - : timeFrame === '10Y' - ? (fullStatement?.slice(0, 10)) - : fullStatement; + balanceSheet = filterStatement(fullStatement, timeFrame); if (mode === true) { diff --git a/src/routes/stocks/[tickerID]/stats/cash-flow/+page.svelte b/src/routes/stocks/[tickerID]/stats/cash-flow/+page.svelte index 7ab2ad1c..a9f4f161 100644 --- a/src/routes/stocks/[tickerID]/stats/cash-flow/+page.svelte +++ b/src/routes/stocks/[tickerID]/stats/cash-flow/+page.svelte @@ -344,11 +344,24 @@ const exportData = (format = 'csv') => { fullStatement = data?.getCashFlowStatement; timeFrame = '10Y'; -cashFlow = fullStatement?.slice(0,10); displayStatement = 'netIncome'; - +const getCurrentYear = () => new Date()?.getFullYear(); + +const filterStatement = (fullStatement, timeFrame) => { + const currentYear = getCurrentYear(); + + switch(timeFrame) { + case '5Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 5); + case '10Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 10); + default: + return fullStatement; + } +}; + $: { if (timeFrame || displayStatement || filterRule) { @@ -359,11 +372,7 @@ $: { fullStatement = data?.getCashFlowStatement; } - cashFlow = timeFrame === '5Y' - ? (fullStatement?.slice(0, 5)) - : timeFrame === '10Y' - ? (fullStatement?.slice(0, 10)) - : fullStatement; + cashFlow = filterStatement(fullStatement, timeFrame); if (mode === true) { diff --git a/src/routes/stocks/[tickerID]/stats/income/+page.svelte b/src/routes/stocks/[tickerID]/stats/income/+page.svelte index 17fc984e..612ff569 100644 --- a/src/routes/stocks/[tickerID]/stats/income/+page.svelte +++ b/src/routes/stocks/[tickerID]/stats/income/+page.svelte @@ -315,10 +315,23 @@ const exportData = (format = 'csv') => { }; */ - +const getCurrentYear = () => new Date()?.getFullYear(); + +const filterStatement = (fullStatement, timeFrame) => { + const currentYear = getCurrentYear(); + + switch(timeFrame) { + case '5Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 5); + case '10Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 10); + default: + return fullStatement; + } +}; + fullStatement = data?.getIncomeStatement; timeFrame = '10Y'; - income = fullStatement?.slice(0,10); displayStatement = 'revenue'; @@ -331,12 +344,7 @@ const exportData = (format = 'csv') => { else { fullStatement = data?.getIncomeStatement; } - - income = timeFrame === '5Y' - ? (fullStatement?.slice(0, 5)) - : timeFrame === '10Y' - ? (fullStatement?.slice(0, 10)) - : fullStatement; + income = filterStatement(fullStatement, timeFrame); if (mode === true) { diff --git a/src/routes/stocks/[tickerID]/stats/ratios/+page.svelte b/src/routes/stocks/[tickerID]/stats/ratios/+page.svelte index 70224f89..5131f4a2 100644 --- a/src/routes/stocks/[tickerID]/stats/ratios/+page.svelte +++ b/src/routes/stocks/[tickerID]/stats/ratios/+page.svelte @@ -288,10 +288,22 @@ const exportData = (format = 'csv') => { fullStatement = data?.getRatiosStatement; timeFrame = '10Y'; -ratios = fullStatement?.slice(0,10); displayStatement = 'priceEarningsRatio'; +const getCurrentYear = () => new Date()?.getFullYear(); +const filterStatement = (fullStatement, timeFrame) => { + const currentYear = getCurrentYear(); + + switch(timeFrame) { + case '5Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 5); + case '10Y': + return fullStatement?.filter(item => currentYear - parseInt(item?.calendarYear) < 10); + default: + return fullStatement; + } +}; $: { @@ -304,11 +316,7 @@ $: { fullStatement = data?.getRatiosStatement; } - ratios = timeFrame === '5Y' - ? (fullStatement?.slice(0, 5)) - : timeFrame === '10Y' - ? (fullStatement?.slice(0, 10)) - : fullStatement; + ratios = filterStatement(fullStatement, timeFrame); if (mode === true) {