acdr-ui/dist/dev/mp-weixin/sheep/util/index.js.map

1 line
5.6 KiB
Plaintext
Raw Normal View History

2024-09-19 07:20:14 +08:00
{"version":3,"file":"index.js","sources":["../../../../../src/sheep/util/index.js"],"sourcesContent":["import dayjs from 'dayjs'\r\n\r\n/**\r\n * 将一个整数转换为分数保留两位小数\r\n * @param {number | string | undefined} num 整数\r\n * @return {number} 分数\r\n */\r\nexport const formatToFraction = (num) => {\r\n if (typeof num === 'undefined') return 0\r\n const parsedNumber = typeof num === 'string' ? parseFloat(num) : num\r\n return parseFloat((parsedNumber / 100).toFixed(2))\r\n}\r\n\r\n/**\r\n * 将一个数转换为 1.00 这样\r\n * 数据呈现的时候使用\r\n *\r\n * @param {number | string | undefined} num 整数\r\n * @return {string} 分数\r\n */\r\nexport const floatToFixed2 = (num) => {\r\n let str = '0.00'\r\n if (typeof num === 'undefined') {\r\n return str\r\n }\r\n const f = formatToFraction(num)\r\n const decimalPart = f.toString().split('.')[1]\r\n const len = decimalPart ? decimalPart.length : 0\r\n switch (len) {\r\n case 0:\r\n str = f.toString() + '.00'\r\n break\r\n case 1:\r\n str = f.toString() + '.0'\r\n break\r\n case 2:\r\n str = f.toString()\r\n break\r\n }\r\n return str\r\n}\r\n\r\n/**\r\n * 将一个分数转换为整数\r\n *\r\n * @param {number | string | undefined} num 分数\r\n * @return {number} 整数\r\n */\r\nexport const convertToInteger = (num) => {\r\n if (typeof num === 'undefined') return 0\r\n const parsedNumber = typeof num === 'string' ? parseFloat(num) : num\r\n // TODO 分转元后还有小数则四舍五入\r\n return Math.round(parsedNumber * 100)\r\n}\r\n\r\n/**\r\n * 时间日期转换\r\n * @param {dayjs.ConfigType} date 当前时间new Date() 格式\r\n * @param {string} format 需要转换的时间格式字符串\r\n * @description format 字符串随意,如 `YYYY-mm、YYYY-mm-dd`\r\n * @description format 季度:\"YYYY-mm-dd HH:MM:SS QQQQ\"\r\n * @description format 星期:\"YYYY-mm-dd HH:MM:SS WWW\"\r\n * @description format 几周:\"YYYY-mm-dd HH:MM:SS ZZZ\"\r\n * @description format 季度 + 星期 + 几周:\"YYYY-mm-dd HH:MM:SS WWW QQQQ ZZZ\"\r\n * @returns {string} 返回拼接后的时间字符串\r\n */\r\nexport function formatDate(date, format = 'YYYY-MM-DD HH:mm:ss') {\r\n // 日期不存在,则返回空\r\n if (!date) {\r\n return ''\r\n }\r\n // 日期存在,则进行格式化\r\n if (format === undefined) {\r\n format = 'YYYY-MM-DD HH:mm:ss'\r\n }\r\n return dayjs(date).format(format)\r\n}\r\n\r\n/**\r\n * 构造树型结构数据\r\n *\r\n * @param {*} data 数据源\r\n * @param {*} id id字段 默认 'id'\r\n * @param {*} parentId 父节点字段 默认 'parentId'\r\n * @param {*} children 孩子节点字段 默认 'children'\r\n * @param {*} rootId 根Id 默认 0\r\n */\r\nexport function handleTree(\r\n data,\r\n id = 'id',\r\n parentId = 'parentId',\r\n children = 'children',\r\n rootId = 0,\r\n) {\r\n // 对源数据深度克隆\r\n const cloneData = JSON.parse(JSON.stringify(data))\r\n // 循环所有项\r\n const treeData = cloneData.filter((father) => {\r\n const branchArr = cloneData.filter((child) => {\r\n // 返回每一项的子级数组\r\n return father[id] === child[parentId]\r\n })\r\n branchArr.length > 0 ? (father.children = branchArr) : ''\r\n // 返回第一层\r\n return father[parentId] === rootId\r\n })\r\n return treeData !== '' ? treeData : data\r\n}\r\n\r\n/**\r\n * 重置分页对象\r\n *\r\n * TODO 芋艿:需要处理其它页面\r\n *\r\n * @param pagination 分页对象\r\n */\r\nexport function resetPagination(pagination) {\r\n pagination.list = []\r\n pagination.total = 0\r\n pagination.pageNo = 1\r\n}\r\n\r\n/**\r\n * 将值复制到目标对象且以目标对象属性为准target: {a:1} source:{a:2,b:3} 结果为:{a:2}\r\n * @param target 目标对象\r\n * @param source 源对象\r\n */\r\nexport const copyValueToTarget = (target, source) => {\r\n const newObj = Object.assign({}, target, source)\r\n // 删除多余属性\r\n Object.keys(newO