Skip to content

Instantly share code, notes, and snippets.

@ranwahle
Created May 12, 2019 11:38
Show Gist options
  • Save ranwahle/345138dc8a54814b088f2e741b65b72c to your computer and use it in GitHub Desktop.
Save ranwahle/345138dc8a54814b088f2e741b65b72c to your computer and use it in GitHub Desktop.
Build routes tree from fragments
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