mirror of https://github.com/ghostfolio/ghostfolio
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
3.4 KiB
136 lines
3.4 KiB
4 years ago
|
import { Currency } from '@prisma/client';
|
||
|
import { getDate, getMonth, getYear, subDays } from 'date-fns';
|
||
|
|
||
4 years ago
|
import { ghostfolioScraperApiSymbolPrefix } from './config';
|
||
|
|
||
4 years ago
|
const cryptocurrencies = require('cryptocurrencies');
|
||
|
|
||
|
export const DEMO_USER_ID = '9b112b4d-3b7d-4bad-9bdd-3b0f7b4dac2f';
|
||
|
|
||
|
export function capitalize(aString: string) {
|
||
|
return aString.charAt(0).toUpperCase() + aString.slice(1).toLowerCase();
|
||
|
}
|
||
|
|
||
4 years ago
|
export function getBackgroundColor() {
|
||
|
return getCssVariable(
|
||
|
window.matchMedia('(prefers-color-scheme: dark)').matches
|
||
|
? '--dark-background'
|
||
|
: '--light-background'
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function getCssVariable(aCssVariable: string) {
|
||
|
return getComputedStyle(document.documentElement).getPropertyValue(
|
||
|
aCssVariable
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function getTextColor() {
|
||
|
return getCssVariable(
|
||
|
window.matchMedia('(prefers-color-scheme: dark)').matches
|
||
|
? '--light-primary-text'
|
||
|
: '--dark-primary-text'
|
||
|
);
|
||
|
}
|
||
|
|
||
4 years ago
|
export function getToday() {
|
||
|
const year = getYear(new Date());
|
||
|
const month = getMonth(new Date());
|
||
|
const day = getDate(new Date());
|
||
|
|
||
|
return new Date(Date.UTC(year, month, day));
|
||
|
}
|
||
|
|
||
|
export function getUtc(aDateString: string) {
|
||
|
const [yearString, monthString, dayString] = aDateString.split('-');
|
||
|
|
||
|
return new Date(
|
||
|
Date.UTC(
|
||
|
parseInt(yearString),
|
||
|
parseInt(monthString) - 1,
|
||
|
parseInt(dayString)
|
||
|
)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
export function getYesterday() {
|
||
|
const year = getYear(new Date());
|
||
|
const month = getMonth(new Date());
|
||
|
const day = getDate(new Date());
|
||
|
|
||
|
return subDays(new Date(Date.UTC(year, month, day)), 1);
|
||
|
}
|
||
|
|
||
|
export function groupBy<T, K extends keyof T>(
|
||
|
key: K,
|
||
|
arr: T[]
|
||
|
): Map<T[K], T[]> {
|
||
|
const map = new Map<T[K], T[]>();
|
||
|
arr.forEach((t) => {
|
||
|
if (!map.has(t[key])) {
|
||
|
map.set(t[key], []);
|
||
|
}
|
||
|
map.get(t[key])!.push(t);
|
||
|
});
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
export function isCrypto(aSymbol = '') {
|
||
|
const cryptocurrencySymbol = aSymbol.substring(0, aSymbol.length - 3);
|
||
|
|
||
|
return cryptocurrencies.symbols().includes(cryptocurrencySymbol);
|
||
|
}
|
||
|
|
||
|
export function isCurrency(aSymbol = '') {
|
||
|
return (
|
||
|
(aSymbol.includes(Currency.CHF) ||
|
||
|
aSymbol.includes(Currency.EUR) ||
|
||
|
aSymbol.includes(Currency.USD)) &&
|
||
|
aSymbol.length >= 6
|
||
|
);
|
||
|
}
|
||
|
|
||
4 years ago
|
export function isGhostfolioScraperApiSymbol(aSymbol = '') {
|
||
4 years ago
|
return aSymbol.startsWith(ghostfolioScraperApiSymbolPrefix);
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export function isRakutenRapidApiSymbol(aSymbol = '') {
|
||
4 years ago
|
return aSymbol === 'GF.FEAR_AND_GREED_INDEX';
|
||
|
}
|
||
|
|
||
|
export function parseCurrency(aString: string): Currency {
|
||
|
if (aString?.toLowerCase() === 'chf') {
|
||
|
return Currency.CHF;
|
||
|
} else if (aString?.toLowerCase() === 'eur') {
|
||
|
return Currency.EUR;
|
||
|
} else if (aString?.toLowerCase() === 'gbp') {
|
||
|
return Currency.GBP;
|
||
|
} else if (aString?.toLowerCase() === 'usd') {
|
||
|
return Currency.USD;
|
||
|
}
|
||
|
|
||
|
return undefined;
|
||
|
}
|
||
|
|
||
|
export function resetHours(aDate: Date) {
|
||
|
const year = getYear(aDate);
|
||
|
const month = getMonth(aDate);
|
||
|
const day = getDate(aDate);
|
||
|
|
||
|
return new Date(Date.UTC(year, month, day));
|
||
|
}
|
||
|
|
||
|
export function resolveFearAndGreedIndex(aValue: number) {
|
||
|
if (aValue <= 25) {
|
||
|
return { emoji: '🥵', text: 'Extreme Fear' };
|
||
|
} else if (aValue <= 45) {
|
||
|
return { emoji: '😨', text: 'Fear' };
|
||
|
} else if (aValue <= 55) {
|
||
|
return { emoji: '😐', text: 'Neutral' };
|
||
|
} else if (aValue < 75) {
|
||
|
return { emoji: '😜', text: 'Greed' };
|
||
|
} else if (aValue >= 75) {
|
||
|
return { emoji: '🤪', text: 'Extreme Greed' };
|
||
|
}
|
||
|
}
|