开发常用函数合集

本文介绍了一系列实用的JavaScript工具函数,包括URL参数序列化与解析、WebP图片支持检测及时间格式化等。这些函数能够帮助开发者高效处理常见的前端任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

util.js

export const serialize = (obj) => Object.keys(obj).map((item) => `${encodeURIComponent(item)}=${encodeURIComponent(obj[item])}`).join('&')

export const getURLParams = (str) => {
	if (typeof str !== 'string') {
		throw new Error('str 必须是字符串类型')
	}

	const hash = {}

	str.replace(/([^=&]+)=([^=&]+)/g, (match, key, value) => {
		key = decodeURIComponent(key)
		value = decodeURIComponent(value)

		if (! isNaN(Number(value))) {
			value = Number(value)
		}

		if (hash[key]) {
			hash[key] = [].concat(hash[key], value)
		} else {
			hash[key] = value
		}
	})

	return hash
}

export function checkWebpSupport() {
	let image = new Image()

	image.onload = () => {
		if (image.width === 1) {
			document.cookie = `webp=avaiable; expires=${new Date(Date.now() + (1 * 365 * 24 * 60 * 60 * 1000)).toUTCString()}; path=/`

			image.onload = null
			image = null
		}
	}

	image.src = 'data:image/webp;base64,UklGRkoAAABXRUJQVlA4WAoAAAAQAAAAAAAAAAAAQUxQSAwAAAARBxAR/Q9ERP8DAABWUDggGAAAABQBAJ0BKgEAAQAAAP4AAA3AAP7mtQAAAA=='
}

export function timeFormat(timestamp) {
	const now = Date.now()
	const date = new Date()
	const aa = + new Date(date.getFullYear(), date.getMonth())

	const v = date.getDay() === 0 ? 6 : date.getDay() - 1
	const bb = + new Date(date.getFullYear(), date.getMonth(), date.getDate() - v)

	const agoDate = new Date(timestamp)

	const yearNow = date.getFullYear()
	const monthNow = date.getMonth()
	const dateNow = date.getDate()

	const yearAgo = agoDate.getFullYear()
	const monthAgo = agoDate.getMonth()
	const dateAgo = agoDate.getDate()

	const hourAgo = agoDate.getHours()
	const minutesAgo = agoDate.getMinutes()

	let str

	// 1小时内
	if (now - timestamp <= 60 * 60 * 1000) {
		str =  ~~ ((now - timestamp) / (60 * 1000))

		return str <= 0 ? '刚刚' : `${str}分钟前`
	}

	// 24小时内
	if (yearNow === yearAgo && monthNow === monthAgo && dateNow === dateAgo) {
		str =  ~~ ((now - timestamp) / (60 * 60 * 1000))

		return `${str}小时前`
	}

	// 昨天
	if (yearNow === yearAgo && monthNow === monthAgo) {
		if (dateNow - dateAgo === 1) {
			return `昨天 ${hourAgo}:${minutesAgo}`.replace(/\b(\w)\b/g, '0$1')
		}
	}

	if (yearNow === yearAgo && monthNow - monthAgo === 1) {
		const max = 2 * 24 * 60 * 60 * 1000
		const min = 1
		const timeDiff = now - timestamp

		if (timeDiff >= min && timeDiff < max) {
			return `昨天 ${hourAgo}:${minutesAgo}`
		}
	}

	// 小于一年
	if (date.getFullYear() === agoDate.getFullYear()) {
		return `${agoDate.getMonth() + 1}${agoDate.getDate()}${hourAgo}:${minutesAgo}`.replace(/\b(\w)\b/g, '0$1')
	}

	// 大于一年
	return `${agoDate.getFullYear()}-${agoDate.getMonth() + 1}-${agoDate.getDate()}`.replace(/\b(\w)\b/g, '0$1')
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值