You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dashboard/src/utils/commons.js

266 lines
6.2 KiB
JavaScript

3 years ago
// https://github.com/uxitten/polyfill/blob/master/string.polyfill.js
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/padStart
import { Tag } from "antd";
import { CaretUpOutlined, CaretDownOutlined } from "@ant-design/icons";
import moment from "moment";
3 years ago
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
2 years ago
targetLength = targetLength >> 0; // floor if number or convert non-number to 0;
padString = String(typeof padString !== "undefined" ? padString : " ");
if (this.length > targetLength) {
return String(this);
} else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
2 years ago
padString += padString.repeat(targetLength / padString.length); // append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
3 years ago
}
export function copy(obj) {
return JSON.parse(JSON.stringify(obj));
3 years ago
}
export function named(value) {
return function (target) {
target.definedName = value;
};
3 years ago
}
export function formatCurrency(name) {
if (name === "USD") {
return "$";
} else if (name === "RMB") {
return "¥";
} else if (name === "EUR") {
return "€";
} else if (name === "GBP") {
return "£";
} else {
return name + " ";
}
3 years ago
}
export function formatPrice(price) {
return Math.ceil(price).toLocaleString();
}
2 years ago
// 千分符的金额转成数字默认为0
export function price_to_number(price) {
2 years ago
const num_string = (price + "").replace(/,/g, "");
const number = parseFloat(num_string);
return isNaN(number) ? 0 : number;
3 years ago
}
export function formatPercent(number) {
return Math.round(number * 100) + "%";
3 years ago
}
export function formatDate(date) {
if (isEmpty(date)) {
return "NaN";
}
3 years ago
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
3 years ago
const monthStr = ("" + month).padStart(2, 0);
const dayStr = ("" + day).padStart(2, 0);
const formatted = year + "-" + monthStr + "-" + dayStr;
3 years ago
return formatted;
3 years ago
}
export function formatTime(date) {
const hours = date.getHours();
const minutes = date.getMinutes();
3 years ago
const hoursStr = ("" + hours).padStart(2, 0);
const minutesStr = ("" + minutes).padStart(2, 0);
const formatted = hoursStr + ":" + minutesStr;
3 years ago
return formatted;
3 years ago
}
export function formatDatetime(date) {
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
3 years ago
const monthStr = ("" + month).padStart(2, 0);
const dayStr = ("" + day).padStart(2, 0);
3 years ago
const hours = date.getHours();
const minutes = date.getMinutes();
3 years ago
const hoursStr = ("" + hours).padStart(2, 0);
const minutesStr = ("" + minutes).padStart(2, 0);
3 years ago
const formatted = year + "-" + monthStr + "-" + dayStr + " " + hoursStr + ":" + minutesStr;
3 years ago
return formatted;
3 years ago
}
export function mixins(...list) {
return function (target) {
list.forEach(val => {
const mixinObj = Object.create(val.prototype, {});
const name = Object.getPrototypeOf(mixinObj).constructor.name;
const camelCase = name.substr(0, 1).toLowerCase() + name.substr(1);
Object.assign(target.prototype, { [camelCase]: mixinObj });
});
};
3 years ago
}
export function camelCase(name) {
return name.substr(0, 1).toLowerCase() + name.substr(1);
3 years ago
}
export class UrlBuilder {
constructor(url) {
this.url = url;
this.paramList = [];
}
append(name, value) {
if (isNotEmpty(value)) {
2 years ago
this.paramList.push({ name, value });
}
return this;
}
build() {
this.paramList.forEach((e, i, a) => {
if (i === 0) {
this.url += "?";
} else {
this.url += "&";
}
this.url += e.name + "=" + e.value;
});
return this.url;
}
3 years ago
}
export function isNotEmpty(val) {
return val !== undefined && val !== null && val !== "";
3 years ago
}
export function isEmpty(val) {
return val === undefined || val === null || val === "";
3 years ago
}
export function empty(a) {
2 years ago
if (a === "") return true; // 检验空字符串
if (a === "null") return true; // 检验字符串类型的null
if (a === "undefined") return true; // 检验字符串类型的 undefined
if (!a && a !== 0 && a !== "") return true; // 检验 undefined 和 null
if (Array.prototype.isPrototypeOf(a) && a.length === 0) return true; // 检验空数组
if (Object.prototype.isPrototypeOf(a) && Object.keys(a).length === 0) return true; // 检验空对象
return false;
3 years ago
}
export function prepareUrl(url) {
return new UrlBuilder(url);
3 years ago
}
export function debounce(fn, delay = 500) {
let timer;
return e => {
e.persist();
clearTimeout(timer);
timer = setTimeout(() => {
fn(e);
}, delay);
};
}
3 years ago
export function throttle(fn, delay, atleast) {
2 years ago
let timeout = null;
let startTime = new Date();
return function () {
2 years ago
const curTime = new Date();
clearTimeout(timeout);
if (curTime - startTime >= atleast) {
fn();
startTime = curTime;
} else {
timeout = setTimeout(fn, delay);
}
};
3 years ago
}
export function clickUrl(url) {
const httpLink = document.createElement("a");
httpLink.href = url;
httpLink.target = "_blank";
httpLink.click();
}
export function show_vs_tag(vs, vs_diff, data1, data2) {
let tag = "-";
if (parseInt(vs) < 0) {
tag = (
<Tag icon={<CaretDownOutlined />} color="gold">
{vs} {vs_diff}
</Tag>
);
} else if (parseInt(vs) > 0) {
tag = (
<Tag icon={<CaretUpOutlined />} color="lime">
{vs} {vs_diff}
</Tag>
);
}
return (
<span>
<div>
{data1} vs {data2}
</div>
{tag}
</span>
);
}
2 years ago
// 数组去掉重复
export function unique(arr) {
2 years ago
const x = new Set(arr);
return [...x];
}
export function getWeek(date) {
// 参数时间戳
2 years ago
const week = moment(date).day();
switch (week) {
case 1:
return "周一";
case 2:
return "周二";
case 3:
return "周三";
case 4:
return "周四";
case 5:
return "周五";
case 6:
return "周六";
case 0:
return "周日";
}
}
2 years ago
// 把非数字下标的数组设置下标因为非数字数组的length为0导致读取失败
export function set_array_index(result) {
2 years ago
const result_array = [];
const result_keys = Object.keys(result);
result_keys.sort(); // 必须做一次排序用for in循环会导致顺序错误
for (const key of result_keys) {
result_array.push(result[key]);
}
return result_array;
}