updated all functions to use the "const fn = () => {}" syntax

This commit is contained in:
2022-04-05 14:55:03 +02:00
parent ca7a4942fb
commit f259e1cdb9
4 changed files with 41 additions and 27 deletions

View File

@@ -1,10 +1,10 @@
export function addGlobalEventListener(
export const addGlobalEventListener = (
type: string,
selector: string,
callback: (arg0: Event) => void,
options: boolean | AddEventListenerOptions,
parent = document
): void {
): void => {
parent.addEventListener(
type,
e => {
@@ -15,10 +15,10 @@ export function addGlobalEventListener(
)
}
export function qs(selector: string, parent = document): Element {
export const qs = (selector: string, parent = document): Element => {
return parent.querySelector(selector)
}
export function qsa(selector: string, parent = document): Element[] {
export const qsa = (selector: string, parent = document): Element[] => {
return [...parent.querySelectorAll(selector)]
}

View File

@@ -1,20 +1,30 @@
const CURRENCY_FORMATTER = new Intl.NumberFormat(undefined, {
currency: "EUR",
export const formatCurrency = (
number: number,
currency: string,
locale: string | undefined = undefined
): string => {
const CURRENCY_FORMATTER = new Intl.NumberFormat(locale, {
currency: currency,
style: "currency",
})
export function formatCurrency (number: number): string {
return CURRENCY_FORMATTER.format(number)
}
const NUMBER_FORMATTER = new Intl.NumberFormat(undefined)
export function formatNumber (number: number): string {
export const formatNumber = (
number: number,
locale: string | undefined = undefined
): string => {
const NUMBER_FORMATTER = new Intl.NumberFormat(locale)
return NUMBER_FORMATTER.format(number)
}
const COMPACT_NUMBER_FORMATTER = new Intl.NumberFormat(undefined, {
export const formatCompactNumber = (
number: number,
locale: string | undefined = undefined
): string => {
const COMPACT_NUMBER_FORMATTER = new Intl.NumberFormat(locale, {
notation: "compact"
})
export function formatCompactNumber (number: number): string {
return COMPACT_NUMBER_FORMATTER.format(number)
}
@@ -30,7 +40,7 @@ type DivisionsT = {
"minute" | "minutes" |
"second" | "seconds"
}
const DIVISTIONS: DivisionsT[] = [
const DIVISIONS: DivisionsT[] = [
{ amount: 60, name: "seconds" },
{ amount: 60, name: "minutes" },
{ amount: 24, name: "hours" },
@@ -39,14 +49,18 @@ const DIVISTIONS: DivisionsT[] = [
{ amount: 12, name: "months" },
{ amount: Number.POSITIVE_INFINITY, name: "years" },
]
const RELATIVE_DATE_FORMATTER = new Intl.RelativeTimeFormat(undefined, {
export const formatRelativeDate = (
toDate: number,
fromDate: number = new Date().getSeconds(),
locale: string | undefined = undefined
): string => {
const RELATIVE_DATE_FORMATTER = new Intl.RelativeTimeFormat(locale, {
numeric: "auto"
})
export function formatRelativeDate(toDate: Date, fromDate: Date = new Date()) {
let duration = (toDate.getSeconds() - fromDate.getSeconds()) / 1000
let duration = (toDate - fromDate) / 1000
for (let i = 0; i <= DIVISTIONS.length; i++) {
const division = DIVISTIONS[i]
for (let i = 0; i <= DIVISIONS.length; i++) {
const division = DIVISIONS[i]
if (Math.abs(duration) < division.amount) {
return RELATIVE_DATE_FORMATTER.format(Math.round(duration), division.name)
}

View File

@@ -1,3 +1,3 @@
export * from "./utils"
export * from "./domUtils"
export * from "./formatters"
export * from "./utils.js"
export * from "./domUtils.js"
export * from "./formatters.js"

View File

@@ -1,3 +1,3 @@
export function sleep (time: number): Promise<unknown> {
export const sleep = (time: number): Promise<unknown> => {
return new Promise((resolve => setTimeout(resolve, time)))
}