JSFiddle - React, Tailwind, and code Playground

by anup1986

JavaScript

const PORTAL_PATHS = {
	HOME: "/",
  HELP: "/help",
  OTHER: "/other"
}

const matchPath = (pathName, settings) => {
	const route = Object.values(PORTAL_PATHS).find(x => x === pathName)
  return route ? { path: route, exact: true } : route
}

const findMatch = pathname => {
  for (const path in Object.values(PORTAL_PATHS)) {
    const match = matchPath(pathname, { path, exact: true })
    if (match) return match
  }
  return null
}

const findMatch_org = pathname => {
  let result
  Object.values(PORTAL_PATHS).forEach(path => {
    const match = matchPath(pathname, { path, exact: true })
    if (match) {
      result = match
    }
  })
  return result
}

const pathToFind = '/iiik'

console.log(findMatch(pathToFind))
console.log(findMatch_org(pathToFind))