118 lines
3.1 KiB
TypeScript
118 lines
3.1 KiB
TypeScript
/**
|
||
* Check if an element has a class
|
||
* @param {HTMLElement} ele
|
||
* @param {string} cls
|
||
* @returns {boolean}
|
||
*/
|
||
export function hasClass(ele: HTMLElement, cls: string) {
|
||
return !!ele.className.match(new RegExp("(\\s|^)" + cls + "(\\s|$)"));
|
||
}
|
||
|
||
/**
|
||
* Add class to element
|
||
* @param {HTMLElement} ele
|
||
* @param {string} cls
|
||
*/
|
||
export function addClass(ele: HTMLElement, cls: string) {
|
||
if (!hasClass(ele, cls)) ele.className += " " + cls;
|
||
}
|
||
|
||
/**
|
||
* Remove class from element
|
||
* @param {HTMLElement} ele
|
||
* @param {string} cls
|
||
*/
|
||
export function removeClass(ele: HTMLElement, cls: string) {
|
||
if (hasClass(ele, cls)) {
|
||
const reg = new RegExp("(\\s|^)" + cls + "(\\s|$)");
|
||
ele.className = ele.className.replace(reg, " ");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 判断是否是外部链接
|
||
*
|
||
* @param {string} path
|
||
* @returns {Boolean}
|
||
*/
|
||
export function isExternal(path: string) {
|
||
const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
|
||
return isExternal;
|
||
}
|
||
|
||
/**
|
||
* 格式化增长率,保留两位小数 ,并且去掉末尾的0 取绝对值
|
||
*
|
||
* @param growthRate
|
||
* @returns
|
||
*/
|
||
export function formatGrowthRate(growthRate: number) {
|
||
if (growthRate === 0) {
|
||
return "-";
|
||
}
|
||
|
||
const formattedRate = Math.abs(growthRate * 100)
|
||
.toFixed(2)
|
||
.replace(/\.?0+$/, "");
|
||
return formattedRate + "%";
|
||
}
|
||
/**
|
||
* Parse the time to string
|
||
* @param {(Object|string|number)} time
|
||
* @param {string} cFormat
|
||
* @returns {string}
|
||
*/
|
||
export function parseTime(time: string | number | Date | null, cFormat: string | undefined) {
|
||
if (arguments.length === 0) {
|
||
return null;
|
||
}
|
||
const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
|
||
let date;
|
||
if (typeof time === "undefined" || time === null || time === "null") {
|
||
return "";
|
||
} else if (typeof time === "object") {
|
||
date = time;
|
||
} else {
|
||
if (typeof time === "string" && /^[0-9]+$/.test(time)) {
|
||
time = parseInt(time);
|
||
}
|
||
if (typeof time === "number" && time.toString().length === 10) {
|
||
time = time * 1000;
|
||
}
|
||
date = new Date(time);
|
||
}
|
||
const formatObj = {
|
||
y: date.getFullYear(),
|
||
m: date.getMonth() + 1,
|
||
d: date.getDate(),
|
||
h: date.getHours(),
|
||
i: date.getMinutes(),
|
||
s: date.getSeconds(),
|
||
a: date.getDay()
|
||
};
|
||
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key: keyof typeof formatObj): string => {
|
||
let value = formatObj[key];
|
||
// Note: getDay() returns 0 on Sunday
|
||
if (key === "a") {
|
||
return ["日", "一", "二", "三", "四", "五", "六"][value];
|
||
}
|
||
if (result.length > 0 && value < 10) {
|
||
value = Number("0" + value.toString());
|
||
}
|
||
return value.toString() || "0";
|
||
});
|
||
return time_str;
|
||
}
|
||
|
||
// 下载文件
|
||
export function downloadFile(obj: BlobPart, name: string, suffix: string) {
|
||
const url = window.URL.createObjectURL(new Blob([obj]));
|
||
const link = document.createElement("a");
|
||
link.style.display = "none";
|
||
link.href = url;
|
||
const fileName = parseTime(new Date(), undefined) + "-" + name + "." + suffix;
|
||
link.setAttribute("download", fileName);
|
||
document.body.appendChild(link);
|
||
link.click();
|
||
document.body.removeChild(link);
|
||
} |