前端常用方法(持续更新)

1、判断一个值是否为空
//为空,返回true
export const isEmpty = (v) => {
	switch (typeof v) {
		case 'undefined':
			return true;
		case 'string':
			if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
			break;
		case 'boolean':
			if (!v) return true;
			break;
		case 'number':
			if (0 === v || isNaN(v)) return true;
			break;
		case 'object':
			if (null === v || v.length === 0) return true;
			for (var i in v) {
				return false;
			}
			return true;
	}
	return false;
}
2、获取当前时间
//获取当前时间(这个时间还需要处理)
export const getCurrentTimes = () => {
	const date = new Date();
	const year = date.getFullYear();
	const month = date.getMonth() + 1;
	const day = date.getDate();
	const hour = date.getHours();
	const minute = date.getMinutes();
	const second = date.getSeconds();
	return {
		detail: {
			year: year,
			month: month,
			day: day,
			hour: withData(hour),
			minute: withData(minute),
			second: withData(second)
		}
	}
}
//时分秒 不足补零
var withData = (num) => {
	let param = parseInt(num);
	return param < 10 ? '0' + param : '' + param;
}
3、将毫秒数转为年月日时分秒
export const convertMsToSeconds = (data) => {
	var now = new Date(data);
	//获取年份
	var year = now.getFullYear();
	//获取月份
	var month = now.getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	//获取日
	var date = now.getDate();
	if (date < 10) {
		date = "0" + date;
	}
	//获取小时
	var h = now.getHours() < 10 ? '0' + now.getHours() : now.getHours();
	//获取分钟
	var m = now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes();
	//获取秒
	var s = now.getSeconds() < 10 ? '0' + now.getSeconds() : now.getSeconds();

	return `${year}-${month}-${date} ${h}:${m}:${s}`
}
4、比较日期的大小
export const compareDate = (beginTime, endTime) => {
	var beginTimes = beginTime.substring(0, 10).split('-');
	var endTimes = endTime.substring(0, 10).split('-');
	beginTime = beginTimes[1] + '-' + beginTimes[2] + '-' + beginTimes[0] + ' ' + beginTime.substring(10, 19);
	endTime = endTimes[1] + '-' + endTimes[2] + '-' + endTimes[0] + ' ' + endTime.substring(10, 19);
	var a = (Date.parse(endTime) - Date.parse(beginTime)) / 3600 / 1000;
	if (a < 0 || a == 0) {
		//结束时间小
		return true
	} else {
		return false
	}
}
5、js计算精度问题
1)mathjs官网
2)示例地址
//npm install mathjs
var math = require("mathjs")
// require()
//小数相加精度失真
export const numAdd = (num1, num2) => {
	let amount=math.add(math.bignumber(num1),math.bignumber(num2))
	let result = math.number(amount)
	return result
}
//小数相减精度失真
export const numSub = (num1, num2) => {
	let amount=math.subtract(math.bignumber(num1),math.bignumber(num2))
	let result = math.number(amount)
	return result
}
//小数相乘精度失真
export const numMulti = (num1, num2) => {
	let amount=math.multiply(math.bignumber(num1),math.bignumber(num2))
	let result = math.number(amount)
	return result
}

//小数相除精度失真
export const numDiv = (num1, num2) => {
	let amount=math.divide(math.bignumber(num1),math.bignumber(num2))
	let result = math.number(amount)
	return result
}
6、获取url上以&拼接的参数
export const getQueryVariable = (variable) => {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return (false);
}
7、压缩base64图片
export const dealImage = (base64, w, callback) => {
	// console.log(base64)
	var newImage = new Image();
	var quality = 0.5; //压缩系数0-1之间
	if (newImage) {
		newImage.src = base64;
	}
	// newImage.setAttribute("crossOrigin", 'Anonymous'); //url为外域时需要
	var imgWidth, imgHeight;
	newImage.onload = function() {
		imgWidth = this.width;
		imgHeight = this.height;
		var canvas = document.createElement("canvas");
		var ctx = canvas.getContext("2d");
		if (Math.max(imgWidth, imgHeight) > w) {
			if (imgWidth > imgHeight) {
				canvas.width = w;
				canvas.height = w * imgHeight / imgWidth;
			} else {
				canvas.height = w;
				canvas.width = w * imgWidth / imgHeight;
			}
		} else {
			canvas.width = imgWidth;
			canvas.height = imgHeight;
			quality = 0.5;
		}
		ctx.clearRect(0, 0, canvas.width, canvas.height);
		ctx.drawImage(this, 0, 0, canvas.width, canvas.height);
		var base64 = canvas.toDataURL("image/jpeg", quality); //压缩语句
		// 如想确保图片压缩到自己想要的尺寸,如要求在50-150kb之间,请加以下语句,quality初始值根据情况自定
		while (base64.length / 1024 > 150) {
			quality -= 0.01;
			base64 = canvas.toDataURL("image/jpeg", quality);
		}
		// 防止最后一次压缩低于最低尺寸,只要quality递减合理,无需考虑
		while (base64.length / 1024 < 50) {
			quality += 0.001;
			base64 = canvas.toDataURL("image/jpeg", quality);
		}
		callback(base64); //必须通过回调函数返回,否则无法及时拿到该值
		// return base64
	}
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值