This commit is contained in:
MuslemRahimi 2024-12-10 19:37:34 +01:00
parent e5435ebd9a
commit d6e92e7b4c
5 changed files with 39 additions and 35 deletions

View File

@ -49,7 +49,7 @@
let downloadWorker: Worker | undefined; let downloadWorker: Worker | undefined;
let checkedItems; let checkedItems;
let stockList = rawData?.slice(0, 150); let stockList = originalData?.slice(0, 150);
let scrollPosition = 0; let scrollPosition = 0;
//$: stockList = originalData.slice(0, 150); //$: stockList = originalData.slice(0, 150);

View File

@ -194,20 +194,21 @@ export const groupNews = (news, watchList) => {
}; };
export const calculateChange = (oldList?: any[], newList?: any[]) => { export const calculateChange = (oldList = [], newList = []) => {
if (!oldList?.length || !newList?.length) return [...(oldList || [])]; if (!oldList.length || !newList.length) return [...oldList];
// Create a Map for fast lookups of new list items by symbol
const newListMap = new Map(newList.map((item) => [item.symbol, item])); const newListMap = new Map(newList.map((item) => [item.symbol, item]));
const updatedList = [];
for (let i = 0; i < oldList.length; i++) { return oldList.map((item) => {
const item = oldList[i];
const newItem = newListMap.get(item.symbol); const newItem = newListMap.get(item.symbol);
if (newItem?.avgPrice) { // Check if the symbols match and the newItem has the necessary properties
if (newItem && newItem.symbol === item.symbol && newItem.avgPrice) {
const { price, changesPercentage } = item; const { price, changesPercentage } = item;
const newPrice = newItem.avgPrice; const newPrice = newItem.avgPrice;
// Only update the changesPercentage if both price and changesPercentage are defined
if (price != null && changesPercentage != null) { if (price != null && changesPercentage != null) {
const baseLine = price / (1 + Number(changesPercentage) / 100); const baseLine = price / (1 + Number(changesPercentage) / 100);
item.changesPercentage = ((newPrice / baseLine - 1) * 100); item.changesPercentage = ((newPrice / baseLine - 1) * 100);
@ -217,40 +218,42 @@ export const calculateChange = (oldList?: any[], newList?: any[]) => {
item.price = newPrice; item.price = newPrice;
} }
updatedList.push(item); return item;
} });
return updatedList;
}; };
export function updateStockList(stockList, originalData) { export function updateStockList(stockList = [], originalData = []) {
// Create a Map for O(1) lookup of original data by symbol // Create a Map for fast O(1) lookups of original data by symbol
const originalDataMap = new Map( const originalDataMap = new Map(originalData.map(item => [item.symbol, item]));
originalData?.map(item => [item.symbol, item])
);
// Use .map() to create a new array with updated stocks // Initialize an array to store the updated stock list
return stockList?.map(stock => { const updatedStockList = [];
// Find matching stock in originalData
// Iterate through each stock in the stockList
for (let i = 0; i < stockList.length; i++) {
const stock = stockList[i];
const matchingStock = originalDataMap?.get(stock?.symbol); const matchingStock = originalDataMap?.get(stock?.symbol);
// If a matching stock is found, update it
// If a match is found, update price and changesPercentage
if (matchingStock) { if (matchingStock) {
return { updatedStockList.push({
...stock, ...stock,
price: matchingStock?.price, price: matchingStock.price,
changesPercentage: matchingStock?.changesPercentage, changesPercentage: matchingStock.changesPercentage,
previous: matchingStock?.previous ?? null, previous: matchingStock.previous ?? null,
}; });
} else {
// If no match, add the stock unchanged
updatedStockList.push(stock);
}
} }
// If no match, return the original stock object unchanged // Return the updated stock list
return stock; return updatedStockList;
});
} }
export const flyAndScale = ( export const flyAndScale = (
node: Element, node: Element,
params: FlyAndScaleParams = { y: -8, x: 0, start: 0.95, duration: 0 }, params: FlyAndScaleParams = { y: -8, x: 0, start: 0.95, duration: 0 },

View File

@ -1,5 +1,5 @@
export const load = async ({ locals, params }) => { export const load = async ({ locals, params }) => {
const { apiKey, apiURL, user } = locals; const { apiKey, apiURL } = locals;
const getETFHoldings = async () => { const getETFHoldings = async () => {
const postData = { const postData = {
ticker: params.tickerID, ticker: params.tickerID,
@ -17,6 +17,7 @@ export const load = async ({ locals, params }) => {
const output = await response.json(); const output = await response.json();
return output; return output;
}; };

View File

@ -8,7 +8,8 @@
import Table from "$lib/components/Table/Table.svelte"; import Table from "$lib/components/Table/Table.svelte";
export let data; export let data;
let rawData = data?.getETFHoldings?.holdings || []; let rawData = data?.getETFHoldings?.holdings;
const lastUpdate = new Date(data?.getETFHoldings?.lastUpdate); const lastUpdate = new Date(data?.getETFHoldings?.lastUpdate);
const options = { month: "short", day: "numeric", year: "numeric" }; const options = { month: "short", day: "numeric", year: "numeric" };
const formattedDate = lastUpdate?.toLocaleDateString("en-US", options); const formattedDate = lastUpdate?.toLocaleDateString("en-US", options);

View File

@ -1,7 +1,6 @@
<script lang="ts"> <script lang="ts">
import { abbreviateNumber } from "$lib/utils"; import { abbreviateNumber } from "$lib/utils";
import Table from "$lib/components/Table/Table.svelte"; import Table from "$lib/components/Table/Table.svelte";
import { screenWidth } from "$lib/store";
import { page } from "$app/stores"; import { page } from "$app/stores";
export let data; export let data;