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, type: string,
selector: string, selector: string,
callback: (arg0: Event) => void, callback: (arg0: Event) => void,
options: boolean | AddEventListenerOptions, options: boolean | AddEventListenerOptions,
parent = document parent = document
): void { ): void => {
parent.addEventListener( parent.addEventListener(
type, type,
e => { 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) return parent.querySelector(selector)
} }
export function qsa(selector: string, parent = document): Element[] { export const qsa = (selector: string, parent = document): Element[] => {
return [...parent.querySelectorAll(selector)] return [...parent.querySelectorAll(selector)]
} }

View File

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

View File

@@ -1,3 +1,3 @@
export * from "./utils" export * from "./utils.js"
export * from "./domUtils" export * from "./domUtils.js"
export * from "./formatters" 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))) return new Promise((resolve => setTimeout(resolve, time)))
} }