前端常用方法(持续更新)
js常见方法
·
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
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)