Skip to content

Instantly share code, notes, and snippets.

@bpinedah
Created March 17, 2020 00:40
Show Gist options
  • Save bpinedah/65a27c8bd8cc6486f32939d40f0fc9d3 to your computer and use it in GitHub Desktop.
Save bpinedah/65a27c8bd8cc6486f32939d40f0fc9d3 to your computer and use it in GitHub Desktop.
Router article
/**
* @description Find predicate to get the current route
* @param m Method from request
* @param p Path from request
* @returns {function(*)}
* @private
*/
_findAndComposeRoute(m, p) {
return route => {
// PARAMS
const { method: _method, path: _path } = route;
const isHome = _path.lastIndexOf("/") === _path.length - 1;
const methodValidation = _method === m;
const _params = this._getPathParams(_path, isHome ? `${p}/`: p);
const _matchKeys = Object.keys(_params);
const matchValidation = _matchKeys.length > 0;
const selected = _method !== "*" ? methodValidation && matchValidation :
matchValidation;
if (selected) {
route.params = _params.params ? {..._params.params} : {};
}
return selected;
};
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment