-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.ts
49 lines (40 loc) 路 1.25 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
export const isSSR = typeof window === 'undefined'
export const isLazyLoadingSupported = !isSSR && 'loading' in HTMLImageElement.prototype
export const isCrawler = !isSSR && (!('onscroll' in window) || /(gle|ing|ro)bot|crawl|spider/i.test(navigator.userAgent))
export function toElementArray<T extends HTMLElement>(
target: string | T | NodeListOf<T> | T[],
parentElement: Element | Document = document,
): T[] {
if (typeof target === 'string')
return [...parentElement.querySelectorAll<T>(target)]
if (target instanceof Element)
return [target]
return [...target]
}
export function getScaledDimensions(aspectRatio: number, referenceSize: number) {
let width: number
let height: number
if (aspectRatio >= 1) {
width = referenceSize
height = Math.round(referenceSize / aspectRatio)
}
else {
width = Math.round(referenceSize * aspectRatio)
height = referenceSize
}
return { width, height }
}
export function debounce<T extends (...args: any[]) => void>(
fn: T,
delay: number,
) {
let timeout: ReturnType<typeof setTimeout> | undefined
return function (...args: Parameters<T>) {
if (timeout)
clearTimeout(timeout)
timeout = setTimeout(() => {
timeout = undefined
fn(...args)
}, delay)
}
}