'use strict'; /** * * * @author Zhong * @date 2019/11/12 * @version */ ((factory) => { if (typeof module !== 'undefined') { module.exports = factory(); } else { window.commonUtil = factory(); } })(() => { function isDef(val) { return typeof val !== 'undefined' && val !== null; } function isEmptyVal(val) { return val === null || val === undefined || val === ''; } // 将树数据排序好 function getSortedTreeData(rootID, items) { return sortSameDedth(rootID, items).reverse(); function sortSameDedth(parentID, items) { const sameDepthItems = items.filter(item => item.ParentID === parentID); if (!sameDepthItems.length) { return []; } const NextIDMapping = {}; sameDepthItems.forEach(item => NextIDMapping[item.NextSiblingID] = item); let curItem = sameDepthItems.length > 1 ? sameDepthItems.find(item => item.NextSiblingID === -1) : sameDepthItems[0]; const sorted = []; while (curItem) { sorted.push(...sortSameDedth(curItem.ID, items)); sorted.push(curItem); curItem = NextIDMapping[curItem.ID] || null; } return sorted; } } // 给数值加上分割 // eg: 1234567.00 => 1,234,567.00 function standardNumber(str) { if (typeof str === 'number') { str = String(str); } if (typeof str !== 'string') { return ''; } const [intPart, decimalPart] = str.split('.'); // 给整数部分加上“,” const temp = []; for (let i = intPart.length - 1, j = 1; i >= 0; i--, j++) { temp.push(intPart[i]); if (j !==0 && j % 3 === 0 && i - 1 >= 0) { temp.push(','); } } const standardIntPart = temp.reverse().join(''); return `${standardIntPart}${decimalPart ? '.' + decimalPart : ''}`; } return { isDef, isEmptyVal, getSortedTreeData, standardNumber, }; });