Router

by evgkch

JavaScript

const KnowledgeBase = 'KnowledgeBase';
const Home = 'Home';
const Result = 'Result';
const List = 'List';
const Article = 'Article';

const router = {
  path: 'knowledge',
  component: KnowledgeBase,
  indexLink: 'result',
  childRoutes: [
    {
      path: 'home',
      component: Home
    },
    {
      path: 'result',
      component: Result,
      indexLink: 'list',
      childRoutes: [
        {
          path: 'list',
          component: List
        },
        {
          path: 'article',
          component: Article
        }
      ]
    }
  ]
}

const Route = (path) => (router) => {
	
}

const findPathIndex = (router) => (path) => {
	let index;
  router.childRoutes.map((child, i) =>
  	(child.path === path)
    	? index = i
      : -1
  );
  return index;
}

const addPath = (router) => (path) => (buffer) => {
  buffer.push(router.path);
  if (router.childRoutes) {
    let index = findPathIndex(router)(path);
    return addPath(router.childRoutes[index])(path)(buffer);
  }
}

const getDefaultPath = (router) => {
  let buffer = [];
  addPath(router)(router.indexLink)(buffer);
  return buffer;
}

console.log(getDefaultPath(router))