48 lines
1.3 KiB
TypeScript
48 lines
1.3 KiB
TypeScript
|
/**
|
|||
|
* @description 添加单位
|
|||
|
* @param {String | Number} value 值 100
|
|||
|
* @param {String} unit 单位 px em rem
|
|||
|
*/
|
|||
|
export const addUnit = (value: string | number, unit = 'px') => {
|
|||
|
return !Object.is(Number(value), NaN) ? `${value}${unit}` : value
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @description 树转数组,队列实现广度优先遍历
|
|||
|
* @param {Array} data 数据
|
|||
|
* @param {Object} props `{ children: 'children' }`
|
|||
|
*/
|
|||
|
|
|||
|
export const treeToArray = (data: any[], props = { children: 'children' }) => {
|
|||
|
data = JSON.parse(JSON.stringify(data))
|
|||
|
const { children } = props
|
|||
|
const newData = []
|
|||
|
const queue: any[] = []
|
|||
|
data.forEach((child: any) => queue.push(child))
|
|||
|
while (queue.length) {
|
|||
|
const item: any = queue.shift()
|
|||
|
if (item[children]) {
|
|||
|
item[children].forEach((child: any) => queue.push(child))
|
|||
|
delete item[children]
|
|||
|
}
|
|||
|
newData.push(item)
|
|||
|
}
|
|||
|
return newData
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* @description 获取正确的路经
|
|||
|
* @param {String} path 数据
|
|||
|
*/
|
|||
|
export function getNormalPath(path: string) {
|
|||
|
if (path.length === 0 || !path || path == 'undefined') {
|
|||
|
return path
|
|||
|
}
|
|||
|
const newPath = path.replace('//', '/')
|
|||
|
const length = newPath.length
|
|||
|
if (newPath[length - 1] === '/') {
|
|||
|
return newPath.slice(0, length - 1)
|
|||
|
}
|
|||
|
return newPath
|
|||
|
}
|