webapp/src/utils/index.ts

84 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-09-09 11:53:35 +00:00
export const reflow = () => document.body.clientWidth
2022-10-07 19:35:53 +00:00
export const unique = (v) => {
2022-09-09 11:53:35 +00:00
const s = new Set(v)
return [...s]
}
export const preventSmoothScrollOnTabbing = () => {
document.addEventListener('keydown', (e) => {
if (e.key !== 'Tab') return
document.documentElement.style.scrollBehavior = ''
setTimeout(() => {
document.documentElement.style.scrollBehavior = 'smooth'
})
})
}
export const capitalize = (originalString: string, firstonly = false) => {
const s = originalString.trim()
return firstonly
? s.charAt(0).toUpperCase() + s.slice(1)
: s
.split(' ')
.map((w) => w.charAt(0).toUpperCase() + w.slice(1))
.join(' ')
}
export const plural = (amount: number, w: string[]) => {
try {
const a = amount.toString()
const x = Number.parseInt(a.charAt(a.length - 1))
const xx = Number.parseInt(a.charAt(a.length - 2) + a.charAt(a.length - 1))
if (xx > 5 && xx < 20) return w[0]
if (x === 1) return w[1]
if (x > 1 && x < 5) return w[2]
} catch (error) {
console.error('[utils] plural error', error)
}
return w[0]
}
2022-10-07 19:35:53 +00:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2022-09-09 11:53:35 +00:00
export const shuffle = (items: any[]) => {
const cached = [...items]
let temp
let i = cached.length
let rand
while (--i) {
rand = Math.floor(i * Math.random())
temp = cached[rand]
cached[rand] = cached[i]
cached[i] = temp
}
return cached
}
export const snake2camel = (s: string) =>
s
.split(/(?=[A-Z])/)
.join('-')
.toLowerCase()
2022-11-26 21:27:54 +00:00
export const formatDate = (date: Date, options: Intl.DateTimeFormatOptions = {}) => {
const opts = Object.assign(
{},
{
month: 'long',
day: 'numeric',
year: 'numeric'
},
options
)
return date.toLocaleDateString('ru', opts).replace(' г.', '')
}