added prettier and eslint for dev
This commit is contained in:
32
lib/domUtils.ts
Normal file
32
lib/domUtils.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
export const addGlobalEventListener = (
|
||||
type: string,
|
||||
selector: string,
|
||||
callback: (arg0: Event) => void,
|
||||
options: boolean | AddEventListenerOptions,
|
||||
parent = document
|
||||
): void => {
|
||||
parent.addEventListener(
|
||||
type,
|
||||
(e: Event) => {
|
||||
if (e != null && e.target != null) {
|
||||
const el = e.target as Element
|
||||
if (el.matches(selector)) callback(e)
|
||||
}
|
||||
},
|
||||
options
|
||||
)
|
||||
}
|
||||
|
||||
export const qs = (
|
||||
selector: string | keyof HTMLElementTagNameMap,
|
||||
parent = document
|
||||
): Element => {
|
||||
return parent.querySelector(selector)
|
||||
}
|
||||
|
||||
export const qsa = (
|
||||
selector: string | keyof HTMLElementTagNameMap,
|
||||
parent = document
|
||||
): Element[] => {
|
||||
return [...parent.querySelectorAll(selector)]
|
||||
}
|
80
lib/formatters.ts
Normal file
80
lib/formatters.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
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)
|
||||
}
|
||||
|
||||
export const formatNumber = (
|
||||
number: number,
|
||||
locale: string | undefined = undefined
|
||||
): string => {
|
||||
const NUMBER_FORMATTER = new Intl.NumberFormat(locale)
|
||||
|
||||
return NUMBER_FORMATTER.format(number)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
type DivisionsT = {
|
||||
amount: number
|
||||
name:
|
||||
| 'year'
|
||||
| 'years'
|
||||
| 'quarter'
|
||||
| 'quarters'
|
||||
| 'month'
|
||||
| 'months'
|
||||
| 'week'
|
||||
| 'weeks'
|
||||
| 'day'
|
||||
| 'days'
|
||||
| 'hour'
|
||||
| 'hours'
|
||||
| 'minute'
|
||||
| 'minutes'
|
||||
| 'second'
|
||||
| 'seconds'
|
||||
}
|
||||
const DIVISIONS: DivisionsT[] = [
|
||||
{ amount: 60, name: 'seconds' },
|
||||
{ amount: 60, name: 'minutes' },
|
||||
{ amount: 24, name: 'hours' },
|
||||
{ amount: 7, name: 'days' },
|
||||
{ amount: 4.34524, name: 'weeks' },
|
||||
{ amount: 12, name: 'months' },
|
||||
{ amount: Number.POSITIVE_INFINITY, name: 'years' }
|
||||
]
|
||||
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 <= DIVISIONS.length; i++) {
|
||||
const division = DIVISIONS[i]
|
||||
if (Math.abs(duration) < division.amount)
|
||||
return RELATIVE_DATE_FORMATTER.format(Math.round(duration), division.name)
|
||||
|
||||
duration /= division.amount
|
||||
}
|
||||
}
|
8
lib/index.ts
Normal file
8
lib/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
export { sleep } from './utils.js'
|
||||
export { qsa, qs, addGlobalEventListener } from './domUtils.js'
|
||||
export {
|
||||
formatCompactNumber,
|
||||
formatNumber,
|
||||
formatCurrency,
|
||||
formatRelativeDate
|
||||
} from './formatters.js'
|
3
lib/utils.ts
Normal file
3
lib/utils.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const sleep = (time: number): Promise<unknown> => {
|
||||
return new Promise((resolve) => setTimeout(resolve, time))
|
||||
}
|
Reference in New Issue
Block a user