1 line
5.6 KiB
Plaintext
1 line
5.6 KiB
Plaintext
{"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(newObj).forEach((key) => {\r\n // 如果不是target中的属性则删除\r\n if (Object.keys(target).indexOf(key) === -1) {\r\n delete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