69 lines
1.9 KiB
JavaScript
69 lines
1.9 KiB
JavaScript
"use strict";
|
|
const common_vendor = require("../../../../common/vendor.js");
|
|
const formatToFraction = (num) => {
|
|
if (typeof num === "undefined")
|
|
return 0;
|
|
const parsedNumber = typeof num === "string" ? parseFloat(num) : num;
|
|
return parseFloat((parsedNumber / 100).toFixed(2));
|
|
};
|
|
const floatToFixed2 = (num) => {
|
|
let str = "0.00";
|
|
if (typeof num === "undefined") {
|
|
return str;
|
|
}
|
|
const f = formatToFraction(num);
|
|
const decimalPart = f.toString().split(".")[1];
|
|
const len = decimalPart ? decimalPart.length : 0;
|
|
switch (len) {
|
|
case 0:
|
|
str = f.toString() + ".00";
|
|
break;
|
|
case 1:
|
|
str = f.toString() + ".0";
|
|
break;
|
|
case 2:
|
|
str = f.toString();
|
|
break;
|
|
}
|
|
return str;
|
|
};
|
|
function formatDate(date, format = "YYYY-MM-DD HH:mm:ss") {
|
|
if (!date) {
|
|
return "";
|
|
}
|
|
if (format === void 0) {
|
|
format = "YYYY-MM-DD HH:mm:ss";
|
|
}
|
|
return common_vendor.dayjs(date).format(format);
|
|
}
|
|
function handleTree(data, id = "id", parentId = "parentId", children = "children", rootId = 0) {
|
|
const cloneData = JSON.parse(JSON.stringify(data));
|
|
const treeData = cloneData.filter((father) => {
|
|
const branchArr = cloneData.filter((child) => {
|
|
return father[id] === child[parentId];
|
|
});
|
|
branchArr.length > 0 ? father.children = branchArr : "";
|
|
return father[parentId] === rootId;
|
|
});
|
|
return treeData !== "" ? treeData : data;
|
|
}
|
|
function resetPagination(pagination) {
|
|
pagination.list = [];
|
|
pagination.total = 0;
|
|
pagination.pageNo = 1;
|
|
}
|
|
const copyValueToTarget = (target, source) => {
|
|
const newObj = Object.assign({}, target, source);
|
|
Object.keys(newObj).forEach((key) => {
|
|
if (Object.keys(target).indexOf(key) === -1) {
|
|
delete newObj[key];
|
|
}
|
|
});
|
|
Object.assign(target, newObj);
|
|
};
|
|
exports.copyValueToTarget = copyValueToTarget;
|
|
exports.floatToFixed2 = floatToFixed2;
|
|
exports.formatDate = formatDate;
|
|
exports.handleTree = handleTree;
|
|
exports.resetPagination = resetPagination;
|