61 lines
1.9 KiB
JavaScript
61 lines
1.9 KiB
JavaScript
export function parseTime(time) {
|
||
if (arguments.length === 0 || !time) {
|
||
return "--";
|
||
}
|
||
let date;
|
||
if (typeof time === "object") {
|
||
date = time;
|
||
} else {
|
||
if (typeof time === "string") {
|
||
if (/^[0-9]+$/.test(time)) {
|
||
time = parseInt(time);
|
||
} else {
|
||
time = time.replace(new RegExp(/-/gm), "/");
|
||
}
|
||
}
|
||
date = new Date(time);
|
||
}
|
||
const formatRes = {
|
||
y: date.getFullYear(),
|
||
M: date.getMonth() + 1,
|
||
d: date.getDate(),
|
||
h: date.getHours(),
|
||
m: date.getMinutes(),
|
||
s: date.getSeconds(),
|
||
week: ["日", "一", "二", "三", "四", "五", "六"][date.getDay()],
|
||
};
|
||
formatRes.M < 10 && (formatRes.M = "0" + formatRes.M);
|
||
formatRes.d < 10 && (formatRes.d = "0" + formatRes.d);
|
||
formatRes.h < 10 && (formatRes.h = "0" + formatRes.h);
|
||
formatRes.m < 10 && (formatRes.m = "0" + formatRes.m);
|
||
formatRes.s < 10 && (formatRes.s = "0" + formatRes.s);
|
||
return formatRes;
|
||
}
|
||
|
||
export function debounce(fn, delay) {
|
||
let timer = null;
|
||
return function () {
|
||
if (timer) clearTimeout(timer);
|
||
let _this = this;
|
||
let _arguments = arguments;
|
||
timer = setTimeout(function () {
|
||
// 在执行时,通过apply来使用_this和_arguments
|
||
fn.apply(_this, _arguments);
|
||
}, delay);
|
||
};
|
||
}
|
||
|
||
export function isLicensePlate(no) {
|
||
let instance = new RegExp(
|
||
"^([京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[a-zA-Z](([DF]((?![IO])[a-zA-Z0-9](?![IO]))[0-9]{4})|([0-9]{5}[DF]))|[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1})$"
|
||
);
|
||
return instance.test(no);
|
||
}
|
||
|
||
// 类型判断
|
||
export function typeJudgment(object) {
|
||
let res = {}.__proto__.toString.call(object);
|
||
let type = /(?<= ).+(?=\])/.exec(res);
|
||
return type.length ? type[0] : "";
|
||
}
|