index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. const common_vendor = require("../../common/vendor.js");
  3. const formatToFraction = (num) => {
  4. if (typeof num === "undefined")
  5. return 0;
  6. const parsedNumber = typeof num === "string" ? parseFloat(num) : num;
  7. return parseFloat((parsedNumber / 100).toFixed(2));
  8. };
  9. const floatToFixed2 = (num) => {
  10. let str = "0.00";
  11. if (typeof num === "undefined") {
  12. return str;
  13. }
  14. const f = formatToFraction(num);
  15. const decimalPart = f.toString().split(".")[1];
  16. const len = decimalPart ? decimalPart.length : 0;
  17. switch (len) {
  18. case 0:
  19. str = f.toString() + ".00";
  20. break;
  21. case 1:
  22. str = f.toString() + ".0";
  23. break;
  24. case 2:
  25. str = f.toString();
  26. break;
  27. }
  28. return str;
  29. };
  30. function formatDate(date, format) {
  31. if (!date) {
  32. return "";
  33. }
  34. if (format === void 0) {
  35. format = "YYYY-MM-DD HH:mm:ss";
  36. }
  37. return common_vendor.dayjs(date).format(format);
  38. }
  39. function handleTree(data, id = "id", parentId = "parentId", children = "children", rootId = 0) {
  40. const cloneData = JSON.parse(JSON.stringify(data));
  41. const treeData = cloneData.filter((father) => {
  42. let branchArr = cloneData.filter((child) => {
  43. return father[id] === child[parentId];
  44. });
  45. branchArr.length > 0 ? father.children = branchArr : "";
  46. return father[parentId] === rootId;
  47. });
  48. return treeData !== "" ? treeData : data;
  49. }
  50. function resetPagination(pagination) {
  51. pagination.list = [];
  52. pagination.total = 0;
  53. pagination.pageNo = 1;
  54. }
  55. exports.floatToFixed2 = floatToFixed2;
  56. exports.formatDate = formatDate;
  57. exports.handleTree = handleTree;
  58. exports.resetPagination = resetPagination;