function throttle(callback: Function, delay: number) {
let timer: number | null
return function () {
if (timer) return
const args = arguments
timer = setTimeout(() => {
callback.apply(null, args)
timer = null
}, delay)
}
}
function encodeURL(url: string, isComponent = true): string {
return isComponent ? encodeURIComponent(url) : encodeURI(url)
}
function decodeURL(url: string, isComponent = true): string {
return isComponent ? decodeURIComponent(url) : decodeURI(url)
}
function getCssVariableValue(cssVariableName: string): string {
return getComputedStyle(document.documentElement).getPropertyValue(cssVariableName)
}
function setCssVariableValue(cssVariableName: string, cssVariableValue: string): void {
document.documentElement.style.setProperty(cssVariableName, cssVariableValue)
}
function clearCookie(): void {
const keyList = document.cookie.match(/[^ =;]+(?==)/g) as string[] | null
keyList && keyList.forEach(key => (document.cookie = `${key}=0;path=/;expires=${new Date(0).toUTCString()}`))
}
function clearCache(): void {
window.localStorage.clear()
window.sessionStorage.clear()
const keyList = document.cookie.match(/[^ =;]+(?==)/g) as string[] | null
keyList && keyList.forEach(key => (document.cookie = `${key}=0;path=/;expires=${new Date(0).toUTCString()}`))
}
function getQueryByName(key, url = window.location.href) {
const queryNameRegExp = new RegExp(`[?&]${key}=([^&]*)(?:&|$)`)
const queryNameMatch = url.match(queryNameRegExp)
return queryNameMatch ? decodeURIComponent(queryNameMatch[1]) : null
}
function timeFix(): string {
const time = new Date()
const hour = time.getHours()
return hour < 9 ? 'Good morning' : hour <= 11 ? 'Good morning' : hour <= 13 ? 'Good afternoon' : hour < 20 ? 'Good afternoon' : 'Good evening'
}
function welcome(): string {
const list = ['Long time no see, I miss you so much! ', 'Wait until the stars go to sleep before I miss you', 'We are open today']
return list[Math.floor(Math.random() * list.length)]
}
function deepClone(source: any): any {
if (!source || typeof source !== 'object') return source
if (source instanceof Date) return new Date(source)
if (source instanceof RegExp) return new RegExp(source)
const target = Array.isArray(source) ? ([] as Record<any, any>) : ({} as Record<any, any>)
for (const key in source) target[key] = typeof source[key] === 'object' ? deepClone(source[key]) : source[key]
return target
}
function getRandomUUID(): string {
const tempURL = URL.createObjectURL(new Blob())
const uuidStr = tempURL.toString()
const separator = uuidStr.includes('/') ? '/' : ':'
URL.revokeObjectURL(tempURL)
return uuidStr.substring(uuidStr.lastIndexOf(separator) + 1)
}
function getRandomUUID(): string {
const fn = (): string => (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
return fn() + fn() + '-' + fn() + '-' + fn() + '-' + fn() + '-' + fn() + fn() + fn()
}
function getRandomBoolean(): boolean {
return Math.random() > 0.5
}
function reverseString(str: string): string {
return str.split('').reverse().join('')
}
function getRandomHexColor(): string {
return `#${Math.floor(Math.random() * 0xffffff).toString(16)}`
}
function getRawType(variable: any): string {
return Object.prototype.toString.call(variable).split(' ')[1].replace(']', '').toLowerCase()
}
function copyText(text: string): void {
const isClipboardApiSupported = window.navigator && window.navigator.clipboard
if (isClipboardApiSupported) {
window.navigator.clipboard.writeText(text)
} else {
const textarea = document.createElement('textarea')
textarea.readOnly = true
textarea.value = text
textarea.style.position = 'absolute'
textarea.style.top = '-9999px'
textarea.style.left = '-9999px'
document.body.appendChild(textarea)
textarea.select()
document.execCommand('copy')
textarea.remove()
}
}
function scrollToTop(element: HTMLElement): void {
element.scrollIntoView({ behavior: 'smooth', block: 'start' })
}
function scrollToBottom(element: HTMLElement): void {
element.scrollIntoView({ behavior: 'smooth', block: 'end' })
}
const obj = { a: 1, b: 2, c: 3, d: 4 }
Object.keys(obj)
Object.values(obj)
Object.entries(obj)
Object.fromEntries( [['a', 1], ['b', 2]])
obj.hasOwnProperty('a')
obj.hasOwnProperty('fff')
const target = { a: 1, b: 2 }
const source = { b: 4, c: 5 }
const result = Object.assign(target, source)
console.log(result)