import { computed, watch } from 'vue' export function randomCode (codeLength) { const UPPER = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' const LOWER = UPPER.toLocaleLowerCase() const DIGITS = '0123456789' const ALPHA_NUMBER = UPPER + LOWER + DIGITS // 字符串转成数组 const symbols: string[] = [] for (let i = 0; i < ALPHA_NUMBER.length; i++) { symbols[i] = ALPHA_NUMBER.charAt(i) } // 随机拼接字符串 let stateCode = '' for (let i = 0; i < codeLength; i++) { stateCode += symbols[Math.floor((Math.random() * symbols.length))] } return stateCode } export function removeObjWithArr (_arr, _obj) { const length = _arr.length for (let i = 0; i < length; i++) { if (_arr[i] === _obj) { if (i === 0) { _arr.shift() return } else if (i === length - 1) { _arr.pop() return } else { _arr.splice(i, 1) return } } } } export function dateFormat (fmt, time) { const date = new Date(time) let ret const opt = { 'y+': date.getFullYear().toString(), // 年 'M+': (date.getMonth() + 1).toString(), // 月 'd+': date.getDate().toString(), // 日 'H+': date.getHours().toString(), // 时 'm+': date.getMinutes().toString(), // 分 's+': date.getSeconds().toString() // 秒 // 有其他格式化字符需求可以继续添加,必须转化成字符串 } for (const k in opt) { ret = new RegExp('(' + k + ')').exec(fmt) if (ret) { fmt = fmt.replace(ret[1], (ret[1].length === 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0'))) } } return fmt } // 转换树形结构 export function transformToTree (sNodes, setting) { let set = { children: 'children', idKey: 'id', pIdKey: 'pId', label: 'name' } if (setting) { set = Object.assign(set, setting) } let i let l const key = set.idKey const parentKey = set.pIdKey const childKey = set.children if (!key || key === '' || !sNodes) return [] if (Array.isArray(sNodes)) { const r: any = [] const tmpMap: any = [] for (i = 0, l = sNodes.length; i < l; i++) { tmpMap[sNodes[i][key]] = sNodes[i] } for (i = 0, l = sNodes.length; i < l; i++) { if ( tmpMap[sNodes[i][parentKey]] && sNodes[i][key] !== sNodes[i][parentKey] ) { if (!tmpMap[sNodes[i][parentKey]][childKey]) { tmpMap[sNodes[i][parentKey]][childKey] = [] } tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]) } else { r.push(sNodes[i]) } } return r } else { return [sNodes] } } export function treeToTransForm (data, setting) { let set = { idKey: 'id', pIdKey: 'pId', children: 'children' } if (setting) { set = Object.assign(set, setting) } const res: any[] = [] const childrenKey = set.children const fn = (source, key, pIdValue) => { source.forEach((el: any) => { if (key && pIdValue) { el[key] = pIdValue } res.push(el) el[childrenKey] && el[childrenKey].length > 0 ? fn(el[childrenKey], set.pIdKey, el[set.idKey]) : '' delete el[childrenKey] }) } fn(data, null, null) return res } // 判断非空 export function isEmpty (obj) { if (obj === null || obj === undefined || obj === '') { return true } else if (typeof obj === 'object') { for (const key in obj) { if (obj.hasOwnProperty(key)) { return false } } return true } else { return false } } export function is (val: unknown, type: string) { return toString.call(val) === `[object ${ type }]` } // 判断是否为函数 export function isFunction (obj) { return typeof obj === 'function' } export function isJsonString (val: unknown): val is string { return is(val, 'String') && ['[', '{'].includes(String(val).substring(0, 1)) } // 驼峰命名转换为中划线命名 export function camelToLine(str) { return (str.slice(0, 1) + str.slice(1).replace(/([A-Z])/g, '-$1')).toLowerCase() } interface IObject { [name: string]: any } /** * 遍历 tree * @param {object[]} tree * @param {function} cb - 回调函数 * @param {string} children - 子节点 字段名 * @param {string} mode - 遍历模式,DFS:深度优先遍历 BFS:广度优先遍历 */ export function treeForEach (tree: any[], cb: (item: any) => unknown, children = 'children', mode = 'DFS') { if (!Array.isArray(tree)) { throw new TypeError('tree is not an array') } if (typeof children !== 'string') { throw new TypeError('children is not a string') } if (children === '') { throw new Error('children is not a valid string') } // 深度优先遍历 depth first search function DFS (treeData: any[]) { // eslint-disable-next-line for (const item of treeData) { cb(item) if (Array.isArray(item[children])) { DFS(item[children]) } } } // 广度优先遍历 breadth first search function BFS (treeData: any[]) { const queen = treeData while (queen.length > 0) { const item = queen.shift() cb(item) if (Array.isArray(item[children])) { queen.push(...item[children]) } } } if (mode === 'BFS') { BFS(tree) } else { DFS(tree) } } /* 对比两个对象或数组 * a object or array * b object or array * */ export function compareOnA (a: any, b: any): boolean { return JSON.stringify(a) === JSON.stringify(b) }