Created
May 12, 2019 11:38
-
-
Save ranwahle/345138dc8a54814b088f2e741b65b72c to your computer and use it in GitHub Desktop.
Build routes tree from fragments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildRoutesTreeFromFragments(urlFragments, routes) { | |
if (!routes.filter) { | |
throw new Error('Routes should be an array'); | |
} | |
const candidates = routes.filter(route => this.isRouteSuitable(route, urlFragments)); | |
if (candidates.length === 0) { | |
return null; | |
} else if (candidates.length === 1) { | |
return this.buildRouteObject(candidates[0], urlFragments); | |
} else { | |
throw new Error(`More than one route corresponds to ${urlFragments.join('/')}`); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment