From f259e1cdb9e04dcb4e79d3d39e0e36de0af5dbc0 Mon Sep 17 00:00:00 2001 From: vikingowl Date: Tue, 5 Apr 2022 14:55:03 +0200 Subject: [PATCH] updated all functions to use the "const fn = () => {}" syntax --- src/domUtils.ts | 8 ++++---- src/formatters.ts | 52 ++++++++++++++++++++++++++++++----------------- src/index.ts | 6 +++--- src/utils.ts | 2 +- 4 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/domUtils.ts b/src/domUtils.ts index b27e2d8..5f24407 100644 --- a/src/domUtils.ts +++ b/src/domUtils.ts @@ -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)] } diff --git a/src/formatters.ts b/src/formatters.ts index dbd3067..cf6748a 100644 --- a/src/formatters.ts +++ b/src/formatters.ts @@ -1,20 +1,30 @@ -const CURRENCY_FORMATTER = new Intl.NumberFormat(undefined, { - currency: "EUR", - style: "currency", -}) -export function formatCurrency (number: number): string { +export const formatCurrency = ( + number: number, + currency: string, + locale: string | undefined = undefined +): string => { + const CURRENCY_FORMATTER = new Intl.NumberFormat(locale, { + currency: currency, + style: "currency", + }) 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, { - notation: "compact" -}) -export function formatCompactNumber (number: number): string { +export const formatCompactNumber = ( + number: number, + locale: string | undefined = undefined +): string => { + const COMPACT_NUMBER_FORMATTER = new Intl.NumberFormat(locale, { + notation: "compact" + }) 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, { - numeric: "auto" -}) -export function formatRelativeDate(toDate: Date, fromDate: Date = new Date()) { - let duration = (toDate.getSeconds() - fromDate.getSeconds()) / 1000 +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" + }) + 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) } diff --git a/src/index.ts b/src/index.ts index f7c2baa..e4251dd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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" diff --git a/src/utils.ts b/src/utils.ts index 357ff83..8ac48df 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,3 +1,3 @@ -export function sleep (time: number): Promise { +export const sleep = (time: number): Promise => { return new Promise((resolve => setTimeout(resolve, time))) }