/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path: string) {
  const isExternal = /^(https?:|http?:|mailto:|tel:)/.test(path);
  return isExternal;
}
export function publicTree(equiptrees:any,equipname:any) {
  let arr:any = []
  let newequiptree = JSON.parse(JSON.stringify(equiptrees)) 
  if(equipname !=''&& equipname !=null){
    newequiptree.forEach((item:any) => {
      if(item.name.toLowerCase().indexOf(equipname.toLowerCase()) !== -1){
        arr.push(item) 
      }
    })
  }else{
    arr = newequiptree
  }
  if(!Array.isArray(arr) || !arr.length) return;
    let map:any = {};
    arr.forEach(item => {//建立每个数组元素id和该对象的关系
      map[item.id] = item //这里可以理解为浅拷贝，共享引用 
    })
    let roots:any = [];
    arr.forEach(item => {
      const parent = map[item.parent_id];
      if(parent){
        (parent.children || (parent.children=[])).push(item);
      }
      else{
        roots.push(item);
      }
    })
    return roots
}