import pathToRegexp from 'path-to-regexp'; export function urlToList(url) { if (url === '/') { return ['/']; } const urllist = url.split('/').filter((i) => i); return urllist.map( (urlItem, index) => `/${urllist.slice(0, index + 1).join('/')}`, ); } /* eslint no-useless-escape:0 */ const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/; export function isUrl(path) { return reg.test(path); } /** * Recursively flatten the data * [{path:string},{path:string}] => {path,path2} * @param menus */ export const getFlatMenuKeys = (menuData) => { let keys = []; menuData.forEach((item) => { keys.push(item.path); if (item.children) { keys = keys.concat(getFlatMenuKeys(item.children)); } }); return keys; }; export const getMenuMatches = (flatMenuKeys, path) => flatMenuKeys.filter((item) => { if (item) { return pathToRegexp(item.split('?')[0]).test(path.split('?')[0]); } return false; });